import {PanelBaseControl} from "../Control/PanelBaseControl";
import {ViewAction} from "../utils/ViewAction";
import {GameStateControl} from "../Control/GameStateControl";
import { GlobalTip } from "../utils/GlobalTip";
import {AgencyUserMessage} from "../Agency/AgencyUserMessage";
import {AgencyInGame} from "../Agency/AgencyInGame";
import { PlayerAccount, AccountUser } from "../WXSDK/PlayerAccount";
import { PROTOCOLS, NetworkManager } from "../WXSDK/NetworkManager";
import { ViewManager } from "../View/ViewManager";
import {pingtaiutils} from "../WXSDK/pingtaiutils";
import {GuangGao} from "../WXSDK/GuangGao";
import { GameState } from "../utils/GameState";
import { Player } from "../Game/Player";
import {PropManager} from "../PrefabControl/PropManager";
import { DispatcherManager } from "../utils/DispatcherManager";
import {CharacterState} from "../PrefabControl/CharacterState";
import { ShareUtil } from "../WXSDK/ShareUtil";
import { RecordManager } from "../WXSDK/RecordManager";
import { ttad } from "../WXSDK/ttad";
// import ChoutiPanView from "../Push/ChoutiPanView";
export class CompletePanel extends PanelBaseControl 
{
    static key:string = "CompletePanel";

    BtnBox:fairygui.GGroup;
    c_state:fairygui.Controller;
    txt_gold:fairygui.GTextField;
    btn_0:fairygui.GButton;
    btn_1:fairygui.GButton;
    btn_2:fairygui.GButton;
    timenum:fairygui.GTextField;
    TianJiaAni:fairygui.Transition;
    BattleBtn:fairygui.GButton;

    tianjia:fairygui.GLoader;

    //分享界面
    ShareVideo:fairygui.GComponent;
    ShareVideoBtn:fairygui.GButton;
    jiesuanbtn:fairygui.GButton;
    share_goldnum:fairygui.GTextField;

    //框
    kuang:fairygui.GButton;
    //对号
    duihao:fairygui.GImage;


    getgold:number = 0;
    private _gameProxy:AgencyInGame;
    
    constructor( gold:number ) 
    { 
        super( "main","gameresult",false,ViewAction.ActionType_none,1 ); 
        this.getgold = gold;
    }
    
    /*************************************************************** */
    private loadScene():void {
		Laya.Scene3D.load("res/threeDimen/scene/ParticleScene/Example_01.ls", Laya.Handler.create(this, function(scene:Laya.Scene3D) {
			this.scene = Laya.stage.addChildAt(scene, 0);
			var camera:Laya.Camera = this.scene.addChild(new Laya.Camera(0, 0.1, 100));
			camera.transform.translate(new Laya.Vector3(0, 1, 0));
		}));
	}
	private garbageCollection():void {
		this.scene.destroy();//销毁场景
		this.scene = null;
		Laya.Resource.destroyUnusedResources();//销毁无用资源(没有被场景树引用,并且没有加资源锁的)
    }
    /*************************************************************** */

    private btn1click():void
    {
        if( this.c_state.selectedIndex == 0 )
        {
            if(this.duihao.visible){
                this.playvideocallback();
            }
            else{
                ( this.getProxy( AgencyUserMessage ) as AgencyUserMessage ).GoldUp( this.getgold );
                ( this.getProxy( AgencyUserMessage ) as AgencyUserMessage ).LevelUp(  );
                this._gameProxy.returnmainview(true);
            }
            
        }
        else if(this.c_state.selectedIndex == 1)
        {
            ( this.getProxy( AgencyUserMessage ) as AgencyUserMessage ).GoldUp( this.getgold );
            this._gameProxy.returnmainview( );
            // ViewManager.Instance.add( ChoutiPanView );
        }
        else if(this.c_state.selectedIndex == 2){
            // ShareUtil.shareImage("分享复活----",this.sharerevive,"1")
            this.sharerevive();
        }
    }

