import { _decorator, Component, Node } from 'cc';
import JNLayerBase from '../../../../extensions/ngame/assets/ngame/ui/base/JNLayerBase';
import { Prefab } from 'cc';
import PlayerPetData, { PlayerPetEvent } from '../../data/PlayerPetData';
import JNScrollView from '../../../../extensions/ngame/assets/ngame/util/components/scrollview/JNScrollView';
import { NodeEventType } from 'cc';
import { sp } from 'cc';
import { app } from '../../App';
import { API, PlayerPetOV } from '../../consts/API';
import { UIPetAnim } from '../../consts/GData';
import { GUI } from '../UIConfig';
import PlayerTacticalData from '../../data/PlayerTacticalData';
import { PetIconSelectScroll } from '../Consts/Pet/PetIconSelectScroll';
import { PetPreviewWindow } from '../Consts/Pet/info/PetPreviewWindow';
import { Label } from 'cc';
const { ccclass, property } = _decorator;

export enum IntoBattleState{
    请选择 = "请选择",下阵 = "下阵",替阵 = "替阵",上阵 = "上阵"
}

//上阵页面
@ccclass('IntoBattleView')
export class IntoBattleView extends JNLayerBase {


    @property(JNScrollView)
    views:JNScrollView; //宠物列表

    @property(PetPreviewWindow)
    petPreview:PetPreviewWindow; //宠物显示窗口

    @property(Label)
    tacticalLabel:Label;//上阵文本

    //宠物数据
    pets:PlayerPetOV[] = [];

    //当前选中
    index:number = -1;

    tIndex:number = -1; //阵法下标

    onJNLoad(data: {index}): void {

        super.onJNLoad();

        //获取传入的下标
        this.tIndex = data.index;

        console.log("你选择的是",this.tIndex);

        app.event.on(PlayerPetEvent.UPDATE_MINUS,this.onPetMinus,this);    //减少宠物

        this.onUpdateView();
        
    }
    
    onJNClose(): void {
        super.onJNClose();
        //取消监听
        app.event.off(PlayerPetEvent.UPDATE_INFO,this.onPetMinus,this);    //减少宠物
    }

    onPetMinus(pet:PlayerPetOV){
        this.views.getItems<PetIconSelectScroll>().forEach(item => {
            if(item.data.petId == pet.petId)
                this.views.delData(item.data);
        });
    }

    //刷新页面
    onUpdateView(){

        //获取所有玩家宠物
        this.pets = PlayerPetData.getIns().getData();

        //获取当前阵法下标是否存在宠物
        let petId = PlayerTacticalData.getIns().getItem(this.tIndex);
        if(petId){
            //如果存在则默认选中
            this.index = this.pets.indexOf(PlayerPetData.getIns().petIdQueryPetInfo(petId));
        }

        this.views.refreshData(this.pets);

        //设置不可选中
        this.views.getItems<PetIconSelectScroll>().forEach(item => {
            if(PlayerTacticalData.getIns().getTacticalInfo().indexOf(item.data.petId) != -1)
                item.select.isNoSelect = true; //如果在阵法里则不可选中
        })

        //向子节点添加点击事件
        this.views.addItemEvent(NodeEventType.TOUCH_START,this.onClickItem.bind(this));

        this.onUpdateSelect();

    }

    //刷新选中
    onUpdateSelect(){

        //默认都不选中
        this.views.getItems<PetIconSelectScroll>().forEach(item => {
            item.select.isSelect = false;
        })

        //设置选中
        if(this.index != -1){
            let current = this.views.getItems<PetIconSelectScroll>()[this.index]
            current.select.isSelect = true;

            //显示选中宠物
            this.petPreview.bind(this.pets[this.index]);
        }

        this.onUpdateTactical();
    }

    //刷新上阵
    onUpdateTactical(){

        //选择的宠物
        this.tacticalLabel.string = `${this.getIntoBattleState()}`;

    }

    //获取当前状态
    getIntoBattleState():IntoBattleState{

        //选择的宠物
        let selectPet = this.pets[this.index];
        if(this.index == -1 || !selectPet) {
            return IntoBattleState.请选择
        }

        //判断选中的宠物是否存在阵法里 存在则下阵
        let selectIndex = PlayerTacticalData.getIns().getItemIndex(selectPet.petId);
        if(selectIndex >= 0){
            
            //存在 如果存在的下标是当前下标则下阵 不是 则替换
            if(selectIndex == this.tIndex)
                return IntoBattleState.下阵
            else return IntoBattleState.替阵;
            
        }else{
            //不存在则上阵
            return IntoBattleState.上阵;
        }

    }

    //点击Item
    onClickItem(index:number){
        
        //设置当前选中
        this.index = index;
        //刷新
        this.onUpdateSelect();

    }

    //点击上阵
    async onClickTactical(){

        let state = this.getIntoBattleState();

        if(state == IntoBattleState.请选择){
            app.layer.Open(GUI.Tips,{text:"请选择要上阵的宠物."})
            return;
        }else if(state == IntoBattleState.上阵 || state == IntoBattleState.替阵){
            //修改上阵信息
            await PlayerTacticalData.getIns().UpdateIndexTactical(this.tIndex,this.pets[this.index].petId);
            app.layer.Open(GUI.Tips,{text:"上阵成功"});
        }else if(state == IntoBattleState.下阵){
            //修改上阵信息
            await PlayerTacticalData.getIns().UpdateIndexTactical(this.tIndex,0);
            app.layer.Open(GUI.Tips,{text:"下阵成功"});
        }

        //上阵完 关闭页面
        app.layer.CloseNode(this.node);

    }

    //点击升星页面
    async onClickUpStar(){

        if(this.index < 0){
            app.layer.Open(GUI.Tips,{text:"请选择宠物."})
            return;
        }
        app.layer.Open(GUI.PetUpStarView,this.pets[this.index]);

    }

}


