import { app } from "../App";
import { TbGEntity } from "../config/data/schema";
import PlayerPetData from "../data/PlayerPetData";
import ResourceData from "../data/ResourceData";
import { GUI } from "../ui/UIConfig";
import { GAttribute } from "./entity/EntityData";

//接受到JSON消息
export const RData = (data:any,isTips:boolean = false) => {
    if(data.data.state == 200){
        //如果有 Resource 字段 表示要刷新资源
        if(data.data['resources']){
            //刷新资源
            data.data['resources'].forEach(res => {
                ResourceData.getIns().onUpdateOV(res.operation,res.resource);   //刷新资源
                PlayerPetData.getIns().onUpdateOV(res.operation,res.pet);       //刷新宠物
            })
        }
        //如果有 rewards 字段 表示要弹出获得资源
        if(data.data['rewards'] && data.data['rewards'].length){
            //弹出获得资源
            app.layer.Open(GUI.RewardClaimView,data.data['rewards'])
        }
        //弹出提示
        if(isTips){
            app.layer.Open(GUI.Tips,{text:data.data.msg});
        }
        return data.data.data;
    }else{
        //弹出提示
        if(isTips){
            app.layer.Open(GUI.Tips,{text:data.data.msg});
        }
        return data.data.data;
    }
}
//接受到Protobuf
export const RProto = (data:any,type:string) => {

    try{
        return app.proto
            .getType(type)
            .decode(new Uint8Array(data.data)) as any;;
    }catch{
        app.layer.Open(GUI.Tips,{text:"Protobuf 解析失败"});
    }

}

/************** 请求类 *******************/
export interface NewsContext{
    state:number,
    msg:string,
    data:any,
}
//玩家登录返回
export interface UserLoginVO{
    token:string,       //token
    user:UserVO,        //玩家信息
}



/************** 实体类 **************************/

//玩家信息
export interface UserVO{
    userId:number,      //玩家Id
    userName:string,    //玩家名称
    userPass:string,    //玩家密码
}
//游戏玩家信息
export interface PlayerInfoOV{
    playerId:number,    //玩家Id
    userId: number,     //用户Id
    playerName:string,  //玩家名
    playerCreateTime:number, //玩家创建时间
    novice: false,      //是否过引导
}
//玩家宠物信息
export interface PlayerPetOV{
    petId:number,               //宠物唯一Id
    petPlayerId:number;         //宠物的玩家Id
    petTbId:number;             //宠物配置表Id
    petStar:number;             //宠物星级
    petStarExp:number;          //宠物星级经验
}
//玩家阵法信息
export interface PlayerTacticalOV{
    playerId:number,            //玩家Id
    tacticalData:string,        //阵法数据
}
//玩家资源
export interface ResourceOV{
    resourceId:number;                //资源Id
    playerId:number;                  //玩家Id
    resourceTbId:number;              //资源配置表Id
    resourceValue:number;             //资源数量
    version:number;                   //版本号
}

export interface ModeOnHookRankingOV{
    playerId:number;                //游戏玩家Id
    playerName:string;              //游戏玩家名称
    levelId:number;                 //玩家关卡Id
    rank:number;                    //玩家当前排名
    mapId:number;                   //地图Id
}


//宠物装备
export interface PetEquip{
    equipId:number;                    //装备唯一Id
    equipCfgId:number;                 //装备配置表Id
    equipPlayerId:number;              //装备的所属玩家Id
    equipLevel:number;                 //装备等级
    equipPetId:number;                  //当前装备穿戴的宠物Id
    equipPosition:number;              //装备部位

    equipBaseAttributes:GAttribute[];   //基础属性
    equipHighAttributes:GAttribute[];   //高级属性
    
}

//宠物锻造台
export interface EquipForgingBench{
    forgingId:number;                       //锻造台Id
    playerId:number;                        //锻造台所属的玩家Id
    forgingExp:number;                      //锻造等级经验
    forgingQuality:number;                  //锻造品质等级
    forgingPetId:number;                    //当前占用宠物Id
    currentForgingQuality:number;           //当前锻造等级经验
    forgingUpTimeExcess: number;            //升级剩余时间
    forgingPetLevels:{[key:string]:number}  //宠物等级
}

//宠物锻造OV
export interface PetEquipForgingOV{
    info:EquipForgingBench;
    equip:PetEquip;
}


