import { app } from "../App";
import { API, PlayerInfoOV, PlayerPetOV } from "../consts/API";
import BaseData from "./BaseData";
import { GBattleDataEnum } from "./GBattleData";
import GOnHookData from "./GOnHookData";
import PetEquipData, { PetEquipDataEnum } from "./PetEquipData";
import PlayerTacticalData from "./PlayerTacticalData";
import { ResourceUpdateType } from "./ResourceData";

export enum PlayerPetEvent{
    UPDATE = "PlayerPetEvent_UPDATE",               //刷新宠物
    UPDATE_INFO = "PlayerPetEvent_UPDATE_INFO",     //刷新宠物信息
    UPDATE_ADD = "PlayerPetEvent_UPDATE_ADD",      //添加宠物
    UPDATE_MINUS = "PlayerPetEvent_UPDATE_MINUS",      //减少宠物
}

//玩家宠物数据
export default class PlayerPetData extends BaseData{

    //玩家宠物列表
    datas:PlayerPetOV[] = [];

    async onInit() {

        //更新玩家宠物
        await this.UpdatePlayerPet();
        
        app.event.on(PetEquipDataEnum.UPDATE_FORGING_INFO,this.onUpdateForgingInfo,this);

    }

    //更新锻造等级 (宠物等级)
    onUpdateForgingInfo(){

        this.datas.forEach(item => item.petLevel = PetEquipData.getIns().getForgingBenchPetLevel(item.petId));
        app.event.emit(PlayerPetEvent.UPDATE)

    }

    //获取全部宠物
    getData():PlayerPetOV[]{
        return this.datas;
    }

    //获取指定宠物
    getPetIdData(petTbId:number,datas:PlayerPetOV[] = this.datas):PlayerPetOV[]{
        return datas.filter(data => data.petTbId == petTbId);
    }

    //获取未上阵的宠物
    getNoTacticalData():PlayerPetOV[]{
        return this.datas.filter(data => PlayerTacticalData.getIns().getTacticalInfo().indexOf(data.petId) < 0);
    }

    //获取上阵的宠物
    getTacticalData():PlayerPetOV[]{
        return this.datas.filter(data => PlayerTacticalData.getIns().getTacticalInfo().indexOf(data.petId) >= 0);
    }

    //更新玩家宠物
    async UpdatePlayerPet(){
        //获取全部宠物
        let datas = await API.GetPlayerPets();

        datas.forEach(item => item.petLevel = PetEquipData.getIns().getForgingBenchPetLevel(item.petId));
        this.datas = datas;
        
        app.event.emit(GBattleDataEnum.UPDARE_ATTRIBUTE); //刷新属性
    }

    //选择宠物
    async SelectNovicePet(petId:number){
        await API.SelectNovicePet(petId);
        //更新玩家宠物列表
        await this.UpdatePlayerPet();
    }

    //通过宠物Id找到宠物
    petIdQueryPetInfo(petId:number):PlayerPetOV{
        return this.datas.filter(item => item.petId == petId)[0]
    }

    //添加宠物
    addPet(pet:PlayerPetOV){
        pet.petLevel = PetEquipData.getIns().getForgingBenchPetLevel(pet.petId);
        this.datas.push(pet);
    }

    //刷新返回宠物
    onUpdateOV(operation:number,resource:PlayerPetOV){


        //如果没有刷新的宠物 则 返回
        if(!resource) return;
        
        resource.petLevel = PetEquipData.getIns().getForgingBenchPetLevel(resource.petId);

        if(operation == ResourceUpdateType.UPDATE){
            //更新资源
            //找到需要被刷新的宠物 然后刷新 找不到则添加
            let pet1 = this.datas.filter(pet => pet.petId == resource.petId)[0];
            if(pet1){
                //更新数据
                Object.assign(pet1,resource);
                app.event.emit(PlayerPetEvent.UPDATE_INFO,pet1)
            }else{
                operation = ResourceUpdateType.ADD;
                this.onUpdateOV(operation,resource);
            }
        }else if(operation == ResourceUpdateType.ADD){
            //查询如果没有这个宠物则添加 负责 刷新
            let pet1 = this.datas.filter(pet => pet.petId == resource.petId)[0];
            if(!pet1){
                //没有则添加
                this.datas.push(resource);
                app.event.emit(PlayerPetEvent.UPDATE)
                app.event.emit(PlayerPetEvent.UPDATE_ADD,resource)
            }else{
                operation = ResourceUpdateType.UPDATE;
                this.onUpdateOV(operation,resource);
            }
        }else if(operation == ResourceUpdateType.MINUS){

            //删除的宠物
            let delPet;
            //删除指定的宠物
            this.datas = this.datas.filter(pet => {
                if(pet.petId == resource.petId){
                    delPet = pet;
                    return false;
                }
                return true;
            });
            app.event.emit(PlayerPetEvent.UPDATE)
            delPet && app.event.emit(PlayerPetEvent.UPDATE_MINUS,delPet)

        }

    }

}


