import { _decorator, Component, Node, LabelComponent, Vec2, Vec3, view } from 'cc';
import { GUIManager } from '../../../engines/gui/GUIManager';
import { GUIMediator } from '../../../engines/gui/GUIMediator';
import { NoticeManager } from '../../../engines/notices/NoticeManager';
import { SceneManager } from '../../../engines/scenes/SceneManager';
import StringUtils from '../../../engines/utils/StringUtils';
import { LiangLiangSDK } from '../../../libs/liangliangSDK';
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 { GameController } from '../fightings/GameController';
import { UIConst } from '../UIConst';
const { ccclass, property } = _decorator;

@ccclass('GameOverMediator')
export class GameOverMediator extends GUIMediator {
    @property(Node)
    WinGroup: Node = null;


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

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


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

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

    @property(Node)
    DefeatedGroup: Node = null;

    @property(Node)
    WinBackButton: Node = null;

    @property(Node)
    DefeatedBackButton: Node = null;

    private isWin: boolean;
    private award: number;
    private diamond:number;
    onLoad() {
        // PlatformManager.showBanner()
    }

    OnShow(data?: any): void {
        super.OnShow(data);
        this.isWin = data;
        this.WinGroup.active = this.isWin;
        this.DefeatedGroup.active = !this.isWin;
        //胜利
        if (this.isWin == true) {
            this.WinGroup.active = true;
            this.DefeatedGroup.active = false;
            let levelConfig: any = GameConfigManager.GetLevelConfig(GameModel.single.currentLevel - 1);
            //关卡基础奖励
            this.award = levelConfig.awards + levelConfig.killAward * GameModel.single.killCount;
            //金币奖励
            this.WinAwardLabel.string ="X"+StringUtils.numberUtilsEn(this.award,0);
            //钻石奖励
            if(levelConfig.diamondRewards>0){
                this.diamond=levelConfig.diamondRewards;
                this.DiamondAwardLabel.string="X"+StringUtils.numberUtilsEn(levelConfig.diamondRewards,0);
            }else{
                this.diamond=0;
                this.DiamondAwardLabel.string="";
            }
            let weChat = PlatformManager.impl as WeChatPlatform;
            if(weChat instanceof WeChatPlatform){
                // weChat.successBranchAnalytics(String(GameModel.single.currentLevel));
                weChat.branchAnalytics(branchIdType.SuccessGame, String(GameModel.single.currentLevel))
            }
        } else {//失败
            //失败不算奖励
            this.WinGroup.active = false;
            this.DefeatedGroup.active = true;
            let weChat = PlatformManager.impl as WeChatPlatform;
            if(weChat instanceof WeChatPlatform){
                // weChat.failBranchAnalytics(String(GameModel.single.currentLevel));
                weChat.branchAnalytics(branchIdType.FailGame, String(GameModel.single.currentLevel))
            }
        }
        this.backButtonHandle();
    }

    /**
     * 返回按钮误触
     */
    backButtonHandle(): void {
        if (LiangLiangSDK.CanWuChu() == true) {
            this.WinBackButton.position = new Vec3(0, -(view.getVisibleSize().height / 2 - 50), 0);
            this.DefeatedBackButton.position = new Vec3(0, -(view.getVisibleSize().height / 2 - 50), 0);
            this.scheduleOnce(() => {
                PlatformManager.showBanner();
                this.WinBackButton.position = new Vec3(0, -375, 0);
                this.DefeatedBackButton.position = new Vec3(0, -358, 0);
            }, 3)
        } else {
            PlatformManager.showBanner();
        }
    }

    /**
     * 开始下一关
     */
    PlayNextLevel(): void {
        this.AddAward();
        this.HideSelf();
        GameController.single.PlayNextLevel();
    }

    /**
     * 重玩
     */
    Replay(): void {
        PlatformManager.showRewardedVideo(() => {
            PlatformManager.hideBanner();
            this.AddAward();
            this.HideSelf();
            GameController.single.Replay();
        }, () => {
            NoticeManager.ShowPrompt("复活失败");
        })
    }

    /**
     * 返回主界面
     */
    BackToMain(): void {
        this.AddAward();
        SceneManager.single.Swicth("PrepareScene");
        GUIManager.single.Hide(UIConst.FIGHTING_UI);
        //关闭自身
        this.HideSelf();
    }

    /**
     * 去全屏幕界面
     */
    GoToFullOutput(): void {
        this.AddAward();
        GUIManager.single.Show(UIConst.FULL_OUTPUT_UI);
        this.HideSelf();
    }

    /**
     * 十倍之后去全屏界面
     */
    getTenAward(): void {
        this.AddAdAward(10, () => {
            GUIManager.single.Show(UIConst.FULL_OUTPUT_UI);
            this.HideSelf();
        });
    }

    /**
     * 十倍奖励
     * @param value 
     */
    private AddAdAward(value: number = 1, success): void {
        if (this.isWin) {
            PlatformManager.showRewardedVideo(() => {
                GameModel.single.gold += this.award * value;
                success();
            }, () => {
                NoticeManager.ShowPrompt("领取失败");
            })
        }
    }

    /**
     * 获取奖励
     */
    private AddAward(value: number = 1): void {
        if (this.isWin) {
            GameModel.single.gold += this.award * value;
            GameModel.single.diamond+=this.diamond*value;
        }
    }

    OnHide(): void {

    }
}
