//--------------------------------------------------------------------------------
// Framework - CopyRight 2021
//--------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using UnityEngine;
using UnityEngine.XR;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.EventSystems;

using omFrame;
using omFrame.Struct;
using omFrame.Static;

using Autohand;
using FTS;
using FTS.Device;
using FTS.Component;
using omFrame.Network.TCP.Packet;
using omFrame.Static;
#if UNITY_EDITOR
using UnityEditor;
#endif

// 항공 소방 - 전투기 테일 후크 훈련
public class omMap_TailHook : omIMap
{
    [SerializeField] protected bool bDebugDialog = false;
    // Member Value
    //-----------------------------------------------------------------------------
    [SerializeField] protected List<omISpawnPos> m_SpawnPos = new List<omISpawnPos>();
    public omFOV omfov;
    public omTarget omtarget;
    bool m_singleCheck = false;             //싱글인지 체킹

    // Member Function
    //-----------------------------------------------------------------------------
    public bool JustOnePlay = false;

    [Header("StartScenarioFlow Set")]
    public Camera m_F16TailHookCam;
    public Camera m_T50TailHookCam; 
    public Camera cableCam;
    //public GameObject F35Manager;
    public GameObject F16Manager;
    public GameObject T50Manager;
    public GameObject StartPointSet;
    public GameObject fireFighting;

    [Header("Game Start Set")]
    public bool F16Start = false;
    public bool T50Start = false;
    public bool ForDebugSkip = false;
    IEnumerator SkipCoroutine;
    Coroutine skipCor;

    omPlayerOwner m_omPlayerOwner;

    // 오브젝트 생성
    public override void Initialize()
    {
        omLog.LogChannel(eLogChannel.Flow, string.Format("levelARF_TailHook::Initialize()"));
        jsLoginAccountRes accoutInfo = null;
        if (omFrameConst.systemInit)
        {
            accoutInfo = omFrameWork.Inst.Cache.CacheNet.GetMyIDInfo();
        }
        List<UserSlotInfo> userInfoList = new List<UserSlotInfo>();
        if (omFrameConst.systemInit)
        {
            userInfoList.AddRange(omFrameWork.Inst.Cache.CacheNet.currUserCacheList);
        }
        int slotIndex = 0;

        foreach (var spawnpos in m_SpawnPos)
        {
            switch (spawnpos.SpawnType)
            {
                // Player 스폰위치에 캐릭터 생성. 
                case eSpawnType.SpawnT_Character:
                    omSpawnCharacter spawnCharacter = spawnpos as omSpawnCharacter;
                    if (spawnCharacter != null)
                    {
                        if (m_singleCheck)
                            break;

                        // 멀티
                        if (userInfoList.Count > 0)
                        {
                            if (slotIndex < userInfoList.Count)
                            {
                                // 내아이디일경우
                                if (userInfoList[slotIndex].userID == accoutInfo.uid)
                                {
                                    // 플레이어 생성
                                    omIPlayer owner = omFrameWork.Inst.Actor.CreatePlayer(new omCachePlayer() { uniqueid = (ulong)userInfoList[slotIndex].userID, table_control_id = 1400, table_spawn_id = 1102 },
                                        null,
                                        spawnCharacter.transform.position,
                                        spawnCharacter.transform.rotation);

                                    // 네트워크 킴
                                    if (owner != null)
                                    {
                                        omFrameWork.Inst.Actor.myPlayer = owner;
                                        owner.GetComponent<omPlayerOwner>().SetNetworkSync(true);
                                    }

                                }
                                //아더일경우
                                else
                                {
                                    // 임시로 적용함. (네트워크 테스트 진행중) >> 1110 , 1103
                                    omIPlayer other = omFrameWork.Inst.Actor.CreatePlayer(new omCachePlayer() { uniqueid = (ulong)userInfoList[slotIndex].userID, table_control_id = 1110, table_spawn_id = 1102 },
                                        null,
                                        spawnCharacter.transform.position,
                                        spawnCharacter.transform.rotation);
                                }
                            }
                            slotIndex++;
                        }
                        //  싱글
                        else
                        {
                            omIPlayer owner = omFrameWork.Inst.Actor.CreatePlayer(new omCachePlayer() { table_control_id = 1400, table_spawn_id = 1102 },
                            null,
                            spawnCharacter.transform.position,
                            spawnCharacter.transform.rotation);
                            m_singleCheck = true;

                            omFrameWork.Inst.Actor.myPlayer = owner;
                        }
                    }
                    break;

                // 스폰위치에 오브젝트 생성. 
                case eSpawnType.SpawnT_Object:
                    omSpawnObject spawnObject = spawnpos as omSpawnObject;
                    if (spawnObject != null)
                    {
                        // Object 스폰 위치일 경우, 오브젝트 생성.
                        if (spawnObject.ObjectType == eSpawnObject.SpawnObject_Dron)
                        {
                            // 드론 생성(인터렉션 오브젝트)
                            omFrameWork.Inst.Actor.CreateInteractionObj(new omCacheInteractionObj() { table_id = 1300 },
                                null,
                                spawnObject.transform.position,
                                spawnObject.transform.rotation);
                        }
                        else if (spawnObject.ObjectType == eSpawnObject.SpawnObject_Test)
                        {
                            // 오브젝트 생성(기본 오브젝트)
                            omFrameWork.Inst.Actor.CreateObj(new omCacheObj() { table_id = 1300 },
                                null,
                                spawnObject.transform.position,
                                spawnObject.transform.rotation);
                        }
                        // LandingSet 생성
                        if (spawnObject.ObjectType == eSpawnObject.SpawnObject_LandingSet)
                        {
                            omIObject landingSet = omFrameWork.Inst.Actor.CreateObj(new omCacheObj() { table_id = 1411 },
                                null,
                                spawnObject.transform.position,
                                spawnObject.transform.rotation);
                            Debug.Log(landingSet);
                        }
                        // Net Test
                        if (spawnObject.ObjectType == eSpawnObject.SpawnObject_NetTest)
                        {
                            omNetObject NetTest = omFrameWork.Inst.Actor.CreateNetworkObj(new omCacheObj() { table_id = 1499 },
                                null,
                                spawnObject.transform.position,
                                spawnObject.transform.rotation);
                            Debug.Log(NetTest);
                        }
                    }
                    break;
                // 스폰위치에 UI 생성. 
                case eSpawnType.SpawnT_UI:
                    break;
            }
        }
    }
    public override void Terminate()
    {
        omLog.LogChannel(eLogChannel.InLevel, string.Format("levelARF_TailHook::Terminate()"));
    }
    public override bool ValidCheck() { return true; }

