import { ui } from "../ui/layaMaxUI";
import { DispatcherManager } from "../GameLogic/DispatcherManager";
import MouseEvent from "../GameLogic/MouseEvent";
import GameManager from "../GameLogic/GameManager";
import { PlayerManager } from "../GameLogic/PlayerManager";
import UserModel from "../game_module/UserModel";
import NetManager, { PROTOCOLS } from "../game_module/NetManager";
import LevelController from "../GameLogic/LevelController";
import { FollowNumber } from "../Util/Mathf";
import { SettingController, SoundType } from "../GameLogic/SettingController";
import { WXSdk } from "../game_module/WXSdk";
import BannerAndVideo1 from "../game_module/BannerAndVideo1";
import ViewManager, { ViewType } from "../game_module/ViewManager";
//import YouZiHuiTuiController from "../game_module/YouZiHuiTuiController";
//import YouziCenter from "../youziscript/YouziCenter";



export default class CompleteView extends ui.CompleteViewUI implements UI_Interface {
    private static instance: CompleteView;
    static get Instance() {
        return CompleteView.instance;
    }
    constructor() {
        super();
        CompleteView.instance = this;
        this.resizeHander();
        DispatcherManager.on(Laya.Event.RESIZE, this, this.resizeHander);
    }
    private isWin = false;
    OnShow(data?: any) {
        this.isWin = data;
        // SettingController.Instance._playMusic(false);
        this.InitDate();
        this.InitHandler();
        this.InitUi();
        BannerAndVideo1.CheatBanner(this.winbtnBox);
        // YouZiHuiTuiController.Instance.ShowBannerHuiTui_main();
        
    }

    


    BannerMove() {
    }
    resizeHander() {
        this.height = Laya.stage.height;
    }
    private coinValue: number;
    InitDate() {
        this.coinValue = this.isWin == true ? LevelController.Instance._level.gold : LevelController.Instance._level.fail_gold;
        this.tes1 = this.coinValue;
        this.coinValue += (PlayerManager.tower_reward * GameManager.Instance.towerCount + PlayerManager.enemy_reward * GameManager.Instance.roleCount + PlayerManager.block_reward * GameManager.Instance.blockCount)
        let iswin = this.isWin == true ? 1 : 0;
        let count = this.coinValue;
        let param = [{ "t": UserModel.Instance.CurModel.ServerTime }, { "play_id": PlayerManager.Instance.Play_Id }, { "win": iswin }, { "money": count }];
        NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.GameResult, param,
            (data: any) => {
                // if (callback) callback();
            },
            (data) => {
                ViewManager.Instance.ShowTips("结算失败");
                // if (callback) callback();
            });

        if (this.isWin) {
            SettingController.Instance.PlaySound(SoundType.win);
            WXSdk.Success(PlayerManager.Instance.stage.toString());
        }
        else {
            SettingController.Instance.PlaySound(SoundType.defeat);
            WXSdk.Fail(PlayerManager.Instance.stage.toString());
        }
    }
    InitHandler() {

        this.doublegetBtn.CZ = () => {
            this.DoubleGetHandler();
        };

        this.nextBtn.CZ = () => {
            if (this.isWin) {
                this.NextHandler();
            } else {
                this.ResetHandler();
            }

        };
    }

    private tes1: number;
    InitUi() {
        this.winTop.visible = this.isWin;
        this.shibaiTop.visible = !this.isWin;
        //this.valueText.text =this.tes1+ "(+" + (this.coinValue-this.tes1)+")";
        this.valueText.text ="+" + this.coinValue;
        this.doubleText.text = (this.coinValue * 3).toString();
        this.towerCount.text = "X" + GameManager.Instance.towerCount.toString();
        this.towerValue.text = (PlayerManager.tower_reward * GameManager.Instance.towerCount).toString();
        this.roleCount.text = "X" + GameManager.Instance.roleCount.toString();
        this.roleValue.text = (PlayerManager.enemy_reward * GameManager.Instance.roleCount).toString();
        this.blockCount.text = "X" + GameManager.Instance.blockCount.toString();
        this.blockValue.text = (PlayerManager.block_reward * GameManager.Instance.blockCount).toString();
    }
  



    DoubleGetHandler() {
        BannerAndVideo1.ShowVideo((isok) => {
            if (isok) {
                WXSdk.AldEvent("结束界面看视屏获得钱");
                var param = [{ "t": UserModel.Instance.CurModel.ServerTime.toString() }, { "reward_item": "1000" }, { "reward_num": (this.coinValue * 2).toString() }];
                NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.Fissionvd, param,
                    (data: any) => {
                        ViewManager.Instance.ShowTips("金币:X" + (this.coinValue * 3));
                        this.NextHandler();
                    },
                    (data) => {

                    });
            }
        });
    }
    NextHandler() {
        GameManager.Instance.NextLevel();
    }
    ResetHandler() {
        ViewManager.Instance.CloseView(ViewType.CompleteView);
        ViewManager.Instance.AddView(ViewType.AttributeUpView);
        // GameManager.Instance.ReStart();
    }

    OnClose() {
        DispatcherManager.off(Laya.Event.RESIZE, this, this.resizeHander);
        CompleteView.instance = null;
        Laya.timer.clearAll(this);
      
        BannerAndVideo1.Clear();
    }
}