//礼包每天充值记录
export interface GiftRecord{
    giftId:number;                       //礼包Id
    playerId:number;                     //玩家Id
    giftCfgId:number;                   //礼包配置表Id
    giftCfgName;               //礼包名称
    giftCfgRewards:TbGEntity.TReward[];     //礼包奖励
    giftCfgPrice:number;                //礼包价格
    giftBuyingTime:number;              //礼包购买时间戳
}
export interface GiftDayRecord{
    giftDayId:number;   //充值Id
    playerId:number;    //玩家Id
    giftId:number;      //礼包Id
    giftCfgId:number;   //礼包配置表Id
}


export const API = {

    /********** debugger *****************/
    DebuggerAddResource : async (type:number,value:number) => RData(await app.api.post(`/debugger/add/resource/${type}/${value}`),true) as Boolean,    //Debuger 添加指定资源

    UserRegister : async () => RData(await app.api.post(`/user/register`)) as UserVO, //玩家注册
    UserLogin : async (account:string,password:string) => RData(await app.api.post(`/user/login`,{userId:account,userPass:password})) as UserLoginVO, //玩家登录
    GetPlayerInfo : async () => RData(await app.api.get(`/game/player/info`),false) as PlayerInfoOV, //获取玩家信息
    
    /********** 新手引导接口 *****************/
    SavePlayerInfo : async (playerName:string,novice:boolean = true) => (await app.api.post(`/game/player/info/save`,{playerName,novice})).data as NewsContext, //保存玩家信息
    SelectNovicePet: async (petId:number) => RData(await app.api.post(`/game/novice/select/${petId}`),false), //选择新手引导宠物  


    /********** 宠物接口 ******************/
    GetPlayerPets: async () => RData(await app.api.get(`/game/pet/list`),false) as PlayerPetOV[],   //获取玩家全部宠物  
    //petId 需合成的Id  pets 被合成的Id列表
    PetUpStar: async (petId:number,consume:number) => RData(await app.api.post(`/game/pet/up/star/${petId}/${consume}`),true) as PlayerPetOV,     //提升宠物星

    /********** 阵法接口 ******************/
    GetPlayerTactical: async () => RData(await app.api.get(`/game/tactical/get`),false) as PlayerTacticalOV, //获取玩家阵法
    SetPlayerTactical: async (data:PlayerTacticalOV) => RData(await app.api.post(`/game/tactical/set`,data),false) as PlayerTacticalOV, //更新玩家阵法

    /********** 资源接口 ******************/
    GetPlayerResource: async () => RData(await app.api.get(`/game/resource/get`),false) as ResourceOV[], //获取玩家资源

    /********** 排行榜接口(无限模式) *******************/
    GOnHookRankings: async (mapId:number) => RData(await app.api.get(`/game/mode/onHook/onRankings/${mapId}`),false) as ModeOnHookRankingOV[], //获取玩家资源

    /********** 宠物装备系统接口 ****************/
    PetEquipAll: async () => RData(await app.api.get(`/game/equip/all`),false) as PetEquip[],  //获取全部装备
    PetEquipForging: async () => RData(await app.api.get(`/game/equip/forging`),true) as PetEquipForgingOV,  //锻造装备
    PetEquipForgingInfo: async () => RData(await app.api.get(`/game/equip/forging/info`),false) as EquipForgingBench,  //锻造台
    PetEquipForgingPetId: async (petId:number) => RData(await app.api.post(`/game/equip/forging/petId/${petId}`),true) as EquipForgingBench,  //设置锻造宠
    PetEquipWear: async (petId:number,equipId:number) => RData(await app.api.post(`/game/equip/wear/${petId}/${equipId}`),true) as PetEquip,  //穿戴装备
    PetEquipForgingUp: async () => RData(await app.api.post(`/game/equip/forging/up`),true) as EquipForgingBench,  //升级锻造台
    PetEquipUseSpeed: async (resType:number) => RData(await app.api.post(`/game/equip/use/speed/${resType}`),true) as EquipForgingBench,  //加速锻造台升级

    /*********** 礼包接口 ******************/
    GetGiftRecord: async () => RData(await app.api.get(`/game/gift/record/all`),false) as GiftRecord[],  //查询当天购买记录
    GetGiftDayRecord: async () => RData(await app.api.get(`/game/gift/record/day`),false) as GiftDayRecord[],  //查询当天购买记录
    BuyGift: async (giftId:number) => RData(await app.api.post(`/game/gift/buy/${giftId}`),true) as Boolean,  //查询当天购买记录

}

