import { _decorator, Component, Node, ProgressBarComponent, director, SpriteComponent, loader, SpriteFrame } from 'cc';
import { GUIManager } from '../../../engines/gui/GUIManager';
import { GUIMediator } from '../../../engines/gui/GUIMediator';
import { DataModelEventType } from '../../../engines/models/DataModelEventType';
import { SceneManager } from '../../../engines/scenes/SceneManager';
import { PlatformManager } from '../../../Platform/PlatformManager';
import { branchIdType } from '../../../Platform/WeChat/branchIdType';
import { WeChatPlatform } from '../../../Platform/WeChat/WeChatPlatform';
import GameConfigManager from '../../models/GameConfigManager';
import { GameModel } from '../../models/GameModel';
import { GamePropertys } from '../../models/GamePropertys';
import { UIConst } from '../UIConst';
const { ccclass, property } = _decorator;

@ccclass('FreeUseGunMediator')
export class FreeUseGunMediator extends GUIMediator {

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

    @property({
        type: ProgressBarComponent
    })
    pregressBar: ProgressBarComponent = null;

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

    progress: number = 0;   //进度条  
    lastTime: number = 0;   //上一次更新时间

    randomProgress: number = 0.8;     //随机进度条

    weaponId:number = 11301;

    isShow: boolean = false;        //是否要显示banner


    OnShow(data?: any): void {
        super.OnShow(data);
        this.AddEvent();
        PlatformManager.hideBanner();
        this.BackButton.active = false;
        this.pregressBar.progress = this.progress;
        this.randomProgress = Math.random() * 0.4 + 0.4;
        this.lastTime = director.getCurrentTime();

        this.weaponId = GameConfigManager.GetSuperWeapon().weaponId;
        this.initWeaponIcon();

        this.scheduleOnce(this.enterGame.bind(this), 3)

    }

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

    initWeaponIcon(): void {
        let currentConfig:any=GameConfigManager.GetWeaponConfig(this.weaponId);
        //当前
        loader.loadRes(currentConfig.icon + "/spriteFrame", SpriteFrame, (err, res: SpriteFrame) => {
            if (err != null) {
                console.log("加载图标出错！");
            }
            this.weaponIcon.spriteFrame = res;
        });
    }

    private enterGame(): void {
        this.BackButton.active = true;
    }

    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 {

    }

    /**
     * 更新进度
     * @param progress 
     */
    UpdateProgress(progress: number): void {
        this.pregressBar.progress = progress;
    }

    /**
     * 疯狂点击
     */
    CrazyClick(): void {
        if (this.isShow == false) {
            this.progress += 0.15;
            this.progress = this.progress > 1 ? 1 : this.progress;
            this.UpdateProgress(this.progress);
            if (this.progress > this.randomProgress) {
                this.isShow = true;
                PlatformManager.showBanner();
                this.scheduleOnce(this.getSuperGun.bind(this), 1.5);
            }
        }
    }

    getSuperGun(): void {
        GameModel.single.trialWeaponId = this.weaponId;
        GameModel.single.useSuperWeapon = true;
        this.startGame();
    }

    startGame(): void {
        //隐藏
        GUIManager.single.Hide(UIConst.PREPARE_UI);
        this.HideSelf();
        PlatformManager.hideBanner();
        SceneManager.single.Swicth("FightingScene");
        let weChat = PlatformManager.impl as WeChatPlatform
        if (weChat instanceof WeChatPlatform) {
            weChat.branchAnalytics(branchIdType.EnterGame, String(GameModel.single.currentLevel))
        }
        this.isShow = false;
        this.progress = 0;
    }

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

    update(deltaTime: number) {
        let currentTime: number = director.getCurrentTime();
        if (currentTime - this.lastTime >= 50 && this.isShow == false) {
            this.lastTime = currentTime;
            this.progress -= 0.01;
            this.progress = this.progress < 0 ? 0 : this.progress;
            this.UpdateProgress(this.progress);
        }
    }
}
