
import { IWindow } from "./Component/IWindow";
import { ui } from "../ui/layaMaxUI";
import { PlayerAccount, AccountUser, entity } from "../Scripts/Network/PlayerAccount";
import { ResourcesManager } from "../Scripts/Game/ResourcesManager";
import PoolManager from "../Scripts/Game/PoolManager";
import { WindowManager, WindowType } from "./WindowManager";
import { HeroData, HatData } from "../Scripts/Game/LevelData";
import { GameUtils } from "../GameUtils/GameUtils";
import { NetworkManager, PROTOCOLS } from "../Scripts/Network/NetworkManager";
import WxApi from "../WXSDK/wxsdk";
import Vector2 = Laya.Vector2;
import { Player } from "../Scripts/Game/Player";
import ADPlayer from "../WXSDK/ADPlayer";
import { GameManager } from "../Scripts/Game/GameManager";

export enum ShopState{role,hat,cb}

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

    roleicon:Laya.Vector2 = new Vector2(69,172);
    cbicon:Vector2 = new Vector2(166,83);
    haticon:Vector2 = new Vector2(102,95);

    InitData()
    {
        this.herodatas = [];
        this.hatDatas = [];
        this.cbDatas = [];

        var gethero = [];
        var nogethero = [];

        var gethat = [];
        var nogethat = [];

        var getcb = [];
        var nogetcb = [];
        for(var key in PlayerAccount.CurrentAccount.CurAccountData.m_heroDic)
        {
            var data = PlayerAccount.CurrentAccount.CurAccountData.m_heroDic[key];
            if(PlayerAccount.CurrentAccount.CurAccountData.HasHero(data.id))
            {
                gethero.push(data);
            }
            else
            {
                nogethero.push(data);
            }
            //this.herodatas.push(data);
        }

        this.herodatas = gethero.concat(nogethero);

        for(var key in PlayerAccount.CurrentAccount.CurAccountData.m_hatDic)
        {
            var data1 = PlayerAccount.CurrentAccount.CurAccountData.m_hatDic[key];
            if(PlayerAccount.CurrentAccount.CurAccountData.HasHat(data1.id))
            {
                gethat.push(data1);
            }
            else
            {
                nogethat.push(data1);
            }
            //this.hatDatas.push(data1);
        }
        this.hatDatas = gethat.concat(nogethat);
        for(var key in PlayerAccount.CurrentAccount.CurAccountData.m_cbDic)
        {
            var data1 = PlayerAccount.CurrentAccount.CurAccountData.m_cbDic[key];
            if(PlayerAccount.CurrentAccount.CurAccountData.HasDane(data1.id))
            {
                getcb.push(data1);
            }
            else
            {
                nogetcb.push(data1);
            }
            //this.cbDatas.push(data1);
        }
        this.cbDatas = getcb.concat(nogetcb);
    }

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

    curstage:Laya.Sprite3D;
    showScence:Laya.Scene3D;
    showcarCamera:Laya.Camera;
    showlight:Laya.DirectionLight;

    showhatdic = {};
    curshowhat:Laya.Sprite3D;
    showcbdic = {};
    curshowcb:Laya.Sprite3D;
    curshowPlayer:Laya.Sprite3D;

    herodatas:HeroData[] = [];
    hatDatas:HatData[] = [];
    cbDatas:HatData[] = [];

    curstate:ShopState = ShopState.role;

    OnOpen(_data = null)
    {
        this.InitData();

        this.showScence = new Laya.Scene3D();
        this.addChild(this.showScence);
        this.ReadyShow();

        this.closeBtn.clickHandler = Laya.Handler.create(this,this.CloseW);
        this.renderList.vScrollBarSkin = "";
        this.roleBtn.clickHandler = Laya.Handler.create(this,this.ChangeState,[ShopState.role],false);
        this.hatBtn.clickHandler = Laya.Handler.create(this,this.ChangeState,[ShopState.hat],false);
        this.cbBtn.clickHandler = Laya.Handler.create(this,this.ChangeState,[ShopState.cb],false);
        this.vdawardBtn.clickHandler = Laya.Handler.create(this,this.OnVdAward,null,false);
        this.height = Laya.stage.height;
    }

    ChangeState(_state:ShopState)
    {
        this.curstate = _state;
        switch(_state)
        {
            case ShopState.role:
            this.OnShowRoleList();
            break;
            case ShopState.hat:
            this.OnShowHatList();
            break;
            case ShopState.cb:
            this.OnShowcbList();
            break;
        }
    }

    OnVdAward()
    {
        if(GameManager.Instance.videogetmoney <= 0)return;
        ADPlayer.showVideoAd((flag)=>
        {
            if(flag)
            {
                var awardmoney = 200;
                var agrs = [{"reward_item":"1000"},{"reward_num":awardmoney.toString()},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.vd,agrs,Laya.Handler.create(this,()=>
                {
                    GameManager.Instance.videogetmoney -= 1;
                    GameUtils.GetMoney(awardmoney);
                }),null)
            }
        })
        WxApi.Message("暂未开放");
    }

    CloseW()
    {
        WindowManager.Instance.ShowWindow(WindowType.HomeWindow);
    }

    OnShowRoleList()
    {
        this.hatbg.visible = true;
        this.cgbg.visible = true;
        this.rolebg.visible = false;
        this.renderList.array = this.herodatas;
        this.renderList.renderHandler = Laya.Handler.create(this,this.OnRenderRoleList,null,false);
    }

    OnShowHatList()
    {
        this.hatbg.visible = false;
        this.cgbg.visible = true;
        this.rolebg.visible = true;
        this.renderList.array = this.hatDatas;
        this.renderList.renderHandler = Laya.Handler.create(this,this.OnRenderHatList,null,false);
    }

    OnShowcbList()
    {
        this.hatbg.visible = true;
        this.cgbg.visible = false;
        this.rolebg.visible = true;
        this.renderList.array = this.cbDatas;
        this.renderList.renderHandler = Laya.Handler.create(this,this.OnRenderCbList,null,false);
    }

    ReadyShow()
    {
        this.showcarCamera = new Laya.Camera();
        this.showScence.addChild(this.showcarCamera);

        this.showlight = new Laya.DirectionLight();
        this.showScence.addChild(this.showlight);

        this.curstage = new Laya.Sprite3D();
        this.showScence.addChild(this.curstage);

        this.showcarCamera.viewport = new Laya.Viewport(this.carShowPan.x,this.carShowPan.y,this.carShowPan.width,this.carShowPan.height);
        this.showcarCamera.clearFlag = 2;
        this.showcarCamera.fieldOfView = 30;
        this.showcarCamera.transform.position = new Laya.Vector3(0,2,3);

        var playerid = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_hero));
        this.TryToShowCar(playerid);
        var hatid = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_hat));
        if(hatid > 0)this.TryToShowHat(hatid);
        var cbid = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_dance));
        if(cbid > 0)this.TryToShowCb(cbid);

        Laya.timer.frameLoop(1,this,this.ShowRot);

        this.ChangeState(ShopState.role);
    }

    OnRenderRoleList(_cell:Laya.Box,_index:number)
    {
        var data = _cell.dataSource as HeroData;
        var btn =  _cell.getChildByName("btn") as Laya.Button;
        var tryBtn = _cell.getChildByName("tryBtn") as Laya.Button;
        var roleicon = _cell.getChildByName("roleicon") as Laya.Image;
        roleicon.visible = true;
        var haticon = _cell.getChildByName("haticon") as Laya.Image;
        haticon.visible = false;
        var cbicon = _cell.getChildByName("cbicon") as Laya.Image;
        cbicon.visible = false;
        var bg = _cell.getChildByName("bg") as Laya.Image;
        var type = data.require_type;
        var num = data.require_num;

        bg.skin = data.id == Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_hero))?"shop/shangcheng_1_4_2.png":"shop/shangcheng_1_4_1.png";

        tryBtn.clickHandler = Laya.Handler.create(this,this.TryToShowCar,[data.id],false);
        roleicon.skin = "shop/"+ data.curicon + ".png";
        btn.visible = true;
        btn.clickHandler = null;
        var box = btn.getChildAt(0) as Laya.Box;
        box.visible = false;
        var box2 = btn.getChildAt(1) as Laya.Box;
        box2.visible = false;
        var box3 = btn.getChildAt(2) as Laya.Box;
        box3.visible = false;
        var box4 = btn.getChildAt(3) as Laya.Box;
        box4.visible = false;

        if(PlayerAccount.CurrentAccount.CurAccountData.HasHero(data.id))
        {
            btn.visible = false;
            return;
        }

        if(type == 2)
        {
            box.visible = true;
            var clip = box.getChildByName("clip") as Laya.FontClip;
            clip.value = "x" + num;
            btn.clickHandler = Laya.Handler.create(this,this.TryBuyHero,[data],false);
        }
        else if(type == 3)
        {
            box2.visible = true;
            var clip = box2.getChildByName("clip") as Laya.FontClip;
            clip.value = PlayerAccount.CurrentAccount.CurAccountData.GetHeroProgress(data.id) + "/" + num;
            btn.clickHandler = Laya.Handler.create(this,this.videoGetHero,[data.id],false);
        }
        else if(type == 4)
        {
            box3.visible = true;
            var clip = box3.getChildByName("clip") as Laya.FontClip;
            clip.value = PlayerAccount.CurrentAccount.CurAccountData.GetHeroProgress(data.id) + "/" + num;
            btn.clickHandler = Laya.Handler.create(this,this.ShareGetHero,[data.id],false);
        }
        else if(type == 6)
        {
            box4.visible = true;
        }
        else
        {
            btn.visible = false;
        }
    }

    OnRenderHatList(_cell:Laya.Box,_index:number)
    {
        var data = _cell.dataSource as HatData;
        var btn =  _cell.getChildByName("btn") as Laya.Button;
        var tryBtn = _cell.getChildByName("tryBtn") as Laya.Button;
        var roleicon = _cell.getChildByName("roleicon") as Laya.Image;
        roleicon.visible = false;
        var haticon = _cell.getChildByName("haticon") as Laya.Image;
        haticon.visible = true;
        var cbicon = _cell.getChildByName("cbicon") as Laya.Image;
        cbicon.visible = false;
        var bg = _cell.getChildByName("bg") as Laya.Image;
        var type = data.require_type;
        var num = data.require_num;

        bg.skin = data.id == Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_hat))?"shop/shangcheng_1_4_2.png":"shop/shangcheng_1_4_1.png";
        
        tryBtn.clickHandler = Laya.Handler.create(this,this.TryToShowHat,[data.id],false);
        haticon.skin = "shop/" + data.icon + ".png";
        btn.visible = true;
        btn.clickHandler = null;
        var box = btn.getChildAt(0) as Laya.Box;
        box.visible = false;
        var box2 = btn.getChildAt(1) as Laya.Box;
        box2.visible = false;
        var box3 = btn.getChildAt(2) as Laya.Box;
        box3.visible = false;
        var box4 = btn.getChildAt(3) as Laya.Box;
        box4.visible = false;

        if(PlayerAccount.CurrentAccount.CurAccountData.HasHat(data.id))
        {
            btn.visible = false;
            return;
        }

        if(type == 2)
        {
            box.visible = true;
            var clip = box.getChildByName("clip") as Laya.FontClip;
            clip.value = "x" + num;
            btn.clickHandler = Laya.Handler.create(this,this.TryBuyHat,[data],false);
        }
        else if(type == 3)
        {
            box2.visible = true;
            var clip = box2.getChildByName("clip") as Laya.FontClip;
            clip.value = PlayerAccount.CurrentAccount.CurAccountData.GetHatProgress(data.id) + "/" + num;
            btn.clickHandler = Laya.Handler.create(this,this.VideoGetHat,[data.id],false);
        }
        else if(type == 4)
        {
            box3.visible = true;
            var clip = box3.getChildByName("clip") as Laya.FontClip;
            clip.value = PlayerAccount.CurrentAccount.CurAccountData.GetHatProgress(data.id) + "/" + num;
            btn.clickHandler = Laya.Handler.create(this,this.ShareGetHat,[data.id],false);
        }
        else if(type == 6)
        {
            box4.visible = true;
        }
        else
        {
            btn.visible = false;
        }
    }

    OnRenderCbList(_cell:Laya.Box,_index:number)
    {
        var data = _cell.dataSource as HatData;
        var btn =  _cell.getChildByName("btn") as Laya.Button;
        var tryBtn = _cell.getChildByName("tryBtn") as Laya.Button;
        var roleicon = _cell.getChildByName("roleicon") as Laya.Image;
        roleicon.visible = false;
        var haticon = _cell.getChildByName("haticon") as Laya.Image;
        haticon.visible = false;
        var cbicon = _cell.getChildByName("cbicon") as Laya.Image;
        cbicon.visible = true;
        var bg = _cell.getChildByName("bg") as Laya.Image;
        var type = data.require_type;
        var num = data.require_num;

        bg.skin = data.id == Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_dance))?"shop/shangcheng_1_4_2.png":"shop/shangcheng_1_4_1.png";
        
        tryBtn.clickHandler = Laya.Handler.create(this,this.TryToShowCb,[data.id],false);
        cbicon.skin = "shop/" + data.icon + ".png";
        btn.visible = true;
        btn.clickHandler = null;
        var box = btn.getChildAt(0) as Laya.Box;
        box.visible = false;
        var box2 = btn.getChildAt(1) as Laya.Box;
        box2.visible = false;
        var box3 = btn.getChildAt(2) as Laya.Box;
        box3.visible = false;
        var box4 = btn.getChildAt(3) as Laya.Box;
        box4.visible = false;

        if(PlayerAccount.CurrentAccount.CurAccountData.HasDane(data.id))
        {
            btn.visible = false;
            return;
        }

        if(type == 2)
        {
            box.visible = true;
            var clip = box.getChildByName("clip") as Laya.FontClip;
            clip.value = "x" + num;
            btn.clickHandler = Laya.Handler.create(this,this.TryBuyCB,[data],false);
        }
        else if(type == 3)
        {
            box2.visible = true;
            var clip = box2.getChildByName("clip") as Laya.FontClip;
            clip.value = PlayerAccount.CurrentAccount.CurAccountData.GetCbProgress(data.id) + "/" + num;
            btn.clickHandler = Laya.Handler.create(this,this.videoGetCB,[data.id],false);
        }
        else if(type == 4)
        {
            box3.visible = true;
            var clip = box3.getChildByName("clip") as Laya.FontClip;
            clip.value = PlayerAccount.CurrentAccount.CurAccountData.GetCbProgress(data.id) + "/" + num;
            btn.clickHandler = Laya.Handler.create(this,this.ShareGetHat,[data.id],false);
        }
        else if(type == 6)
        {
            box4.visible = true;
        }
        else
        {
            btn.visible = false;
        }
    }

    TryToShowCar(_carid:number)
    {
        var asset = ResourcesManager.Instance.roleassets[_carid].getChildAt(0) as Laya.Sprite3D;
        this.OnPlayerLoad(_carid,asset);
        this.TryToShowHat(Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_hat)));
        this.TryToShowCb(Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.equip_hat)));
        if(PlayerAccount.CurrentAccount.CurAccountData.HasHero(_carid))
        {
            this.TryEquipHero(_carid);
        }
    }

    TryToShowHat(_id:number)
    {
        if(_id == 0)return;
        var asset = ResourcesManager.Instance.hatasset[_id] as Laya.Sprite3D;
        this.OnHatLoad(_id,asset);
        if(PlayerAccount.CurrentAccount.CurAccountData.HasHat(_id))
        {
            this.TryEquipHat(_id);
        }
    }

    TryToShowCb(_id:number)
    {
        if(_id == 0)return;
        var asset = ResourcesManager.Instance.cbasset[_id] as Laya.Sprite3D;
        this.onCbLoad(_id,asset);
        if(PlayerAccount.CurrentAccount.CurAccountData.HasDane(_id))
        {
            this.TryEquipCB(_id);
        }
    }

    TryEquipHero(_id:number)
    {
        var agrs = [{"id":_id.toString()}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.EquipHero,agrs,Laya.Handler.create(this,()=>
        {
            Player.Instance.RessetPlayer();
            this.InitData();
            this.renderList.array = this.herodatas;
        }),null);
    }

    TryEquipHat(_id:number)
    {
        var agrs = [{"id":_id.toString()}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.EquipHat,agrs,Laya.Handler.create(this,()=>
        {
            Player.Instance.RessetPlayer();
            this.InitData();
            this.renderList.array = this.hatDatas;
        }),null);
    }
    TryEquipCB(_id:number)
    {
        var agrs = [{"id":_id.toString()}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.EquipDance,agrs,Laya.Handler.create(this,()=>
        {
            Player.Instance.RessetPlayer();
            this.InitData();
            this.renderList.array = this.hatDatas;
        }),null);
    }

    TryBuyHero(_data:HeroData)
    {
        WxApi.Instance.recordAldEvent("商城界面:点击购买角色->"+_data.name,"1");

        var money = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money));
        if(money < _data.require_num)
        {
            WxApi.Message("金币不足！");
            return;
        };

        var agrs = [{"id":_data.id.toString()}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.BuyHero,agrs,Laya.Handler.create(this,()=>
        {
            WxApi.Instance.recordAldEvent("商城界面:成功购买角色->"+_data.name,"1");
            this.TryToShowCar(_data.id);
        }),null);
    }

    ShareGetHat(_id:number)
    {
        WxApi.Instance.recordAldEvent("商城界面:点击分享获得帽子皮肤->"+_id,"1");
        WxApi.shareImage("分享获得帽子",(flag)=>
        {
            if(flag)
            {
                var agrs = [{"id":_id.toString()},{"channel":"share"},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.fissionhat,agrs,Laya.Handler.create(this,()=>
                {
                    this.InitData();
                    this.renderList.array = this.hatDatas;
                }),Laya.Handler.create(this,()=>{}));
            }
        },"1")
    }

    ShareGetCB(_id:number)
    {
        WxApi.Instance.recordAldEvent("商城界面:点击分享获得翅膀皮肤->"+_id,"1");
        WxApi.shareImage("分享获得翅膀",(flag)=>
        {
            if(flag)
            {
                var agrs = [{"id":_id.toString()},{"channel":"share"},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.fissiondance,agrs,Laya.Handler.create(this,()=>
                {
                    this.InitData();
                    this.renderList.array = this.hatDatas;
                }),Laya.Handler.create(this,()=>{}));
            }
        },"1")
    }
    ShareGetHero(_id:number)
    {
        WxApi.Instance.recordAldEvent("商城界面:点击分享获得角色皮肤->"+_id,"1");
        WxApi.shareImage("分享获得英雄",(flag)=>
        {
            if(flag)
            {
                var agrs = [{"id":_id.toString()},{"channel":"share"},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.fissionhero,agrs,Laya.Handler.create(this,()=>
                {
                    this.InitData();
                    this.renderList.array = this.herodatas;
                }),Laya.Handler.create(this,()=>{}));
            }
        },"1")
    }
    VideoGetHat(_id:number)
    {
        WxApi.Instance.recordAldEvent("商城界面:点击看视频获得帽子皮肤","1");
        ADPlayer.showVideoAd((flag)=>
        {
            if(flag)
            {
                var agrs = [{"id":_id.toString()},{"channel":"video"},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.fissionhat,agrs,Laya.Handler.create(this,()=>
                {
                    this.InitData();
                    this.renderList.array = this.herodatas;
                }),Laya.Handler.create(this,()=>{}));
            }
        })
        //WxApi.Message("暂未开放");
    }
    videoGetHero(_id:number)
    {
        WxApi.Instance.recordAldEvent("商城界面:点击看视频获得角色皮肤","1");
        ADPlayer.showVideoAd((flag)=>
        {
            if(flag)
            {
                var agrs = [{"id":_id.toString()},{"channel":"video"},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.fissionhero,agrs,Laya.Handler.create(this,()=>
                {
                    this.InitData();
                    this.renderList.array = this.herodatas;
                }),Laya.Handler.create(this,()=>{}));
            }
        })
    }
    videoGetCB(_id:number)
    {
        WxApi.Instance.recordAldEvent("商城界面:点击看视频获得翅膀皮肤","1");
        ADPlayer.showVideoAd((flag)=>
        {
            if(flag)
            {
                var agrs = [{"id":_id.toString()},{"channel":"video"},{"t":PlayerAccount.CurrentAccount.ServerTime.toString()}];
                NetworkManager.Instance.HttpPostRequest(PROTOCOLS.fissiondance,agrs,Laya.Handler.create(this,()=>
                {
                    this.InitData();
                    this.renderList.array = this.herodatas;
                }),Laya.Handler.create(this,()=>{}));
            }
        })
    }

    TryBuyHat(_data:HatData)
    {
        WxApi.Instance.recordAldEvent("商城界面:点击购买帽子->"+_data.name,"1");
        var money = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money));
        if(money < _data.require_num)
        {
            WxApi.Message("金币不足！");
            return;
        };

        var agrs = [{"id":_data.id.toString()}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.BuyHat,agrs,Laya.Handler.create(this,()=>
        {
            this.TryToShowHat(_data.id);
        }),null);
    }

    TryBuyCB(_data:HatData)
    {
        WxApi.Instance.recordAldEvent("商城界面:点击购买翅膀->"+_data.name,"1");
        var money = Number(PlayerAccount.CurrentAccount.CurAccountData.GetUser(AccountUser.money));
        if(money < _data.require_num)
        {
            WxApi.Message("金币不足！");
            return;
        };

        var agrs = [{"id":_data.id.toString()}];
        NetworkManager.Instance.HttpPostRequest(PROTOCOLS.BuyDance,agrs,Laya.Handler.create(this,()=>
        {
            this.TryToShowCb(_data.id);
        }),null);
    }

    OnPlayerLoad(_id:number,_model:Laya.Sprite3D)
    {
        if(this.curshowPlayer != null)
        {
            if(this.curshowhat)
            {
                this.curshowhat.active = false;
                this.curshowhat.removeSelf();
            }
            if(this.curshowcb)
            {
                this.curshowcb.active = false;
                this.curshowcb.removeSelf();
            }
            
            PoolManager.Instance.RecoverMeshSprite3D(this.curshowPlayer);
        }

        // var brid = cursprite.getChildAt(1) as Laya.Sprite3D;
        // if(brid)brid.active = false;
        var player = PoolManager.Instance.CreatMeshSprite3D(_model,"");
        
        this.curshowPlayer = player;

        this.curstage.addChild(player);
        player.transform.localPosition = new Laya.Vector3(0,0,0);
        var lookpos = new Laya.Vector3(player.transform.position.x,player.transform.position.y + 0.03,player.transform.position.z + 0.01);
        this.showcarCamera.transform.lookAt(lookpos,new Laya.Vector3(0,1,0));
    }

    OnHatLoad(_id:number,_model:Laya.Sprite3D)
    {
        if(this.curshowhat != null)
        {
            this.curshowhat.active = false;
        }

        var sprite:Laya.Sprite3D;

        if(this.showhatdic[_id])
        {
            sprite = this.showhatdic[_id];
            sprite.active = true;
        }
        else
        {
            sprite = PoolManager.Instance.CreatMeshSprite3D(_model,"");
            this.showhatdic[_id] = sprite;
        }
        
        this.curshowhat = sprite;

        this.AddHatToBody(sprite);
        //this.curstage.addChild(sprite);
        sprite.transform.localPosition = new Laya.Vector3(0.06,0,0);
        sprite.transform.localRotationEuler = new Laya.Vector3(0,90,0);
    }

    onCbLoad(_id:number,_model:Laya.Sprite3D)
    {
        if(this.curshowcb != null)
        {
            this.curshowcb.active = false;
        }

        var sprite:Laya.Sprite3D;

        if(this.showcbdic[_id])
        {
            sprite = this.showcbdic[_id];
            sprite.active = true;
        }
        else
        {
            sprite = PoolManager.Instance.CreatMeshSprite3D(_model,"");
            this.showcbdic[_id] = sprite;
        }
        
        this.curshowcb = sprite;

        this.AddCbToBody(sprite);
        //this.curstage.addChild(sprite);
        sprite.transform.localPosition = new Laya.Vector3(-0.06,-0.06,0);
        sprite.transform.localRotationEuler = new Laya.Vector3(-25,90,180);
    }

    AddHatToBody(sprite:Laya.Sprite3D)
    {
        var head = this.curshowPlayer.getChildAt(0).getChildAt(0) as Laya.Sprite3D;
        head.addChild(sprite);
        //sprite.transform.position = head.transform.position
        //console.log(head);
    }

    AddCbToBody(sprite:Laya.Sprite3D)
    {
        var back = this.curshowPlayer.getChildAt(0).getChildAt(1) as Laya.Sprite3D;
        back.addChild(sprite);
        //console.log(back);
    }

    ShowRot()
    {
        this.curstage.transform.rotate(new Laya.Vector3(0,-0.01,0));
    }

    OnHide()
    {
        Laya.timer.clearAll(this);
        for(var key in this.showhatdic)
        {
            var hat = this.showhatdic[key];
            hat.active = true;
            PoolManager.Instance.RecoverMeshSprite3D(hat);
        }
        for(var key in this.showcbdic)
        {
            var cb = this.showcbdic[key];
            cb.active = true;
            PoolManager.Instance.RecoverMeshSprite3D(cb);
        }
        PoolManager.Instance.RecoverMeshSprite3D(this.curshowPlayer);
        this.showcarCamera.destroy();
        this.showScence.destroy();
    }
}