import { _decorator, Component, Node } from 'cc';
import PlayerTacticalData from '../../../data/PlayerTacticalData';
import { app } from '../../../App';
import { GUI } from '../../UIConfig';
import { sp } from 'cc';
import { UIPetAnim } from '../../../consts/GData';
import PlayerPetData from '../../../data/PlayerPetData';
import { Vec3 } from 'cc';
import { NodeEventType } from 'cc';
import { EventTouch } from 'cc';
import JNodeDrag from '../../../../../extensions/ngame/assets/ngame/util/components/JNodeDrag';
import { PetIcon } from '../Pet/PetIcon';
const { ccclass, property } = _decorator;

@ccclass('PlayerTacticalItem')
export class PlayerTacticalItem extends Component {

    //阵法的Index;
    index:number;

    //没有宠物的节点
    @property(Node)
    noPet:Node;
    
    //有宠物的节点
    @property(Node)
    havePet:Node;

    //宠物节点
    @property(PetIcon)
    petIcon:PetIcon;

    //拖拽
    @property(JNodeDrag)
    drag:JNodeDrag;

    //当前上阵的宠物
    petId:number;

    //初始位置
    initPos:Vec3;
    
    //初始化阵法
    onInit(index:number){
        this.index = index;
        this.initPos = this.node.position;
    }
    
    protected start(): void {
        this.onUpdateView();
    }
    
    //更新信息
    onUpdateView(){
        
        //获取阵法下的宠物
        this.petId = PlayerTacticalData.getIns().getItem(this.index);

        //如果为0则没有宠物
        if(this.petId){
            this.havePet.active = true;
            this.noPet.active = false;
            this.onUpdatePetView();
        }else{
            this.noPet.active = true;
            this.havePet.active = false;
        }

    }

    //更新宠物信息
    onUpdatePetView(){

        //获取宠物信息
        let info = PlayerPetData.getIns().petIdQueryPetInfo(this.petId);
        this.petIcon.set(info);
        
    }
    
    //打开选择阵法宠物
    onClick(){

        //如果拖拽了则不生效点击事件
        if(this.drag.isMove) return;

        // //如果没有宠物则弹出选择宠物 负责 删除宠物
        // if(this.petId){
        //     //移除宠物
        //     //提示是否移除宠物
        //     app.layer.Open(GUI.SelectionBox,{
        //         tigText:"是否移除宠物?",
        //         cancel:()=>{},
        //         confirm:async ()=>{
        //             //移除宠物 (0就是移除)
        //             await PlayerTacticalData.getIns().UpdateIndexTactical(this.index,0);
        //         }
        //     })
        // }else{
        // }

        //选择宠物
        app.layer.Open(GUI.IntoBattleView,{
            index:this.index,  //当前选择的阵法下标
        });
        
    }

    //交换阵法
    onExchange(item:PlayerTacticalItem){
        let rootIndex = this.index;
        this.index = item.index;
        item.index = rootIndex;

        //更新拖拽位置
        let rootOriginal = this.drag.original;
        this.drag.onUpdateOriginal(item.drag.original);
        item.drag.onUpdateOriginal(rootOriginal);
    }

    addMoveEvent(fun:Function){
        this.drag.addMoveEvent((e) => {
            fun(this,e);
        });
    }

    addMoveEndEvent(fun:Function){
        this.drag.addMoveEndEvent((e) => {
            fun(this,e);
        });
    }
    
}


