import TimeUtil from "../../../extensions/ngame/assets/ngame/util/TimeUtil";
import { TD, app } from "../App";
import { API, EquipForgingBench, PetEquip } from "../consts/API";
import { GUI } from "../ui/UIConfig";
import BaseData from "./BaseData";
import { GBattleDataEnum } from "./GBattleData";
import GOnHookData from "./GOnHookData";
import PlayerPetData from "./PlayerPetData";

export enum PetEquipDataEnum{
    //更新锻造数据
    UPDATE_FORGING_INFO = "PetEquipDataEnum_UPDATE_FORGING_INFO",
    //更新装备
    UPDATE_EQUIP = "PetEquipDataEnum_UPDATE_EQUIP"
}

//宠物装备数据类
export default class PetEquipData extends BaseData{

    //装备
    equips:PetEquip[];
    //锻造台
    info:EquipForgingBench;

    //锻造升级结束时间
    forgingBenchUpEnd:number;
    //获取升级剩余时间
    get forgingUpTimeExcess(){
        if(!this.forgingBenchUpEnd) return 0;
        return TimeUtil.remainder(this.forgingBenchUpEnd)
    }

    //锻造台是否有宠物
    get isForgingBenchPet(){
        return !!PlayerPetData.getIns().petIdQueryPetInfo(this.info.forgingPetId);
    }

    async onInit() {
        this.onUpdateEquips(await API.PetEquipAll());
        this.onUpdateInfo(await API.PetEquipForgingInfo());
    }

    //更新装备
    onUpdateEquips(equips:PetEquip[]){
        this.equips = equips;

        app.event.emit(GBattleDataEnum.UPDARE_ATTRIBUTE); //刷新属性
    }

    //更新锻造数据
    onUpdateInfo(info:EquipForgingBench){
        if(!info) return;
        this.info = info;
        this.forgingBenchUpEnd = Date.now() + this.info.forgingUpTimeExcess;
        console.log("this.forgingBenchUpEnd",this.forgingBenchUpEnd);
        app.event.emit(PetEquipDataEnum.UPDATE_FORGING_INFO);
    }

    //锻造
    async forging(){
        let info = await API.PetEquipForging();
        if(info){
            this.onUpdateInfo(info.info);
            return this.addEquip(info.equip);
        }else return null;
    }

    //添加装备
    addEquip(equip:PetEquip):PetEquip{
        if(!equip) return null;
        console.log("添加装备",equip)
        if(this.equips.filter(item => item.equipId == equip.equipId).length) return null;
        this.equips.push(equip);
        return equip;
    }

    //设置锻造宠
    async setForgingPetId(petId:number){
        this.onUpdateInfo(await API.PetEquipForgingPetId(petId));
    }

    //穿戴指定装备
    async wear(equip:PetEquip){
        if(this.equips.indexOf(equip) < 0) {
            app.layer.Open(GUI.Tips,{text:"装备不存在"});
            return;
        }
        let info = await API.PetEquipWear(this.info.forgingPetId,equip.equipId);
        if(info){
            //脱下之前的装备
            this.equips.forEach(item => {
                if(info.equipPetId == item.equipPetId && item.equipPosition == info.equipPosition){
                    item.equipPetId = 0;
                }
            })
            Object.assign(equip,info);
            app.event.emit(PetEquipDataEnum.UPDATE_EQUIP);

            app.event.emit(GBattleDataEnum.UPDARE_ATTRIBUTE); //刷新属性
        }
    }

    //获取指定宠物指定位置的装备
    getPetEquip(position:number,petId:number = this.info.forgingPetId){
        if(!petId) return null;
        return this.equips.filter(equip => (equip.equipPetId == petId && equip.equipPosition == position))[0];
    }

    //升级锻造台
    async upForgingBench(){
        this.onUpdateInfo(await API.PetEquipForgingUp());
    }

    //加速升级锻造台
    async useSpeedUpForgingBench(resType: number){
        this.onUpdateInfo(await API.PetEquipUseSpeed(resType));
    }

    //获取指定宠物等级
    getForgingBenchPetLevel(petId:number = this.info.forgingPetId){
        let exp = this.info.forgingPetLevels[`${petId}`] || 0;
        let list = TD.TbGRoleEquipLevel.getDataList().filter(info => (exp-=info.exp) >= 0);
        return list[list.length - 1].id;
    }


}