import { __StageData } from "../../../../data/sdata/SManage";
import { StageGoodAttr } from "../../../../data/udata/Player";
import Stage from "../../Stage";

/**
 * 地图小图标节点
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class MiniMapItem extends cc.Component {

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

    @property(cc.Node)
    mStarNode: cc.Node = null;

    @property([cc.SpriteFrame])
    mStarx: Array<cc.SpriteFrame> = [];
  
    @property(cc.Label)
    mId: cc.Label = null;//序号

    public stage:Stage;
    public _stageData:__StageData;
  
    public callback:(item:MiniMapItem)=>void;

    public starCount = 0;

    onLoad(){
        if(!this.mStarNode){
            this.mStarNode = this.node.children[0].children[0]
        }
        this.mId.string = ''+this._stageData.id;

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

    public setCallback(callback:(item:MiniMapItem)=>void){
        this.callback = callback;
    }
    public onclick(){
        let main = this.stage.main;
        let stageData = main.player.stage;
        if(stageData.stageIndex >= this._stageData.id - 1){
            this.callback(this);
        }else{
            // this.callback(this);
            main.showTips('关卡还未开启')
        }
        
    }
    public getStarCount(){
        let main = this.stage.main;
        let stageData = main.player.stage;
        let stageGoodAttr:StageGoodAttr = stageData.data[this._stageData.id];
        if(stageGoodAttr){
            return stageGoodAttr.starCount
        }
        return 0;
    }
    /**
     * 获取当前星星数量
     */
    public getStarCount1(){
        let main = this.stage.main;
        let stageData = main.player.stage;
        let stageGoodAttr:StageGoodAttr = stageData.data[this._stageData.id];
        let starCount = 0
        if(stageData.stageIndex > this._stageData.id - 1){
            starCount ++
        }
        if(stageGoodAttr != 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 getCount(goodId){
        let main = this.stage.main
        let player = main.player;
        let stage = player.stage;
        let curr:StageGoodAttr = stage.data[this._stageData.id];
        if(curr && curr.good){
            let count = curr.good[''+goodId];
            if(count == undefined){
                return 0 ;
            }
            return count;
        }else{
            return 0;
        }
    }
   
}
