import { _decorator, Component, Node } from 'cc';
import JNLayerBase from '../../../../extensions/ngame/assets/ngame/ui/base/JNLayerBase';
import JNScrollView from '../../../../extensions/ngame/assets/ngame/util/components/scrollview/JNScrollView';
import PlayerPetData, { PlayerPetEvent } from '../../data/PlayerPetData';
import { API, PlayerPetOV } from '../../consts/API';
import { PetOV, PlayerPetOVSelect } from '../VO/PetOV';
import { Label } from 'cc';
import { app, TD } from '../../App';
import JProgressBar from '../../../../extensions/ngame/assets/ngame/util/components/Progress/JProgressBar';
import { Slider } from 'cc';
import ResourceData, { ResourceType } from '../../data/ResourceData';
import { GUI } from '../UIConfig';
import { PetPreviewWindow } from '../Consts/Pet/info/PetPreviewWindow';
const { ccclass, property } = _decorator;

@ccclass('PetUpStarView')
export class PetUpStarView extends JNLayerBase {

    @property(Label)
    petNameLabel:Label; //宠物名称
    @property(Label)
    petStar:Label; //宠物星级
    @property(Label)
    petStarExp:Label; //宠物星级经验
    @property(JProgressBar)
    petExpProgress:JProgressBar; //宠物经验条
    @property(JProgressBar)
    viewPetExpProgress:JProgressBar; //预览宠物经验条

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

    @property(Slider)
    slider:Slider; //滑块

    @property(Label)
    qsLabel:Label;      //魂数量

    //消耗数量
    _consume:number = 0;
    get consume(){
        return Math.min(ResourceData.getIns().getValue(ResourceType.Q1S),this._consume);
    }
    set consume(value:number){
        let max = ResourceData.getIns().getValue(ResourceType.Q1S);
        this._consume = value;
        //修正
        this.slider.progress = this.consume / max;
        this.qsLabel.string = `${ResourceData.getIns().getValue(ResourceType.Q1S) - this.consume}`
    }

    data:PlayerPetOV;
    
    onJNLoad(data: PlayerPetOV): void {

        super.onJNLoad(data);
        this.data = data;
        this.onUpdateView();

        //监听
        app.event.on(PlayerPetEvent.UPDATE_INFO,this.onUpdateInfo,this);    //刷新宠物信息

    }

    onJNClose(): void {
        //取消监听
        app.event.off(PlayerPetEvent.UPDATE_INFO,this.onUpdateInfo,this);    //刷新宠物信息
    }


    //刷新页面
    onUpdateView(){
        //显示选中宠物
        this.petPreview.bind(this.data);
        this.onUpdateInfo();
    }

    //刷新信息
    onUpdateInfo(){

        //显示宠物名称
        this.petNameLabel.string = TD.TbGRole.get(this.data.petTbId).roleName;
        this.petStar.string = `当前星级: ${this.data.petStar} 星`;
        
        let upStarExp = TD.TbGRoleUpStar.get(this.data.petStar + 1).merge;
        this.petStarExp.string = `升级进度: ${upStarExp} / ${this.data.petStarExp}`;
        this.petExpProgress.value = this.data.petStar + (this.data.petStarExp / upStarExp);

        this.qsLabel.string = `${ResourceData.getIns().getValue(ResourceType.Q1S) - this.consume}`

    }

    //更新预览进度条
    onUpdatePreview(){

        let upStarExp;
        let petStarExp = this.data.petStarExp + this.consume;
        let petStar = this.data.petStar;

        while(petStarExp >= (upStarExp = TD.TbGRoleUpStar.get(petStar + 1).merge)){
            petStar++;
            petStarExp = Math.floor(petStarExp - upStarExp);
        }
        
        this.petStar.string = `当前星级: ${petStar} 星`;
        this.petStarExp.string = `升级进度: ${upStarExp} / ${petStarExp}`;
        //预览进度
        this.viewPetExpProgress.value = petStar + (petStarExp / upStarExp);
        this.qsLabel.string = `${ResourceData.getIns().getValue(ResourceType.Q1S) - this.consume}`

    }

    //滑动滑块
    onSliderMove(slider: Slider){

        let max = ResourceData.getIns().getValue(ResourceType.Q1S);
        if(!max){
            this.consume = 0;
            return;
        }
        this.consume = Math.ceil(max * slider.progress);

        this.onUpdatePreview();

    }

    //选择全部
    onClickAll(){

    }

    //点击合成
    async onClickUp(){

        if(!this.consume){
            app.layer.Open(GUI.Tips,{text:"拖拽进行升星吧~"})
            return;
        }

        await API.PetUpStar(this.data.petId,this.consume);
        this.consume = 0;

    }
    
}


