import i18n from "../../../i18n/i18n";
import ViewObject from "../../../main/ViewObject";
import { __StageData } from "../../data/sdata/SManage";
import FF from "../FF";

/**
 * 退出box
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FExitBox extends ViewObject {

    @property(cc.Label)
    mAbout: cc.Label = null;

    @property(cc.Sprite)
    mGoodIcon1: cc.Sprite = null;

    @property(cc.Label)
    mGood1: cc.Label = null;

    @property(cc.Sprite)
    mGoodIcon2: cc.Sprite = null;

    @property(cc.Label)
    mGood2: cc.Label = null;
    
    @property(cc.Node)
    mStar: cc.Node = null;//星星节点

    @property([cc.SpriteFrame])
    mStarx: Array<cc.SpriteFrame> = [];

    public endType = 0;//0,通关后退出,1是半路退出

    public ff:FF
    public stageData:__StageData
    public callbackOK:()=>void;
    public callbackClose:()=>void;
   
    public setOKCallback(callbackOK:()=>void){
        this.callbackOK = callbackOK;
    }
    public setCloseCallback(callbackClose:()=>void){
        this.callbackClose = callbackClose;
    }

    public onclickOK(){
        this.__distroyAll();
        this.callbackOK();
        cc.director.getPhysicsManager().enabled = true;
    }
    public onclickCancel(){
        this.__distroyAll();
        if(this.callbackClose){
            this.callbackClose()
        }
    }
    public init(__stageData:__StageData){
        this.stageData = __stageData
        let sManage = this.main.sManage;
        let good1 = sManage.getGoodById1(this.stageData.goodId1);
        cc.resources.load('icon/good/'+good1.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mGoodIcon1.spriteFrame = spriteFrame;
            }
        } );

        let good2 = sManage.getGoodById1(this.stageData.goodId2);
        cc.resources.load('icon/good/'+good2.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mGoodIcon2.spriteFrame = spriteFrame;
            }
        } );
        
        this.flush();

        let starCount = this.getStarCount()
        let nodes = this.mStar.children;
        for (let i = 0; i < nodes.length; i++) {
            let node = nodes[i];
            let sprite = node.getComponent(cc.Sprite);
            if(i < starCount){
                sprite.spriteFrame = this.mStarx[1];
            }else{
                sprite.spriteFrame = this.mStarx[0];
            }
        }

        let starCount1 = 0
        let main = this.main;
        let stageData = main.player.stage;
        let stageAttr = stageData.data[this.stageData.id];
        
        if(stageAttr != null){
            let count1 = this.getCount(this.stageData.goodId1);
            if(count1 >= this.stageData.goodCount1){
                starCount1 ++
            }
            let count2 = this.getCount(this.stageData.goodId2);
            if(count2 >= this.stageData.goodCount2){
                starCount1 ++
            }
        }

        if(starCount1 >= 2){
            this.mAbout.string = i18n.t('关卡内任务已完成')
        }else{
            this.mAbout.string = i18n.t('关卡内还有未完成的任务，是否确定退出？')
        }
    }

    /**
     * 获取当前星星数量
     */
    private getStarCount(){
        let main = this.main;
        let stageData = main.player.stage;
        let stageAttr = stageData.data[this.stageData.id];
        let starCount = 0;//星星的数量
        if(this.endType == 0){//通关后退出
            starCount ++
        }else{//半路退出
            if(stageData.stageIndex > this.stageData.id){
                starCount ++
            }
        }
        if(stageAttr != null){
            let count1 = this.getCount(this.stageData.goodId1);
            if(count1 >= this.stageData.goodCount1){
                starCount ++
            }
            let count2 = this.getCount(this.stageData.goodId2);
            if(count2 >= this.stageData.goodCount2){
                starCount ++
            }
        }
        return starCount
    }

    public flush(){
        let count1 = this.getCount(this.stageData.goodId1);
        this.mGood1.string = count1+'/'+this.stageData.goodCount1;

        let count2 = this.getCount(this.stageData.goodId2);
        this.mGood2.string = count2+'/'+this.stageData.goodCount2;

    }
    public getCount(goodId){
        let player = this.main.player;
        let stage = player.stage;
        let curr = stage.data[this.stageData.id];
        if(curr && curr.good){
            let count = curr.good[''+goodId];
            if(count == undefined){
                return 0 ;
            }
            return count;
        }else{
            return 0;
        }
    }
}
