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

@ccclass('FreeUpgradeMediator')
export class FreeUpgradeMediator extends GUIMediator {
    @property({
        type: LabelComponent
    })
    glodLabel: LabelComponent = null;

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

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

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

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

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

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

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

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

    private a:WeaponCell;
    private b:WeaponCell;
    private nextWeaponId:number;
    OnShow(data?:any):void{
        super.OnShow(data);
        this.a=data.a;
        this.b=data.b;
        let currentConfig:any=this.a.weaponConfig;
        let level:number=currentConfig.level+1;
        currentConfig=GameConfigManager.GetWeaponConfigByLevel(level);
        for (let index = 0; index < 2; index++) {
            const element:any=GameConfigManager.GetWeaponConfigByLevel(level);
            if(element!=null){
                if(GameConfigManager.WeaponIsMaxLevel(element.id)==false){
                    level++;
                }
            }
        }
        let nextConfig:any=GameConfigManager.GetWeaponConfigByLevel(level);
        this.nextWeaponId=nextConfig.id;

        //当前
        loader.loadRes(currentConfig.icon+"/spriteFrame",SpriteFrame,(err,res:SpriteFrame)=>{
            if(err!=null){
                console.log("加载图标出错！");
            }
            this.weaponIcon.spriteFrame=res;
        });
        this.weaponName.string=currentConfig.name;
        this.weaponLevel.string=currentConfig.level;

        loader.loadRes(nextConfig.icon+"/spriteFrame",SpriteFrame,(err,res:SpriteFrame)=>{
            if(err!=null){
                console.log("加载图标出错！");
            }
            this.targetWeaponIcon.spriteFrame=res;
        });
        this.targetWeaponName.string=nextConfig.name;
        this.targetWeaponLevel.string=nextConfig.level;

        this.RefreshGlod();
        this.RefreshDiamond();
        this.AddEvent();

        GUIManager.single.Hide(UIConst.PREPARE_UI);
        PlatformManager.hideBanner();
    }

    OnHide():void{
        this.RemoveEvent();
    }

    private AddEvent():void{
        GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
    }

    private RemoveEvent():void{
        GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
    }

    private GameModelPropertyChanged(key: string): void {
        switch (key) {
            case GamePropertys.gold:
                this.CallNextFrame(this.RefreshGlod.bind(this));
                break;
            case GamePropertys.diamond:
                this.CallNextFrame(this.RefreshDiamond.bind(this));
                break;
        }
    }

    private RefreshGlod(): void {
        if (this.glodLabel != null) {
            this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
        }
        this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
    }

    private RefreshDiamond(): void {
        if (this.diamondLabel != null) {
            this.diamondLabel.string = GameModel.single.diamond.toString();
        }
    }
    
    AbandonButtonClickHandler():void{
        GameModel.single.SynthesisWeapon(this.a.cellId,this.b.cellId);
        this.closeView();
    }

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

    private AddWeapon(weaponId:number):boolean{
        let cell:WeaponCell=GameModel.single.FindWeaponEmptyCell();
        if(cell==null){
            NoticeManager.ShowPrompt("武器格子不足！");
            return false;
        }
        let select:boolean=false;
        if(GameModel.single.currentWeaponCell==this.a||GameModel.single.currentWeaponCell==this.b){
            select=true;
        }
        //删除A
        GameModel.single.RemoveWeapon(this.a.cellId);
        //删除B
        GameModel.single.RemoveWeapon(this.b.cellId);
        //添加新武器
        GameModel.single.AddWeapon(cell.cellId,weaponId);
        if(select){
            GameModel.single.currentWeaponCellId=cell.cellId;
            GameModel.single.DispatchEvent(DataModelEventType.PROPERTY_CHANGED,GamePropertys.WeaponCell);
        }
        let synthesisMaxWeaponConfig:any=GameConfigManager.GetWeaponConfig(GameModel.single.synthesisMaxWeaponId);
        let newWeaponConfig:any=GameConfigManager.GetWeaponConfig(cell.weaponId);
        //新记录
        if (newWeaponConfig.level > synthesisMaxWeaponConfig.level) {
            GameModel.single.synthesisMaxWeaponId = cell.weaponId;
            if(GameModel.single.currentWeaponCell.weaponConfig && newWeaponConfig.level > GameModel.single.currentWeaponCell.weaponConfig.level){//如果比手上的好
                GameModel.single.EquipWeapon(cell);
                return;
            }
        }
        return true;
    }


    private closeView():void{
        GUIManager.single.Show(UIConst.PREPARE_UI);
        this.HideSelf();
    }

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

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