import { ui } from "../ui/layaMaxUI";
import { DispatcherManager } from "../Game/DispatcherManager";
import {MouseEvent} from "../Game/MouseEvent";
import {GameManager} from "../Game/GameManager";
import { PlayerManager } from "../Game/PlayerManager";
import UserModel from "../Net/UserModel";
import NetManager, { PROTOCOLS } from "../Net/NetManager";
import {LevelController} from "../Game/LevelController";
import { FollowNumber } from "../Util/MathTool";
import { SettingController, SoundType } from "../Game/SettingController";
import { WXSdk } from "../Net/WXSdk";
import {GuangGao} from "../Net/GuangGao";
import  {ViewManager, ViewType } from "../Net/ViewManager";
import { FYHD_Sdk } from "../Net/FYHD_Sdk";
//import YouZiHuiTuiController from "../Net/YouZiHuiTuiController";
//import YouziCenter from "../youziscript/YouziCenter";



export default class CompleteView extends ui.CompleteViewUI implements SC {
    private static instance: CompleteView;
    private banner:any;
    static get Instance() {
        return CompleteView.instance;
    }
    constructor() {
        super();
        CompleteView.instance = this;
        this.Resize();
        DispatcherManager.on(Laya.Event.RESIZE, this, this.Resize);
    }
    private isWin = false;
    OnShow(data?: any) {
        this.isWin = data;
        this.RefreshData();
        this.RegButton();
        this.RefreshUI();
        this.SetTopAd();
        if(GameManager.GameCount == 1 || GameManager.GameCount>=4){
            // GuangGao.OpenBanner();
            GuangGao.Instance.showBanner((_banner)=>{
                if(_banner){
                    this.banner = _banner["banner"];
                    console.log("banner加载完毕---",this.banner);
                }
            });
            this.nextBtn.y = Laya.stage.height-this.nextBtn.height-300;
        }
        else{
            if(FYHD_Sdk.iswhite){
                this.nextBtn.y = Laya.stage.height-this.nextBtn.height-50;
            }
            else{
                GuangGao.Instance.showBanner((_banner)=>{
                    if(_banner){
                        this.banner = _banner["banner"];
                        console.log("banner加载完毕---",this.banner);
                    }
                });
                this.nextBtn.y = Laya.stage.height-this.nextBtn.height-300;
            }
        }
    }

    Resize() {
        this.height = Laya.stage.height;
    }
    private coinValue: number;
    RefreshData() {
        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());
        }
    }
    //点击普通领取按钮次数
    clicknum = 0;
    RegButton() {
        this.ExitBtn.visible = FYHD_Sdk.iswhite;
        this.ExitBtn.clickHandler = new Laya.Handler(this,this.ClickExit);
        this.doublegetBtn.CZ = () => {
            this.DoubleGet();
        };
        this.nextBtn.CZ = () => {
            this.clicknum ++;
            if(GameManager.GameCount <= 1 || GameManager.GameCount>=4){
                if (this.isWin) {
                    this.Next();
                } else {
                    this.Resset();
                }
            }
            else{
                if(FYHD_Sdk.iswhite){
                    if(this.clicknum <= 1){
                        GuangGao.CheatBanner3(this.nextBtn,()=>{

                        });
                    }
                    else{
                        if (this.isWin) {
                            this.Next();
                        } else {
                            this.Resset();
                        }
                    }
                }else{
                    if (this.isWin) {
                        this.Next();
                    } else {
                        this.Resset();
                    }
                }
            }
        };
    }
    ClickExit(){
        ViewManager.Instance.AddView(ViewType.ExitView);
    }

    Next() {
        GameManager.Instance.LevelUp();
    }
    Resset() {
        ViewManager.Instance.CloseView(ViewType.CompleteView);
        // ViewManager.Instance.AddView(ViewType.AttributeUpView);
        GameManager.Instance.ReStart();
    }

    OnClose() {
        DispatcherManager.off(Laya.Event.RESIZE, this, this.Resize);
        CompleteView.instance = null;
        Laya.timer.clearAll(this);
        if(FYHD_Sdk.iswhite){
            if(GuangGao.Instance._bannerAd3){
                GuangGao.Instance._bannerAd3.hide();
            }
            if(GuangGao.Instance._bannerAd){
                GuangGao.Instance._bannerAd.hide();
            }
        }

        if(GameManager.GameNum==1){
            if(GameManager.GameCount <= 3 && FYHD_Sdk.iswhite){
                ViewManager.Instance.AddView(ViewType.GoldEggView);
            }
            // ViewManager.Instance.AddView(ViewType.AttributeUpView);
            ViewManager.Instance.AddView(ViewType.ExportView);
        }
        else{
            if(GameManager.GameCount <= 3&& FYHD_Sdk.iswhite){
                ViewManager.Instance.AddView(ViewType.GoldEggView);
            }
            // ViewManager.Instance.AddView(ViewType.IntensifyView);
            ViewManager.Instance.AddView(ViewType.ExportView,true);
        }
        

    }

    private tes1: number;
    RefreshUI() {
        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.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();
    }
  

    DoubleGet() {
        GuangGao.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.Next();
                    },
                    (data) => {

                    });
            }
        });
    }

    topAdData: any;
    topLeftAdIndex: any;
    bottomleftAdIndex
    topRightAdIndex: any;
    bottomRightAdIndex:any;
    //左右抖动互推
    SetTopAd() {
        if(Laya.Browser.onPC)return;
        this.topAdData = FYHD_Sdk.pushData || [];
        if (this.topAdData.length < 4) return;
        this.topLeftAdIndex = { index: 0 };
        this.topRightAdIndex = { index: 1 };
        this.bottomleftAdIndex = {index:2};
        this.bottomRightAdIndex = {index:3};

        this._setTopAd(this.LeftTopPushIcon, this.topLeftAdIndex);
        this._setTopAd(this.LeftBottomPushIcon, this.bottomleftAdIndex);
        this._setTopAd(this.RightTopPushIcon, this.topRightAdIndex);
        this._setTopAd(this.RightBottomPushIcon, this.bottomRightAdIndex);
    }
    private _setTopAd(topAd: Laya.Button, adIndex: any) {
        let _icon = topAd.getChildByName("icon") as Laya.Image;
        let _name = topAd.getChildByName("name") as Laya.Text;
        topAd.clickHandler =new Laya.Handler(this, () => {
            FYHD_Sdk.ShowHuTuiJump(this.topAdData[adIndex.index]);
        });
        this._setIcon(_icon,_name, adIndex);
        Laya.timer.loop(3000, this, () => {
            this._setIcon(_icon,_name, adIndex);
            this.pushshake.play(null,false);
        });
    }
    private _setIcon(_icon: Laya.Image, _name:Laya.Text, adIndex: any) {
        adIndex.index = (adIndex.index + 4) % this.topAdData.length;
        _icon.skin = this.topAdData[adIndex.index].icon;
        _name.text = this.topAdData[adIndex.index].togame_name;
    }
}