import { Component, director, find, LabelComponent, loader, Node, SpriteComponent, SpriteFrame, systemEvent, SystemEventType, Touch, Vec2, Vec3, _decorator } from 'cc';
import { NoticeManager } from '../../../engines/notices/NoticeManager';
import NodeUtils from '../../../engines/utils/NodeUtils';
import StringUtils from '../../../engines/utils/StringUtils';
import GameConfigManager from '../../models/GameConfigManager';
import { GameModel } from '../../models/GameModel';
import { WeaponCell } from '../../models/weapons/WeaponCell';
const { ccclass, property } = _decorator;

@ccclass('WeaponCellScript')
export class WeaponCellScript extends Component {
    

    @property({
        type:Node
    })
    useing:Node=null;

    @property({
        type:LabelComponent
    })
    levelLabel:LabelComponent=null;

    @property({
        type:SpriteComponent
    })
    iconLoader:SpriteComponent=null;

    @property({
        type:SpriteComponent
    })
    goldIconLoader:SpriteComponent=null;

    
    @property({
        type:Node
    })
    imgState1:Node=null;

    @property({
        type:Node
    })
    imgState2:Node=null;
    
    @property({
        type:Node
    })
    imgState3:Node=null;

    @property({
        type:Node
    })
    imgState4:Node=null;

    @property({
        type:Node
    })
    labelGroup:Node=null;

    @property({
        type:LabelComponent
    })
    earningsLabel:LabelComponent=null;

    /**
     * 武器槽数据
     */
    public weaponCell:WeaponCell;

    /**
     * 当前武器的配置
     */
    private weaponConfig:any;

    /**
     * 状态 0 空槽状态  1有枪常态 2 可合成
     */
    private state:number=0;

    start () {
        this.AddEvent();
    }

    private AddEvent():void{
        
    }

    private RemoveEvent():void{
        
    }

    public UpdateWeaponCell(value:WeaponCell):void{
        this.weaponCell=value;
        if(this.weaponCell.weaponId<0){
            this.state=0;
            
        }else{
            this.state=1;
        }
        this.RefreshState();
        //取消选中
        this.ChangeSelected(null);

        if(this.weaponCell.weaponId<0){
            this.earningsLabel.node.active=false;
            this.labelGroup.active=false;
            this.levelLabel.string="";
            this.iconLoader.spriteFrame=null;
            this.goldIconLoader.node.active=false;
        }else{
            this.goldIconLoader.node.active=true;
            this.weaponConfig=GameConfigManager.GetWeaponConfig(this.weaponCell.weaponId);
            //收益
            this.earningsLabel.node.active=true;
            this.earningsLabel.string=StringUtils.numberUtilsEn(this.weaponConfig.earnings*GameModel.single.earningTime/1000);
            //等级
            this.labelGroup.active=true;
            if(GameConfigManager.WeaponIsMaxLevel(this.weaponCell.weaponId)){
                this.levelLabel.string="Max"
            }else{
                this.levelLabel.string=this.weaponConfig.level.toString();
            }
            loader.loadRes(this.weaponConfig.icon+"/spriteFrame",SpriteFrame,this.loadCompleteHandler.bind(this));
        }
    }
    
    private loadCompleteHandler(err:Error,asset:SpriteFrame):void{
        if(err!=null){
            return;
        }
        this.iconLoader.spriteFrame=asset;
    }

    /**
     * 选中
     */
    public ChangeSelected(value:WeaponCell):void{
        if(this.weaponCell==value){
            this.imgState4.active=true;
        }else{
            this.imgState4.active=false;
        }
    }

    /**
     * 标记为可以
     */
    public Mark(value:boolean):void{
        if(value){
            this.state=2;
            this.RefreshState();
        }else{
            this.UpdateWeaponCell(this.weaponCell);
        }
    }

    private RefreshState():void{
        if(this.state==0){//没有武器
            this.imgState1.active=true;
            this.imgState2.active=this.imgState3.active=this.imgState4.active=this.iconLoader.node.active=false;
        }else if(this.state==1){//有武器
            this.imgState1.active=false;
            this.imgState3.active=false;
            this.imgState2.active=true;
            this.iconLoader.node.active=true;
        }else if(this.state==2)//可合成状态
        {
            this.imgState1.active=false;
            this.imgState3.active=true;
            this.imgState2.active=true;
        }
        if(GameModel.single.currentWeaponCell==this.weaponCell){
            this.useing.active=true;
        }else{
            this.useing.active=false;
        }
    }

    update (deltaTime: number) {
        if(this.weaponCell.weaponId<0){
            return;
        }
        let currentTime:number=director.getCurrentTime();
        let IntervalTime:number=GameModel.single.earningTime;
        let IntervalTimeS:number=IntervalTime/1000;
        if(currentTime-this.weaponCell.lastOutputTime>IntervalTime){
            let startPos:Vec3=new Vec3();
            NodeUtils.GetNodePos(this.node,startPos);
            startPos.x+=this.node.width/2;
            NoticeManager.ShowPromptByPos("金币：+"+StringUtils.numberUtilsEn(this.weaponCell.weaponConfig.earnings* IntervalTimeS),startPos);
            this.weaponCell.lastOutputTime=currentTime;
        }
    }

    destroy():void{
        super.destroy();
        this.RemoveEvent();
    }
}