    public override Camera GetCamera() { return Camera.main; }

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
            ForSkipSystem();

        if (bDebugDialog)
            Debuging_Dialog();
    }
    void Start()
    {
        // StartCoroutine(StartScenarioFlow());
        if (!ForDebugSkip)
            skipCor = StartCoroutine("StartScenarioFlow");

    }

    void ForSkipSystem()
    {
        ForDebugSkip = true;
        StopCoroutine(skipCor);

        StartPointSet.gameObject.SetActive(true);
        omFrameWork.Inst.Dialog.GetWSHud?.SetCameraFollow(Camera.main);
        omFrameWork.Inst.Dialog.GetWSHud?.ResetAllHudInfo(false);
        omFrameWork.Inst.Dialog.GetWSHud.SetInfoText(15030, true);
    }
    // TailHook 시나리오 진행
    IEnumerator StartScenarioFlow()
    {

        // 시작시 실행되는 연출 텍스트
        yield return new WaitForSeconds(3f);
        omFrameWork.Inst.Dialog.GetWSHud?.SetCameraFollow(Camera.main);
        omFrameWork.Inst.Dialog.GetWSHud?.ResetAllHudInfo(false);
        omFrameWork.Inst.Dialog.GetWSHud.SetInfoText(15011, true);          // 연출 1 text
                                                                            // F-35 비행 시작
        omFrameWork.Inst.Actor.FindObjType<omIObject>(eObjectType.ActObjT_F35);
        omFrameWork.Inst.Actor.CreateObj(new omCacheObj() { table_id = 1412 }, null, null, null, true);

        // 시간 지난 후 텍스트 
        yield return new WaitForSeconds(10f);
        // 진동 시작
        //F35VibrateStart();
        omFrameWork.Inst.Dialog.GetWSHud.SetInfoText(15012, true);

        // 진동 끔
        yield return new WaitForSeconds(8f);
        //F35VibrateEnd();

        yield return new WaitForSeconds(4f);

        // 각 비행 훈련시 세팅
        if (F16Start == true)
        {
            // F-16 시작 
            F16Manager.SetActive(true);
            omFrameWork.Inst.Dialog.GetWSHud.OpenRightEventViewInTime(m_F16TailHookCam, 30f);
        }
        else if (T50Start == true)
        {
            // T-50 시작
            T50Manager.SetActive(true);
            omFrameWork.Inst.Dialog.GetWSHud.OpenRightEventViewInTime(m_T50TailHookCam, 30f);
        }

        // 캠표시 - > 케이블 인게이지 쪽
        omFrameWork.Inst.Dialog.GetWSHud.OpenLeftEventViewInTime(cableCam, 26f, 4f);
        cableCam.gameObject.SetActive(true);

        // 항공기 착지가 완료되고 마지막 텍스트
        yield return new WaitForSeconds(28f);
        omFrameWork.Inst.Dialog.GetWSHud.SetInfoText(15013, true);

        // Fade UI
        yield return new WaitForSeconds(6f);
        omFrameWork.Inst.Dialog.GetWSHud.FadeOutInUI(4, 4, 4, 4);

        // 재배치, 기둥 형성, obj Destroy
        yield return new WaitForSeconds(10f);
        GameObject.FindObjectOfType<omObjectJetFighter>().transform.position = new Vector3(-335f, -0.26f, 42f);
        GameObject.Destroy(fireFighting);
        StartPointSet.gameObject.SetActive(true);
    }

     #region F35 진동 설정
    void F35VibrateStart()
    {
        // 조끼 진동 설정
        Bhaptics_Setup.instance.VestManager.PlayVibrate(HapticVestType.All, 50, 3);
        Bhaptics_Setup.instance.VestManager.PlayVibrate(HapticVestType.All, 70, 3);
        Bhaptics_Setup.instance.VestManager.PlayVibrate(HapticVestType.All, 100, 3);
        Bhaptics_Setup.instance.VestManager.PlayVibrate(HapticVestType.All, 50, 3);

        // 장갑 진동 설정
        omFrameWork.Inst.VR.StartVibrateFinger(FTS.HandType.Left, VibrateFingerType.All, 3);
        omFrameWork.Inst.VR.StartVibrateFinger(FTS.HandType.Right, VibrateFingerType.All, 3);

        omFrameWork.Inst.VR.StartVibrateFinger(FTS.HandType.Left, VibrateFingerType.All, 5);
        omFrameWork.Inst.VR.StartVibrateFinger(FTS.HandType.Right, VibrateFingerType.All, 5);

        omFrameWork.Inst.VR.StartVibrateFinger(FTS.HandType.Left, VibrateFingerType.All, 7);
        omFrameWork.Inst.VR.StartVibrateFinger(FTS.HandType.Right, VibrateFingerType.All, 7);

        omFrameWork.Inst.VR.StartVibrateFinger(FTS.HandType.Left, VibrateFingerType.All, 5);
        omFrameWork.Inst.VR.StartVibrateFinger(FTS.HandType.Right, VibrateFingerType.All, 5);
    }
    void F35VibrateEnd()
    {
        // 조끼 진동 끔
        Bhaptics_Setup.instance.VestManager.StopVibrate();

        // 장갑 진동 끔
        omFrameWork.Inst.VR.StopVibrateFinger(FTS.HandType.Left, VibrateFingerType.All);
        omFrameWork.Inst.VR.StopVibrateFinger(FTS.HandType.Right, VibrateFingerType.All);

    }
    #endregion

    #region 다이어로그 관련
    public void Debuging_Dialog()
    {
        // 1~9 숫자키 눌러 다이얼로그 테스트
        if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Alpha1))
        {
            omLog.LogChannel(eLogChannel.Framework, string.Format("Debuging_Dialog > HudBase"));

            //허드 테스트
            //omFrameWork.Inst.Dialog.GetWSHud.StartAllHudInfo();
            //omFrameWork.Inst.Dialog.GetWSHud.SetTitleText(omUIHelper.TestGetScriptTableID());
            //페이드 인아웃
            //omFrameWork.Inst.Dialog.GetWSHud.SetCameraFollow(camera);
            omFrameWork.Inst.Dialog.GetWSHud.FadeOutInUI(1, 1, 1, 1);
            //tts테스트
            //omFrameWork.Inst.Dialog.GetWSHud.SetTitleText(7002, true);
            //omFrameWork.Inst.Dialog.GetWSHud.SetInfoText(7005, true);
            //프로그래스바 테스트
            //omFrameWork.Inst.Dialog.GetWSHud.OpenProgressBar();
        }
        else if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Alpha2))
        {
            omLog.LogChannel(eLogChannel.Framework, string.Format("Debuging_Dialog > Marker"));

            //마커 테스트
            //omFrameWork.Inst.Dialog.GetWSMarker.OpenMaker(MarkerViewType.IMAGE, omUIHelper.TestGetSpriteTableID(), Vector3.zero, 200, 200, Quaternion.identity);
        }
        else if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Alpha3))
        {
            omLog.LogChannel(eLogChannel.Framework, string.Format("Debuging_Dialog > HandTouch"));

            //핸드터치 테스트
            // omFrameWork.Inst.Dialog.GetWSHandTouch.OpenTouch(omUIHelper.TestGetSpriteTableID(), Vector3.zero, 350, 350, Quaternion.identity, null);
        }
        else if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Alpha4))
        {
            omLog.LogChannel(eLogChannel.Framework, string.Format("Debuging_Dialog > ProgressBar"));

            //오브젝트 프로그래스바 테스트
            omFrameWork.Inst.Dialog.GetWSProgressBar.OpenProgressBar(Vector3.zero, 300, 150, Quaternion.identity, 100);
        }
        else if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Alpha5))
        {
            omLog.LogChannel(eLogChannel.Framework, string.Format("Debuging_Dialog > HudEmoji"));

            //허드 이모지 테스트
            omFrameWork.Inst.Dialog.GetWSHud.OpenInfoAni(HudEmojiSignType.Motion2, 0.5f);
        }
        else if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Alpha6))
        {
            omLog.LogChannel(eLogChannel.Framework, string.Format("Debuging_Dialog > Alpha6"));
        }
        else if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Alpha7))
        {
            omLog.LogChannel(eLogChannel.Framework, string.Format("Debuging_Dialog > Alpha7"));
        }
        else if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Alpha8))
        {
            omLog.LogChannel(eLogChannel.Framework, string.Format("Debuging_Dialog > Alpha8"));
        }
        else if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Alpha9))
        {
            omLog.LogChannel(eLogChannel.Framework, string.Format("Debuging_Dialog > Alpha9"));
        }
    }
    #endregion
}