//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using GameFramework;
using GameFramework.Event;
using UnityEngine;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace MetaClient
{
    public class ProcedureCustomRole : ProcedureBase
    {

        private bool m_SelectComplete = false;

        private CustomRoleForm m_SkinForm = null;

        private Transform m_PreviewPos = null;

        private bool m_StartGame = false;
        private string m_NextSceneName = "";
        private GameMode m_NextGameMode;

        public override bool UseNativeDialog
        {
            get
            {
                return false;
            }
        }

        private GameObject m_CurPreviewShow = null;

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);
            m_SelectComplete = false;
            Log.Info("开始捏角色!");
            CustomManager.Instance.Init();

            GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess);
            GameEntry.UI.OpenUIForm(UIFormId.CustomRole, this);
        }

        protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
        {
            base.OnLeave(procedureOwner, isShutdown);
            
            GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess);

            if (m_SkinForm != null)
            {
                m_SkinForm.Close(true);
                m_SkinForm = null;
            }
        }

        public void StartSingleGame()
        {

        }

        public void StartMultiGame()
        {

        }

        public void StartMiniGame()
        {

        }

        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);

            if (m_SelectComplete && m_StartGame && m_NextSceneName != "")
            {
                procedureOwner.SetData<VarByte>("GameMode", (byte)m_NextGameMode);
                procedureOwner.SetData<VarString>("NextScene", m_NextSceneName);
                ChangeState<ProcedureChangeScene>(procedureOwner);
            }
        }

        private void OnOpenUIFormSuccess(object sender, GameEventArgs e)
        {
            OpenUIFormSuccessEventArgs ne = (OpenUIFormSuccessEventArgs)e;
            if (ne.UserData != this)
            {
                return;
            }

            //m_SkinForm = (CustomRoleForm)ne.UIForm.Logic;
        }
    }
}
