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

const {ccclass, property} = cc._decorator;

@ccclass
export default class StageInfo extends ViewObject {

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

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

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

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

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

    @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.Sprite)
    mGoodIcon3: cc.Sprite = null;

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

    @property(cc.Node)
    mTask: cc.Node = null;//任务节点

    @property(cc.Node)
    mStartNode: cc.Node = null;//开始按钮

    @property(cc.Node)
    mGuideLayout: cc.Node = null;//引导层


    public inGuide = 0;//是否需要引导  = 2的时候需要引导

    public stageData:__StageData;

    /**
     * 
     * @param prev 父界面
     */
     public show(prev?:ViewObject){
        if(prev){
            this.prev = prev;
            this.prev.__close();
        }
        this.main.viewManage.popView1(this.node);
        if(this.main && this.main.gameHttp){
            this.main.gameHttp.pushEvent(this);
        }
    }


    onLoad(){
        let sceneData = this.main.sManage.getSceneById(this.stageData.zid);
        this.mSceneName.string = i18n.t(sceneData.name);
        this.mStageName.string = i18n.t(this.stageData.name);
        this.mStageAbout.string = i18n.t(this.stageData.about)
        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;
            }
        } );

        let good3 = sManage.getGoodById1(this.stageData.goodId3);
        cc.resources.load('icon/good/'+good3.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mGoodIcon3.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];
            }
            
        }

        if(this.inGuide == 2){
            this.openGuide1()
        }
    }
    private tempIndex = 1;
    public openGuide1(){
        this.mGuideLayout.active = true;
        let nodes = this.mGuideLayout.children;
        for (let i = 2; i < nodes.length; i++) {
            const element = nodes[i];
            element.active = false;
        }
    }
    /**
     * 打开引导
     */
    public openGuide2(){
        let main = this.main
        let guideMask = main.mGuideMask
        guideMask.setTargetNode(this.mStartNode)
        guideMask.show()

        this.setGuideCallback(()=>{
            guideMask.close()
        })
    }
 /**
     * 获取当前星星数量
     */
    private getStarCount(){
        let main = this.main;
        let stageData = main.player.stage;
        let stageAttr = stageData.data[this.stageData.id];
        let starCount = 0;//星星的数量
        if(stageData.stageIndex > this.stageData.id-1){
            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;

        let count3 = this.getCount(this.stageData.goodId3);
        this.mGood3.string = count3+'/'+this.stageData.goodCount3;
    }
    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;
        }
    }
    public callback:(info:StageInfo)=>void;
    public guideCallback:()=>void;

    public setCallback(callback:(info:StageInfo)=>void){
        this.callback = callback;
    }
    public setGuideCallback(callback:()=>void){
        this.guideCallback = callback;
    }
    public onclick(){
        this.exitDistroy();
        if(this.callback){
            this.callback(this);
        }
        if(this.guideCallback){
            this.guideCallback();
        }
    }
    /**
     * 点击伙伴
     */
    public onclickPet(){
        this.main.viewManage.loadFunc(GameViewType.equip,(viewObject:ViewObject)=>{
            viewObject.show(this);
        });
    }
    /**
     * 关卡收集的道具
     */
    public getGoods(){
        let goods = [
            {
                id:this.stageData.goodId1,
                count:this.stageData.goodCount1,
            },
            {
                id:this.stageData.goodId2,
                count:this.stageData.goodCount2,
            },
            {
                id:this.stageData.goodId3,
                count:this.stageData.goodCount3,
            },
        ];
        return goods;
    }


    public onclickNextGuide(){
        this.tempIndex ++;
        let nodes = this.mGuideLayout.children
        if(this.tempIndex >= nodes.length){
            this.openGuide2()
            this.mGuideLayout.active = false
            return
        }
        nodes[this.tempIndex - 1].active = false
        nodes[this.tempIndex].active = true

    }


}