    public onComplete(): void {
        Laya.Sprite3D.load("res/threeDimen/skinModel/LayaMonkey/LayaMonkey.lh", Laya.Handler.create(this, function(layaMonkey3D:Laya.Sprite3D):void {
            this.layaMonkey3D = layaMonkey3D;
            this.scene.addChild(layaMonkey3D);
            this.layaMonkey2D = Laya.stage.addChild(new Laya.Image("res/threeDimen/monkey.png")) as Laya.Image;
            Laya.timer.frameLoop(1, this, this.animate);
        }))
    }

    /******************************************** */
    private scene:Laya.Scene3D;
    private camera:Laya.Camera;
    private layaMonkey3D:Laya.Sprite3D;
    private layaMonkey2D:Laya.Image;
    private _position:Laya.Vector3 = new Laya.Vector3();
    private _outPos:Laya.Vector3 = new Laya.Vector3();
    private scaleDelta:number = 0;
    private scale:Laya.Vector3 = new Laya.Vector3(0.1,0.1,0.1);
    private animate(): void {
        this._position.x = Math.sin(this.scaleDelta += 0.01);
        this.layaMonkey3D.transform.position = this._position;
        this.layaMonkey3D.transform.scale = this.scale;
        //转换坐标
        this.camera.viewport.project(this.layaMonkey3D.transform.position, this.camera.projectionViewMatrix, this._outPos);
        //赋值给2D
        this.layaMonkey2D.pos(this._outPos.x / Laya.stage.clientScaleX, this._outPos.y / Laya.stage.clientScaleY);
    }
/******************************************** */
    

    private btn0click():void
    {
        if( this.c_state.selectedIndex == 0 )
        {
            this.playvideocallback();
            // pingtaiutils.Instance.playVideo( "",this,this.playvideocallback );
        }
        else if(this.c_state.selectedIndex == 1)
        {
            ( this.getProxy( AgencyUserMessage ) as AgencyUserMessage ).GoldUp( this.getgold );
            this._gameProxy.returnmainview( );
            // ViewManager.Instance.add( ChoutiPanView );
        }
        else if(this.c_state.selectedIndex == 2){
            this.count = false;
            // pingtaiutils.Instance.playVideo("",this,this.playvideorevive);
            this.playvideorevive();
        }
    }

