import {PanelBaseControl} from "../Control/PanelBaseControl";
import {ViewAction} from "../utils/ViewAction";
import {AgencySkin} from "../Agency/AgencySkin";
import {SkinItem} from "../utils/SkinItem";
import {GameState} from "../utils/GameState";
import {UserInfoEvent} from "../Data/UserInfoEvent";
import {RoleData} from "../Data/RoleData";
import {ResourceControl} from "../Control/ResourceControl";
import { AssetPath } from "../Config/AssetPath";
import {PropManager} from "../PrefabControl/PropManager";
import {CharacterState} from "../PrefabControl/CharacterState";
import { ViewManager } from "../View/ViewManager";
import {CharacterPool} from "../PrefabControl/CharacterPool";
import {Player} from "../Game/Player";

export class SkinPanel extends PanelBaseControl 
{
    static key:string = "SkinPanel";
    c_title:fairygui.Controller;
    list_0:fairygui.GList;
    btn_close:fairygui.GButton;
    private _ui3DScene:Laya.Scene3D;
    private _uicarma3D:Laya.Camera;
    // private _uilight:Laya.DirectionLight;
    private agencyskin:AgencySkin;
    private _rolemo:Laya.Sprite3D;
    private _isdestorys:boolean = false;
    constructor() 
    { 
        super( "main","skinview",false,ViewAction.ActionType_none,0 ); 
    }

    protected initView():void
    {
        this._isdestorys = false;
        this.c_title = this.getControllerByBame( "c_title" );
        this.agencyskin = this.getProxy( AgencySkin );
        this.list_0.setVirtual();
        this.list_0.itemRenderer = Laya.Handler.create( this,this.itemlistrender,null,false );
        this.c_title.selectedIndex = 0;
        this.onchange();
        //添加3D场景
        this._ui3DScene = this.displayObject.addChildAt(new Laya.Scene3D(),0) as Laya.Scene3D;
        //添加照相机
        this._uicarma3D = (this._ui3DScene.addChild(new Laya.Camera(0, 0.1, 80))) as Laya.Camera;
        // this._uicarma3D.clearColor = null;
        this._uicarma3D.viewport = new Laya.Viewport(this.x,this.y,this.width,this.height);
        this._uicarma3D.transform.translate(new Laya.Vector3(0,1.2,5),false);

        // //添加方向光
        // this._uilight = PropManager.scene3D.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
        // this._uilight.color = new Laya.Vector3(0.6,0.6,0.6);
        // this._uilight.transform.worldMatrix.setForward(new Laya.Vector3(1, -1, 0));

        this.updateroleHandler();
    }

    protected initEvent():void
    {
        this.AddEventByType( this.btn_close,Laya.Event.CLICK,this,this.onclosefun );
        this.AddEventByType( this.c_title,fairygui.Events.STATE_CHANGED,this,this.onchange );
        this.addDataEvent( GameState.updateSKinView,this,this.onchange );
        this.addDataEvent( UserInfoEvent.userinfoEvent_updateroleid,this,this.updateroleHandler );
    }

    

    private ClearCharacter():void
    {
        if( this._rolemo )
        {
            var script:Player = this._rolemo.getComponent( Player ) as Player;
            CharacterPool.ClearCharacter( this._rolemo,script.resouseName,script );
            this._rolemo = null;
        }
    }

    private onclosefun():void
    {
        ViewManager.Instance.close( SkinPanel );
    }

    public destory():void
    {
        this._isdestorys = true;
        this.ClearCharacter();
        if( this._uicarma3D )
        {
            this._uicarma3D.destroy();
            this._uicarma3D = null;
        }
        if( this._ui3DScene )
        {
            this._ui3DScene.destroy();
            this._ui3DScene = null;
        }
        super.destory();
    }

    private onchange():void
    {
        if( this.c_title.selectedIndex == 0 )
        {
            this.list_0.numItems = this.agencyskin.pifuArr.length;
        }
        else
        {
            this.list_0.numItems = 0;
        }
    }

    private itemlistrender( index:number,item:SkinItem ):void
    {
        if( this.c_title.selectedIndex == 0 )
        {
            if( index < this.agencyskin.pifuArr.length )
            {
                item.showData( this.agencyskin.pifuArr[index] );
            }
        }
    }

    private updateroleHandler():void
    {
        var RoleData:RoleData = this.agencyskin.RoleData;
        this.ClearCharacter();
        var pathArr:Array<any> = [];
        pathArr.push( { url:AssetPath.get3DRoleModel( RoleData.roleCfg.name )} );
        ResourceControl.Instance.loader3DArr( pathArr,this,this.loadcomplete,null,true,RoleData.roleCfg.name );
    }

    private _out:Laya.Quaternion = new Laya.Quaternion;
    private loadcomplete( resousename:string ):void
    {
        var RoleData:RoleData = this.agencyskin.RoleData;
        if( !this._isdestorys && this._ui3DScene && resousename == RoleData.roleCfg.name )
        {
            this._rolemo = CharacterPool.AddCharacter( resousename,Player,this._ui3DScene );
            this._rolemo.transform.position = new Laya.Vector3( 0,2,0 );
            Laya.Quaternion.createFromAxisAngle( new Laya.Vector3( 0,1,0 ),-135*180/Math.PI,this._out );
            this._rolemo.transform.rotation = this._out;
            ( this._rolemo.getComponent( Player ) as Player ).PlayAnimator( CharacterState.stand );
        }
    }

   

}