
import { IWindow } from "./Component/IWindow";
import { ui } from "../ui/layaMaxUI";
import { GameManager, GameState } from "../Scripts/Game/GameManager";
import { LevelManager } from "../Scripts/Game/LevelManager";
import { NetworkManager, PROTOCOLS } from "../Scripts/Network/NetworkManager";
import WxApi from "../WXSDK/wxsdk";
import { PlayerAccount, AccountUser } from "../Scripts/Network/PlayerAccount";
import { Queue } from "../Scripts/Game/Queue";
import { Player } from "../Scripts/Game/Player";
import { SignWindow } from "./SignWindow";
import { WindowManager, WindowType } from "./WindowManager";
import ADPlayer from "../WXSDK/ADPlayer";
import { GameUtils } from "../GameUtils/GameUtils";
import { RecommendWindow } from "./RecommendWindow";


export class SettlementWindow extends ui.Window.SettlementWindowUI implements IWindow
{
    private static instance:SettlementWindow;
    constructor()
    {
        super();
        SettlementWindow.instance = this;
    }

    public static get Instance()
    {
        return SettlementWindow.instance;
    }

    OnOpen(win = false)
    {
        WxApi.Instance.recordAldEvent("进入结算界面 "+  "win:"+win + " stage:" + LevelManager.Instance.curLevel.levelid,"1");
        if(win)
        {
            this.winshow.visible = true;
        }
        else
        {
            this.loseshow.visible = true; 
        }
        //this.awardBtn.clickHandler = Laya.Handler.create(this,this.OnPlayAgain,[win]);
        this.getmoney.value = GameManager.Instance.gamegold.toString();
        this.scoreTex.value = Player.Instance.m_runner.die?"6":Queue.Instance.GetPlayerScore.toString();
        var iswin = win?"1":"0";
        var rank = Queue.Instance.GetPlayerScore;
        var equiphero = PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_hero);
        var qeuiphat = PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_hat);
        var qeuipdance = PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_dance);
        var agrs = [{"t":PlayerAccount.CurrentAccount.ServerTime.toString()},{"win":iswin},{"rank":rank.toString()},{"money":GameManager.Instance.gamegold.toString()},{"hat":qeuiphat},{"hero":equiphero},{"dance":qeuipdance}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.end,agrs,Laya.Handler.create(this,this.OnSettlementEnd,[win]),Laya.Handler.create(this,this.OnError));

        WindowManager.Instance.OpenPopView(WindowType.RecommendWindow);
        RecommendWindow.Instance.ShowRecommendWindow(false,false,false,true,false);
        //GuangGao.WudaoAnNiu(this.Cheat,true);

        // ADPlayer.showBannerAd();
        ADPlayer.MoveButton(this.BtnPan);
        this.awardBtn.clickHandler = Laya.Handler.create(this,this.OnVdAward);
        this.height = Laya.stage.height;
    }

    OnVdAward()
    {
        WxApi.Instance.recordAldEvent("点击3倍奖励","");
        ADPlayer.showVideoAd((flag)=>
        {
            if(flag)
            {
                var awardmoney = GameManager.Instance.gamegold * 3;
                var agrs = [{"reward_item":"1000"},{"reward_num":awardmoney.toString()},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.vd,agrs,Laya.Handler.create(this,()=>
                {
                    GameUtils.GetMoney(awardmoney);
                }),null)
            }
        })
        //WxApi.Message("暂未开放");
    }

    OnPlayAgain(_win:boolean)
    {
        if(_win)
        {
            GameManager.Instance.EnterNextLevel();
        }
        else
        {
            GameManager.Instance.RessetGame();
        }
        if(SignWindow.keyiqiandao)
        {
            WindowManager.Instance.ShowWindow(WindowType.SignWindow);
        }
        else
        {
            ADPlayer.ClearBanner();
            RecommendWindow.Instance.chouti.visible = true;
            RecommendWindow.Instance.choutiBtn.visible = false;
            RecommendWindow.Instance.OpenChouTiView(true);
            RecommendWindow.Instance.closeCt.clickHandler = Laya.Handler.create(this,()=>
            {
                WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
            });
            
        }
    }

    OnSettlementEnd(_win:boolean,_data)
    {
        var level = LevelManager.Instance.curLevel;
        if(_win)
        {
            WxApi.Instance.levelSuccess(level.levelid.toString(),level.levelid.toString(),PlayerAccount.CurrentAccount.CurAccountData.UseId);
        }
        else
        {
            WxApi.Instance.levelFail(level.levelid.toString(),level.levelid.toString(),PlayerAccount.CurrentAccount.CurAccountData.UseId,Player.Instance.CurLevelPrecent.toString(),Player.Instance.m_runner.m_sprite.name)
        }
        
        var stage = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.stage));
        if(_win)WxApi.writeStage(stage);
        this.backhome.clickHandler = Laya.Handler.create(this,this.OnPlayAgain,[_win]);
    }

    OnError(e)
    {
        console.log(e);
    }

    OnHide()
    {

    }
}