    /**看视频复活成功 */
    private playvideorevive():void{
        ttad.ShowVideo((isLoad=>{
            if(isLoad){
                // ViewManager.Instance.add(ChoutiPanView);
                GameState.fuhuocishu --;
                ViewManager.Instance.close( CompletePanel );
                GameState.isdie = false;
                Player.Instance._iscanMove = true;
                // Player.Instance.PlayAnimator( CharacterState.run );
                Player.Instance.nowAction = CharacterState.run;
                if (Player.Instance._nowscenename == "" || Player.Instance._nowbuildname == "")  {
                    console.log("玩家位置----",Player.Instance._initPosVec);
                    console.log("玩家速度_moveVec",Player.Instance._moveVec);
                    Player.Instance._transform.position = new Laya.Vector3(0,1,3);
                }
                else  {
                    console.log("当前场景名字---",Player.Instance._nowscenename);
                    console.log("当前建筑名字---",Player.Instance._nowbuildname);
                    console.log("建筑物位置----",PropManager.getPosVecbyScene(Player.Instance._nowscenename, Player.Instance._nowbuildname));
                    Player.Instance._transform.position = PropManager.getPosVecbyScene(Player.Instance._nowscenename, Player.Instance._nowbuildname).clone();
                }
                Player.Instance._speedMoveV3.z = -Player.Instance._RoleData.movespeed*2;
                Laya.timer.once(3000,this,()=>{
                    Player.Instance._speedMoveV3.z = -Player.Instance._RoleData.movespeed;
                })
            }
            else{
                this.count = true;
            }
        }))
    }
    /**分享复活成功 */
    private sharerevive(){
        RecordManager.Instance.ShareVideo((isLoad)=>{
             if(isLoad){
                RecordManager.Instance.RecordStart();
                GameState.fuhuocishu --;
                // GameState.recordscree = true;
                ViewManager.Instance.close( CompletePanel );
                GameState.isdie = false;
                Player.Instance._iscanMove = true;
                // Player.Instance.PlayAnimator( CharacterState.run );
                Player.Instance.nowAction = CharacterState.run;
                if (Player.Instance._nowscenename == "" || Player.Instance._nowbuildname == "")  {
                    console.log("玩家位置----",Player.Instance._initPosVec);
                    Player.Instance._transform.position = new Laya.Vector3(0,1,3);
                }
                else  {
                    console.log("当前场景名字---",Player.Instance._nowscenename);
                    console.log("当前建筑名字---",Player.Instance._nowbuildname);
                    console.log("建筑物位置----",PropManager.getPosVecbyScene(Player.Instance._nowscenename, Player.Instance._nowbuildname));
                    Player.Instance._transform.position = PropManager.getPosVecbyScene(Player.Instance._nowscenename, Player.Instance._nowbuildname).clone();
                }
                    // GlobalTip.Instance.showWordTips("复活加速成功");
                    Player.Instance._speedMoveV3.z = -Player.Instance._RoleData.movespeed*2;
                    Laya.timer.once(3000,this,()=>{
                        Player.Instance._speedMoveV3.z = -Player.Instance._RoleData.movespeed;
                    })
            }
            else{
                this.count = true; 
            }
        })
        // if(_success){
        //     // ViewManager.Instance.add(ChoutiPanView);
        //         GameState.fuhuocishu --;
        //         ViewManager.Instance.close( CompletePanel );
        //         GameState.isdie = false;
        //         Player.Instance._iscanMove = true;
        //         // Player.Instance.PlayAnimator( CharacterState.run );
        //         Player.Instance.nowAction = CharacterState.run;
        //         if (Player.Instance._nowscenename == "" || Player.Instance._nowbuildname == "")  {
        //             console.log("玩家位置----",Player.Instance._initPosVec);
        //             Player.Instance._transform.position = Player.Instance._initPosVec.clone()
        //         }
        //         else  {
        //             console.log("当前场景名字---",Player.Instance._nowscenename);
        //             console.log("当前建筑名字---",Player.Instance._nowbuildname);
        //             console.log("建筑物位置----",PropManager.getPosVecbyScene(Player.Instance._nowscenename, Player.Instance._nowbuildname));
        //             Player.Instance._transform.position = PropManager.getPosVecbyScene(Player.Instance._nowscenename, Player.Instance._nowbuildname).clone();
        //         }
        //             // GlobalTip.Instance.showWordTips("复活加速成功");
        //             Player.Instance._speedMoveV3.z = -Player.Instance._RoleData.movespeed*2;
        //             Laya.timer.once(3000,this,()=>{
        //                 Player.Instance._speedMoveV3.z = -Player.Instance._RoleData.movespeed;
        //             })
        // }
        // else{
        //     this.count = true;
        // }
    }

    private playvideocallback():void
    {
        ttad.ShowVideo((isLoad=>{
            if(isLoad){
                ( this.getProxy( AgencyUserMessage ) as AgencyUserMessage ).GoldUp( this.getgold*3 );
                ( this.getProxy( AgencyUserMessage ) as AgencyUserMessage ).LevelUp(  );
                this._gameProxy.returnmainview(true);
                this.BattleBtn.visible = false;
            }
        }))
        // ttad.ShowVideo((isLoad=>{
        //     if(isLoad){
        //         ( this.getProxy( AgencyUserMessage ) as AgencyUserMessage ).GoldUp( this.getgold*3 );
        //         this._gameProxy.returnmainview(true);
        //     }
        // }))
    }

    //分享视频
    private ShareVideoCallBack(){
        RecordManager.Instance.ShareVideo((isLoad=>{
            if(isLoad){
                ( this.getProxy( AgencyUserMessage ) as AgencyUserMessage ).GoldUp( this.getgold*3 );
                this.BattleBtn.visible = false;
                this.ShareVideo.visible = false;
            }
        }))
    }

    //点击勾选框
    ClickKuang(){
        if(this.duihao.visible){
            this.duihao.visible = false;
            this.btn_1.visible = this.duihao.visible;
            this.btn_2.visible = !this.duihao.visible;
        }
        else{
            this.duihao.visible = true;
            this.btn_1.visible = this.duihao.visible;
            this.btn_2.visible = !this.duihao.visible;
        }
    }

    

    public destory():void
    {
        Laya.timer.clear(this,this.CountTime);
        // Laya.timer.clear(this,this.playanimator);
        // GuangGao.Clear();
        ttad.Clear();
        super.destory();
    }

