import { _decorator, Component, Node, SpriteComponent, LabelComponent, loader, SpriteFrame, assert, director } from 'cc';
import { GUIManager } from '../../../engines/gui/GUIManager';
import { GUIMediator } from '../../../engines/gui/GUIMediator';
import { NoticeManager } from '../../../engines/notices/NoticeManager';
import { MathUtils } from '../../../engines/utils/MathUtils';
import StringUtils from '../../../engines/utils/StringUtils';
import { PlatformManager } from '../../../Platform/PlatformManager';
import GameConfigManager from '../../models/GameConfigManager';
import { GameModel } from '../../models/GameModel';
import { WeaponCell } from '../../models/weapons/WeaponCell';
import { GameController } from '../fightings/GameController';
import { UIConst } from '../UIConst';
const { ccclass, property } = _decorator;

@ccclass('DropBoxMediator')
export class DropBoxMediator extends GUIMediator {
    
    @property({
        type:SpriteComponent
    })
    weaponIcon:SpriteComponent=null;

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

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

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

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


    diamond:number=20;
    gold:number=0;
    type:number;
    weaponID:number;

    isFigthing:boolean;
    OnShow(data?:any):void{
        super.OnShow(data);
        this.isFigthing=data;

        this.type=Math.random();
        //钱
        if(this.type<0.5){
            this.gold=GameModel.single.fullEarnings*5*60;
            //五分钟收益
            this.goldLabel.string="X"+this.gold.toString();
            this.weaponState.active=false;
            this.goldState.active=true;
        }else{//枪
            this.weaponState.active=true;
            this.goldState.active=false;
            let weaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.synthesisMaxWeaponId);
            let maxLevel:number=weaponConfig.level;
            let minLevel:number=Math.max(1,maxLevel-3);
            let level:number=Math.floor(MathUtils.RandomRange(minLevel,maxLevel));
            weaponConfig=GameConfigManager.GetWeaponConfigByLevel(level);
            this.weaponID=weaponConfig.id;

            loader.loadRes(weaponConfig.icon+"/spriteFrame",SpriteFrame,(err,asset:SpriteFrame)=>{
                if(err!=null){
                    console.log("图标加载错误："+weaponConfig.icon);                    
                }
                this.weaponIcon.spriteFrame=asset;
            })
            this.weaponName.string=weaponConfig.name;
        }
        this.diamondLabel.string=this.diamond.toString();
        PlatformManager.hideBanner();
    }

    OnHide():void{

    }

    DiamondButtonClickHandler():void{
        if(GameModel.single.diamond<this.diamond){
            NoticeManager.ShowPrompt("钻石不足!");
            return;
        }
        if(this.AddAward()){
            this.closeView();
        }
    }

     /**
     * 视频按钮点击
     */
    VideoButtonClickHandler():void{
        PlatformManager.showRewardedVideo(()=>{
            if(this.AddAward()){
                this.closeView();
            }
        },()=>{
            NoticeManager.ShowPrompt("看视频失败");
        });
    }

    private AddAward():boolean{
        if(this.type<0.5){
            GameModel.single.gold+=this.gold;   
            NoticeManager.ShowPrompt("金币+"+StringUtils.numberUtilsEn(this.gold));
        }else{
            let cell:WeaponCell=GameModel.single.FindWeaponEmptyCell();
            if(cell){
                GameModel.single.AddWeapon(cell.cellId,this.weaponID);
            }else{
                return false;
            }
        }
        return true;
    }

    private closeView():void{
        if(this.isFigthing){
            GameController.single.PlayGame();
        }else{
            GUIManager.single.Show(UIConst.PREPARE_UI);
        }
        this.HideSelf();
    }

    CloseButtonClickHandler():void{
        this.closeView();
    }

    start () {
        // Your initialization goes here.
    }

    // update (deltaTime: number) {
    //     // Your update function goes here.
    // }
}
