import Main from "../../main/Main";
import FFCalAttr from "../data/FFCalAttr";
import { __EquipData, __GoodData } from "../data/sdata/SManage";
import { EquipAttr } from "../data/udata/Player";

const {ccclass, property} = cc._decorator;
/**
 * 道具/装备节点
 */
@ccclass
export default class GoodItem extends cc.Component {

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

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

    @property(cc.SpriteFrame)
    mNullIcon: cc.SpriteFrame = null;

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

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

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

    @property(cc.SpriteFrame)
    mStar: cc.SpriteFrame = null;

    @property(cc.Label)
    mLevel: cc.Label = null;//强化等级/道具数量

    @property(cc.Label)
    mContrast: cc.Label = null;//数量对比

    public main:Main;
    public equip:EquipAttr;
    public equipData:__EquipData;

    /**
     * {
     *      icon:"good/1001",
     *      count:1
     * }
     */
    public data:any;
    public good:__GoodData;
    /**
    {
        type:3,    ---0:金币,1:钻石,2:道具,3:装备
        icon:'icon/equip/'+equip.icon,
        count:element.count,
        id:element.id,
        pz:equip.pz,
        PI:element.PI,
        star:element.star,
        PR:1
     }
     * @param rewardData 
     */
    public initReward(main:Main,rewardData:any){
        this.mNullNode.active = true;
        cc.resources.load(rewardData.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mIcon.spriteFrame = spriteFrame;
            }
        } );
        this.mBG.spriteFrame = this.mBGs[rewardData.pz-1];
        if(rewardData.type < 3){
            this.mLevel.string = 'x'+rewardData.count;
        }else{
            let qhLevel = FFCalAttr.getEquipQHLevel(main,rewardData.PI,rewardData._data)
            if(qhLevel > 0){
                this.mLevel.string = 'Lv.'+qhLevel;
            }else{
                this.mLevel.string = ''
            }
            this.mStarNode.destroyAllChildren()
            for (let i = 0; i < rewardData.star; i++) {
                let node = new cc.Node()
                let sprite = node.addComponent(cc.Sprite)
                sprite.spriteFrame = this.mStar
                node.parent = this.mStarNode
            }
        }
    }
    public initRewardInfo(data:any){
        this.mNullNode.active = true;
        cc.resources.load(data.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mIcon.spriteFrame = spriteFrame;
            }
        });
        this.mBG.spriteFrame = this.mBGs[data.pz-1];
        if(data.count){
            this.mLevel.string = 'x'+data.count;
        }else{
            this.mLevel.string = '';
        }
    }

    public initEquip(main:Main,equip:EquipAttr){
        this.mNullNode.active = true;
        this.main = main;
        this.equip = equip;
        
        let sManage = main.sManage;
        this.equipData = sManage.getEquipById(equip.id);

        this.flushEquip(main);
    }
    public initGood(main:Main,data:any){
        this.mNullNode.active = true;
        this.main = main;
        this.data = data;
        if(this.data.icon == undefined){
            let sManage = this.main.sManage;
            this.good = sManage.getGoodById(this.data.id);
            this.data.icon = 'good/'+this.good.icon;
        }
        this.flushGood();
    }

    public init(main:Main,data:any){
        this.mNullNode.active = true;
        this.main = main
        this.data = data
        this.flushGood()
    }

    public flushEquip(main:Main){
        let equipData = this.equipData
        let qhLevel = FFCalAttr.getEquipQH(main,this.equip,equipData)
       
        if(qhLevel > 0){
            this.mLevel.string = 'Lv.'+qhLevel;
        }else{
            this.mLevel.string = ''
        }
        this.mNullNode.active = true;
        this.mBG.spriteFrame = this.mBGs[this.equipData.pz-1];
        cc.resources.load('icon/equip/'+this.equipData.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mIcon.spriteFrame = spriteFrame;
            }
        } );

        this.mStarNode.destroyAllChildren()
        for (let i = 0; i < this.equip.star; i++) {
            let node = new cc.Node()
            let sprite = node.addComponent(cc.Sprite)
            sprite.spriteFrame = this.mStar
            node.parent = this.mStarNode
        }
    }
    
    public flushGood(){
        cc.resources.load('icon/'+this.data.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mIcon.spriteFrame = spriteFrame;
            }
        } );
        this.mLevel.string = 'x'+this.data.count;
        if(this.good){
            this.mBG.spriteFrame = this.mBGs[this.good.pz-1];
        }else if(this.data.pz){
            this.mBG.spriteFrame = this.mBGs[this.data.pz-1];
        }
    }

    public setNull(){
        this.equip = null;
        this.equipData = null;
        this.data = null;
        this.good = null;
        this.mBG.spriteFrame = this.mNullIcon;
        this.mNullNode.active = false;
        this.callback = null;
    }

    public callback:(goodItem:GoodItem)=>void;

    public setCallback(callback:(goodItem:GoodItem)=>void){
        this.callback = callback;
    }

    public inGuide = false;//是否进行引导中
    private guideCallback:()=>void;
    public setGuideCallback(guideCallback:()=>void){
        this.guideCallback = guideCallback
    }

    public onclick(){
        if(this.callback){
            this.callback(this);
        }
        if(this.guideCallback){
            this.guideCallback()
            this.guideCallback = null;
        }
    }
    /**
     * 刷新数量对比
     */
    public initContrast(main:Main,data:any){
        this.main = main;
        this.data = data;
        if(this.data.id){
            let sManage = this.main.sManage;
            this.good = sManage.getGoodById(this.data.id);
            this.data.icon = 'good/'+this.good.icon;
        }
        this.flushGood();

        this.mLevel.node.active = false
        this.mContrast.node.active = true
        let count = this.main.player.getGoodCount(this.data.id)
        
        this.mContrast.string = count+'/'+data.count
        if(count >= data.count){
            this.mContrast.node.color = cc.color(75,43,0)
        }else{
            this.mContrast.node.color = cc.color(255,0,0)
        }
    }

}