    protected initView():void
    {
        this.tianjia.visible = false;
        this.TianJiaAni = this.getTransitionByName("tianjiaani");
        this.TianJiaAni.stop();
        // Laya.timer.loop(2000,this,this.playanimator);
        this.count = true;
        
        this._gameProxy = this.getProxy( AgencyInGame );
        this.c_state = this.getControllerByBame( "c_state" );
        this.txt_gold.text = this.getgold.toString();
        
        this.duihao = this.kuang.getChildAt(1) as fairygui.GImage;
        this.duihao.visible = PlayerAccount.CurrentAccount.CurAccountData.Choose;
        this.share_goldnum = this.ShareVideo.getChildAt(4) as fairygui.GTextField;
        this.ShareVideoBtn = this.ShareVideo.getChildAt(1) as fairygui.GButton;
        this.jiesuanbtn = this.ShareVideo.getChildAt(2) as fairygui.GButton;
        this.share_goldnum.text = this.getgold.toString();
        this.btn_1.visible = this.duihao.visible;
        this.btn_2.visible = !this.duihao.visible;

        if(GameState.recordscree ){
            this.BattleBtn.visible = false;
        }
        else{
            this.BattleBtn.visible = false;
            if(PlayerAccount.CurrentAccount.CurAccountData.ShowBattleBtn){
                this.BattleBtn.visible = true;
            }
        }
        // this.BattleBtn.visible = !GameState.recordscree;
        if(!GameState.isdie){//玩家未死亡的状态
            if( GameStateControl.Instance.endGamePlayerArr.length == 0 )
            {
                this.c_state.selectedIndex = 0;
            }
            else
            {
                this.c_state.selectedIndex = 1;
            }
        }
        else{
            if(GameState.fuhuocishu>0){
                this.c_state.selectedIndex = 2;
                Laya.timer.loop(1000,this,this.CountTime);
            }
            else{
                this.c_state.selectedIndex = 1;
            }
        }
        if(this.c_state.selectedIndex == 0){
            this.ShareVideo.visible = true;
            this.jiesuanbtn.visible = false;
            Laya.timer.once(2000,this,()=>{
                this.jiesuanbtn.visible = true;
            })
        }
        else{
            this.ShareVideo.visible = false;
        }
        GuangGao.CheatBanner(this.btn_1);
    }
    playanimator(){
        this.TianJiaAni.play(null,1);
    }

    ClickJieSuan(){
        this.ShareVideo.visible = false;
    }

    protected initEvent():void
    {
        this.AddEventByType( this.btn_0,Laya.Event.CLICK,this,this.btn0click );
        this.AddEventByType( this.btn_1,Laya.Event.CLICK,this,this.btn1click );
        this.AddEventByType( this.BattleBtn,Laya.Event.CLICK,this,this.ShareBattle);
        this.AddEventByType( this.btn_2,Laya.Event.CLICK,this,this.btn1click);
        this.AddEventByType( this.kuang,Laya.Event.CLICK,this,this.ClickKuang);
        this.AddEventByType( this.ShareVideoBtn,Laya.Event.CLICK,this,this.ShareVideoCallBack);
        this.AddEventByType( this.jiesuanbtn,Laya.Event.CLICK,this,this.ClickJieSuan);
    }

     time = 9;
     count = false;
     fuhuo = false;
     //倒计时
     CountTime()
     {
         if(this.count && this.time > 0)
         {
             this.time -= 1;
             this.timenum.text = this.time.toString();
             if(this.time < 1)
             {
                Laya.timer.clear(this,this.CountTime);
                GameState.fuhuocishu--;
                // Laya.timer.clear(this,this.playanimator);
                ViewManager.Instance.close(CompletePanel);
                DispatcherManager.dispatchEvent( GameState.lose );
             }
         }
     }

     //录屏分享
     ShareBattle(){
         RecordManager.Instance.ShareVideo((isLoad)=>{
            if(isLoad){
                this.BattleBtn.visible = false;
            }
         })
        //  RecordManager.Instance.ShareVideo((isLoad=>{
        //      if(isLoad){
        //          this.BattleBtn.visible = false;
        //      }
        //  }))

     }
}