import { ui } from "../ui/layaMaxUI";
import { AccountManager } from "../Net/AccountManager";
import MainGameView from "./MainGameView";
import { ServerManager, ArcherWorrior } from "../Net/ServerManager";
import { StageControl } from "../Game/StageControl";
import { GameManager } from "../Game/GameManager";
import { EventManager, StageState } from "../Tools/EventManager";
import GuangGao from "../Net/GuangGao";
import { WXSDK } from "../Net/WXSDK";
import { AudioManager, MusicType } from "../Tools/AudioManager";
import { ViewManager, ViewType } from "./ViewManager";
import { SDKZS } from "../Net/SDKZS";
// import { ZSSdk } from "../Net/ZSSDK";

export default class CompleteView extends ui.CompleteViewUI{
    private static ins:CompleteView;
    public win:boolean = true;
    constructor(){
        super();
    }
    public static get Instance():CompleteView{
        if(CompleteView.ins){
            return CompleteView.ins
        }
        else{
            return new CompleteView();
        }
    }
    Show(_data){//1.胜利 2.失败 3.复活
        this.Init(_data);
        GuangGao.WudaoAnNiu(this.BtnBox);
        let gold = this.DealData(GameManager.Instance.KillNum);
        this.goldnum.text =Math.floor(gold).toString();
        this.SetData();
        this.height = Laya.stage.height;
        Laya.stage.on(Laya.Event.RESIZE,this,()=>
        {
            this.height = Laya.stage.height;
        })
    }
    Close(){

    }
    Init(_data){
        switch (_data) {
            case 1:
                WXSDK.AldEvent("打开胜利界面");
                this.loseimage.visible = false;
                this.reviveimage.visible = false;
                this.NoBtn.visible = false;
                this.TryAgainBtn.visible = false;
                this.ReviveBtn.visible = false;
                this.NextLevelBtn.clickHandler = Laya.Handler.create(this, this.NextLevel,[true]);
                break;
            case 2:
                WXSDK.AldEvent("打开失败界面");
                this.winimage.visible = false;
                this.reviveimage.visible = false;
                this.NoBtn.visible = false;
                this.NextLevelBtn.visible = false;
                this.ReviveBtn.visible = false;
                this.TryAgainBtn.clickHandler = Laya.Handler.create(this,this.TryAgain,[false]);
                break;
            case 3:
                WXSDK.AldEvent("打开结算界面");
                this.winimage.visible = false;
                this.loseimage.visible = false;
                this.NextLevelBtn.visible = false;
                this.TryAgainBtn.visible = false;
                this.NoBtn.clickHandler = Laya.Handler.create(this,this.NoRevive);
                this.ReviveBtn.clickHandler = Laya.Handler.create(this,this.Revive);
        }
    }
    NextLevel(_win){
        let stage = AccountManager.Instance.curplayerData.GainAccount("stage");
        WXSDK.AldEnd(stage,true);
        AudioManager.playMusic(MusicType.click);
        this.win = _win;//记录一下本局胜负情况
        var iswin = _win?"1":"0";
        var coinnum = this.goldnum.text;
        var args = [{"t":AccountManager.Instance.ServerTime.toString()},{"play_id":MainGameView.Instance.play_id},{"win":iswin},{"money":coinnum}];
        ServerManager.Instance.SendHttp(ArcherWorrior.JieShu,args,Laya.Handler.create(this,this.Success),null);
    }
    Revive(){
        
        AudioManager.playMusic(MusicType.click);
        GuangGao.Instance.loadVideo(isLoaded => {
            if (isLoaded) {
                GuangGao.Instance.showVideo(isOk => {
                    if (isOk) {
                        WXSDK.AldEvent("观看视频复活成功");
                        GameManager.Instance.ReviveNum -=1;
                        GameManager.Instance.Revive();
                    }
                    else {
                        
                    }
                });
            }
            else {
                WXSDK.fengxiangtupian(isOk => {
                    if (isOk) {
                        WXSDK.AldEvent("分享复活成功");
                        GameManager.Instance.ReviveNum -=1;
                        GameManager.Instance.Revive();
                    }
                    else {
                        EventManager.StageTrigger(StageState.Lose);
                    }
                });
            }
        })
    }
    NoRevive(){
        GameManager.Instance.ReviveNum-=1;
        ViewManager.Instance.ShowView(ViewType.CompleteView,2);
    }
    TryAgain(_win){
        let stage = AccountManager.Instance.curplayerData.GainAccount("stage");
        WXSDK.AldEnd(stage,false);
        AudioManager.playMusic(MusicType.click);
        this.win = _win;//记录一下本局胜负情况
        var iswin = _win?"1":"0";
        var coinnum =this.goldnum.text ;
        var args = [{"t":AccountManager.Instance.ServerTime.toString()},{"play_id":MainGameView.Instance.play_id},{"win":iswin},{"money":coinnum}];
        ServerManager.Instance.SendHttp(ArcherWorrior.JieShu,args,Laya.Handler.create(this,this.Success),null);
    }
    Success(){
        EventManager.StageTrigger(StageState.Restart);
        ViewManager.Instance.OpenPopView(ViewType.ExportView);
    }

    /**传入玩家kill的npc数量，返回所得的奖励 */
    DealData(_num:number):number{
        if(_num>StageControl.Instance.curstage.Goal){
            _num = StageControl.Instance.curstage.Goal
        }
        var _reward = AccountManager.Instance.curplayerData.GlobalData("mission_reward");
        var gold = _reward*StageControl.Instance.curstage.Modulus*(_num/StageControl.Instance.curstage.Goal);
        return gold;
    }

    private SetData() {
        SDKZS.SetHuTuiList(this.PushList, 1, "");
        this.GunGun();
    }
    GunGun() {
        var maxTop;
        var maxBtm;
        try {
            this.PushList.scrollBar.value = 0;
            maxBtm = this.PushList.scrollBar.max;
        } catch (error) {
            console.error(error);
            return;
        }
        let tempTop = 1;
        let tempBtm = 1;
        Laya.timer.frameLoop(1, this, () => {
            this.PushList.scrollBar.value += tempBtm;
            if (this.PushList.scrollBar.value >= maxBtm || this.PushList.scrollBar.value <= 0) {
                tempBtm = -tempBtm;
            }
        });
    }
}
