import { _decorator, Component, Node } from 'cc';
import PetEquipData, { PetEquipDataEnum } from '../../../data/PetEquipData';
import { app } from '../../../App';
import { GUI } from '../../UIConfig';
import { API, PlayerPetOV } from '../../../consts/API';
import { PetIconSelectShow } from '../../Consts/Pet/PetIconSelectShow';
import PlayerPetData from '../../../data/PlayerPetData';
import { PetEquipIcon } from '../../Consts/PetEquip/icon/PetEquipIcon';
import { Label } from 'cc';
import TimeUtil from '../../../../../extensions/ngame/assets/ngame/util/TimeUtil';
import { ResourceType } from '../../../data/ResourceData';
import JComponent from '../../../../../extensions/ngame/assets/ngame/util/components/JComponent';
import { Button } from 'cc';
import { EventTouch } from 'cc';
const { ccclass, property } = _decorator;


//锻造台
@ccclass('MainForgingBench')
export class MainForgingBench extends JComponent {


    //锻造等级显示
    @property(Label)
    forgingLevel:Label;

    //锻造宠
    @property(PetIconSelectShow)
    petView:PetIconSelectShow;

    //装备列表
    @property(Node)
    equips:Node;

    onLoad(){

        //监听锻造数据
        app.event.on(PetEquipDataEnum.UPDATE_FORGING_INFO,this.onUpdateView,this);
        app.event.on(PetEquipDataEnum.UPDATE_EQUIP,this.onUpdateEquip,this);

        this.onUpdateView();

    }

    protected onDestroy(): void {
        
        app.event.off(PetEquipDataEnum.UPDATE_FORGING_INFO,this.onUpdateView,this);
        app.event.off(PetEquipDataEnum.UPDATE_EQUIP,this.onUpdateEquip,this);
    }

    updates(){
        if(PetEquipData.getIns().forgingUpTimeExcess){
            this.forgingLevel.string = `(锻造等级: ${PetEquipData.getIns().info.currentForgingQuality} ) ${TimeUtil.remainderString(PetEquipData.getIns().forgingBenchUpEnd)}`;
        }else{
            this.forgingLevel.string = `(锻造等级: ${PetEquipData.getIns().info.currentForgingQuality} )`;
        }
    }

    onUpdateView(){
        this.onUpdateForging();
        this.onUpdateEquip();
    }

    //刷新装备
    onUpdateEquip(){
        this.equips.getComponentsInChildren(PetEquipIcon).forEach((equip,index) => {
            equip.set(PetEquipData.getIns().getPetEquip(index + 1));
        })
    }

    //刷新锻造数据
    onUpdateForging(){
        console.log("刷新锻造数据",PlayerPetData.getIns().petIdQueryPetInfo(PetEquipData.getIns().info.forgingPetId));
        this.petView.set(PlayerPetData.getIns().petIdQueryPetInfo(PetEquipData.getIns().info.forgingPetId));
    }

    //点击锻造
    async onClickForging(){
        if(!PetEquipData.getIns().isForgingBenchPet){
            app.layer.Open(GUI.Tips,{text:"请选择锻造宠"});
            return;
        }
        let equip = await PetEquipData.getIns().forging();
        equip && app.layer.Open(GUI.PetEquipForgingPopupView,equip);
    }

    //点击锻造宠
    async onClickForgingPet(){
        let info:PlayerPetOV = await (new Promise(resolve => {
            app.layer.Open(GUI.PetRadioView,{resolve});
        }))
        if(!info) return;

        //设置锻造宠
        await PetEquipData.getIns().setForgingPetId(info.petId);

    }

    //点击升级锻造台
    async onClickUpForgingBench(){
        if(PetEquipData.getIns().forgingUpTimeExcess)
            await PetEquipData.getIns().useSpeedUpForgingBench(ResourceType.SpeedUp);
        else
            await PetEquipData.getIns().upForgingBench();
    }

    //打开指定装备
    onClickEquip(event: EventTouch){

        let equip = PetEquipData.getIns().getPetEquip(event.target.getSiblingIndex() + 1);

        if(!equip){
            app.layer.Open(GUI.Tips,{text:"快锻造装备吧~"});
            return;
        }

        app.layer.Open(GUI.PetEquipForgingPopupView,equip)

    }

}


