import i18n from "../../i18n/i18n";
import Main from "../../main/Main";
import { __StageData } from "../data/sdata/SManage";
import FF from "./FF";
import FTmpTaskGood from "./FTmpTaskGood";
import FTmpTaskPanel from "./FTmpTaskPanel";

/**
 * 战斗中显示收集的物品
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FFHeader extends cc.Component {

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

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

    @property(cc.Prefab)
    mTmpGoodItem: cc.Prefab = null;

    @property(FTmpTaskPanel)
    mTmpPanel: FTmpTaskPanel = null;//点击后显示的物品属性

    private tmpGoods:Map<number,number> = new Map();

    
    /**
     * 当前打的关卡
     */
    public stageData:__StageData;

    private main:Main;
    private ff:FF;
    public onLoad(){
        this.mTmpPanel.node.active = false
    }
   
    public init(ff:FF,stageData:__StageData){
        this.ff = ff;
        this.main = this.ff.main;
        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;
            }
        } );

        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();
    }
    public flush(){
        // cc.log('this.stageData : ',this.stageData)
        let player = this.main.player;
        let stage = player.stage;
        // cc.log('stage:',stage)
        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 addTmpGood(id,count){
        let xx = this.tmpGoods.get(id);
        if(this.tmpGoods.has(id)){
            xx += count;
            this.tmpGoods.set(id,xx);
        }else{
            this.tmpGoods.set(id,count);
        }
        this.flushTmpGood();

        let _good = this.main.sManage.getGoodById(id)
        //<color=#FFFFFF>获得道具</c><color=#0fffff>服饰卡*1</color>
        let str = '<color=#FFFFFF>'+i18n.t('获得道具')+':</c><color=#0fffff>'+i18n.t(_good.name)+'x'+count+'</c>'

        this.main.showTips(str)
    }
    public removeTmpGood(id,count){
        if(this.tmpGoods.has(id)){
            let xx = this.tmpGoods.get(id);
            xx -= count;
            if(xx > 0){
                this.tmpGoods.set(id,xx);
            }else{
                this.tmpGoods.delete(id);
            }
            this.flushTmpGood();
        }
    }
    public getTmpCount(id){
        if(this.tmpGoods.has(id)){
            return this.tmpGoods.get(id);
        }
        return 0;
    }

    private flushTmpGood(){
        this.mTmpGoods.active = this.tmpGoods.size > 0
        let manage = this.ff.main.sManage;
        this.mTmpContent.destroyAllChildren()
        this.tmpGoods.forEach((count, id)=> {
            let good = manage.getGoodById1(id);
            let node:cc.Node = cc.instantiate(this.mTmpGoodItem)
            node.parent = this.mTmpContent
            let tmpTaskGood = node.getComponent(FTmpTaskGood)
            tmpTaskGood.init(good,count)
            tmpTaskGood.setCallback((goodItem:FTmpTaskGood,isActive)=>{
                if(isActive){
                    this.mTmpPanel.show(goodItem)
                }else{
                    this.mTmpPanel.node.active = false
                }
                
            })
        })
    }

}
