import {AgencyBaseControl} from "./AgencyBaseControl";
import {GameState} from "../utils/GameState";
import { ViewManager } from "../View/ViewManager";
import {CompletePanel} from "../View/CompletePanel";
import {GameStateControl} from "../Control/GameStateControl";
import { GameUtils } from "../utils/GameUtils";
import {SceneControl} from "../Control/SceneControl";
import {PropManager} from "../PrefabControl/PropManager";
import { PlayerAccount, AccountUser } from "../WXSDK/PlayerAccount";
import { PROTOCOLS, NetworkManager } from "../WXSDK/NetworkManager";
import { WxApi } from "../WXSDK/Sdk";
import {AgencyTrial} from "./AgencyTrial";
import { FYHD_Sdk } from "../WXSDK/FYHD_Sdk";
import FriendInviteView from "../Push/FriendInviteView";
import GiftView from "../View/GiftView";
import ChoutiPanView from "../Push/ChoutiPanView";
import { RecordManager } from "../WXSDK/RecordManager";
import { MainPanel } from "../View/MainPanel";

export  class AgencyInGame extends AgencyBaseControl 
{
    static key:string = "AgencyInGame";
    static gamenum:number = 0;

    constructor() { super(); }

    protected initSelf():void
    {
        this.DisOn( GameState.end,this,this.endgameHandler );
        this.DisOn(GameState.revive,this,this.RevivegameHandler);
        this.DisOn(GameState.lose,this,this.LosegameHandler);
    }
    /**复活界面 */
    private RevivegameHandler():void{
        Laya.timer.once(1000,this,this.reviveView);
    }
    /**失败界面 */
    private LosegameHandler():void{
        Laya.timer.once(1000,this,this.loseView);
    }
    private reviveView(){
        RecordManager.Instance.RecordStop();
        var nowmingcvi:number = GameStateControl.Instance.nowMingCi;
        var getGold:number = 0;
        WxApi.recordAldEvent("打开结算界面(复活)");
        ViewManager.Instance.add( CompletePanel,getGold );
    }
    private loseView():void{
        RecordManager.Instance.RecordStop();
        var nowmingcvi:number = GameStateControl.Instance.nowMingCi;
        var getGold:number = 0;
        WxApi.recordAldEvent("打开结算界面(失败)");
        this.RecropLevelEnd( false );
        ViewManager.Instance.add( CompletePanel,getGold );
    }
    private endgameHandler():void
    {
        RecordManager.Instance.RecordStop();
        Laya.timer.once( 1000,this,this.startEndView);
    }

    public startEndView():void
    {
        var nowmingcvi:number = GameStateControl.Instance.nowMingCi;
        var getGold:number = 0;
        if( GameStateControl.Instance.endGamePlayerArr.length == 0 )
        {
            getGold = this.RoleData.levelcfg.winGold;
            WxApi.recordAldEvent("打开结算界面(游戏胜利)");
            this.RecropLevelEnd( true );
        }
        else
        {
            var arr:Array<string> = GameUtils.cutstrtoArr( this.RoleData.levelcfg.failGod );
            getGold = GameUtils.randRangeint( Number(arr[0]),Number(arr[1]) );
            WxApi.recordAldEvent("打开结算界面(游戏失败)");
            this.RecropLevelEnd( false );
        }
        ViewManager.Instance.add( CompletePanel,getGold );
        // ViewManager.Instance.add( ChoutiPanView,2 );
    }

    RecropLevelEnd(_win:boolean)
    {
        // if(!Laya.Browser.onWeiXin)return;
        // var stageid = this.RoleData.LevelID.toString();
        // var useid = PlayerAccount.CurrentAccount.CurAccountData.UseId;
        // var stagename = "第" + this.RoleData.LevelID + "关";
        // if(_win)
        // {
        //     WxApi.levelSuccess(stageid,stagename,stageid);
        // }
        // else
        // {
        //     var equipid = this.RoleData.CharacterID.toString();
        //     var carname = this.RoleData.roleCfg.name;
        //     WxApi.levelFail(stageid,stagename,stageid,"0",carname);
        // }
    }

    public returnmainview(win?):void
    {
        if (win && PlayerAccount.CurrentAccount.CurAccountData.WhiteUser) {
            let ran = Math.random();
            console.log("误触几率为---", ran);
            if (ran <= PlayerAccount.CurrentAccount.CurAccountData.EndCheat) {
                ViewManager.Instance.add(GiftView);
            }
            GameState.fuhuocishu = 1;
            GameState.recordscree = false;
            GameState.isdie = false;
            PropManager.Instance.CloseGameOverEffect();
            ViewManager.Instance.close(CompletePanel);
            this.DisTrigger(GameState.returnMainView);
            SceneControl.Instance.ShowTheGameScene();
            var agencytrial: AgencyTrial = this.getSingleTon(AgencyTrial);
            if (agencytrial.isshiyonging)  {
                SceneControl.Instance.CreatePlayer(this.RoleData.roleCfg);
                agencytrial.isshiyonging = false;
            }
        }
        else{
            GameState.fuhuocishu = 1;
            GameState.recordscree = false;
            GameState.isdie = false;
            PropManager.Instance.CloseGameOverEffect();
            ViewManager.Instance.close(CompletePanel);
            this.DisTrigger(GameState.returnMainView);
            SceneControl.Instance.ShowTheGameScene();
            var agencytrial: AgencyTrial = this.getSingleTon(AgencyTrial);
            if (agencytrial.isshiyonging)  {
                SceneControl.Instance.CreatePlayer(this.RoleData.roleCfg);
                agencytrial.isshiyonging = false;
            }
        }
        if(GameState.GameNum%3 == 0){
            MainPanel.instance.skinclick();
        }
        //打开好友邀请界面
        // ViewManager.Instance.add(FriendInviteView);
    }

    public startGame():void
    {
        AgencyInGame.gamenum ++;
        var stageid = this.RoleData.getNowServerLevel().toString().toString();
        var stagename = "第" + stageid + "关";
        var useid = PlayerAccount.CurrentAccount.CurAccountData.UseId;
        WxApi.levelStart(stageid,stagename,useid);
        GameStateControl.Instance.startGame();
    }
   
}