using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
using Object = UnityEngine.Object; // InputSystem 사용을 위해 추가

public abstract class BaseScene : MonoBehaviour
{
    public Define.Scene _sceneType { get; protected set; } = Define.Scene.Uknown;
    protected Canvas _canvas;
    protected GameObject _object;
    
    public bool OnPenel = false;
    public GameObject PanelObject = null;

    private void Awake()
    {
        Init();
    }

    protected virtual void Init()
    {
        Object obj = FindFirstObjectByType<EventSystem>();
        if (obj == null)
            Manager.Resource.Instantiate("Prefabs/UI/EventSystem").name = "@EventSystem";
        
        GameObject objects = GameObject.Find("@Object");
        if (objects == null)
        {
            _object = new GameObject("@Object");
        }
        
        GameObject canvas = GameObject.Find("@Canvas");
        if (canvas == null)
        {
            GameObject newCanvas = new GameObject("@Canvas");
            _canvas = newCanvas.AddComponent<Canvas>();
            _canvas.renderMode = RenderMode.ScreenSpaceOverlay;
            
            newCanvas.AddComponent<CanvasScaler>();
            //Canvas Scaler 의 ui Scale Mode을 Scale With Screen Size로 설정
            CanvasScaler canvasScaler = newCanvas.GetComponent<CanvasScaler>();
            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
            canvasScaler.referenceResolution = new Vector2(1920, 1080);
            canvasScaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
            canvasScaler.matchWidthOrHeight = 0.5f; //0: Width,
            
            newCanvas.AddComponent<GraphicRaycaster>();
        }
        else
        {
            _canvas = canvas.GetComponent<Canvas>();
        }
            
        //
        // Manager.Input.InputAction -= Check;
        // Manager.Input.InputAction += Check;
    }

    // private void Check(Define.InputEvent evt)
    // {
        // switch (evt)
        // {
        //     case Define.InputEvent.Click:
        //         Debug.Log($"Clicked Position:"); //{Manager.Input.LastPointPosition}");
        //         break;
        //     default:
        //         Manager.Input.PointAction -= DebugLog;
        //         break;
        // }
    // }

    // public void DebugLog(Vector2 vec)
    // {
    //     Debug.Log(vec);
    // }

    public abstract void Clear();
    
    
    public void OnMenu()
    {
        UIPanel panel = Manager.UI.SwitchOnObject<UIPanel>("Panel", true);
        if (panel != null)
        {
            OnPenel = true;
            PanelObject = panel.gameObject;
            PanelObject.GetComponent<Image>().color = new Color(0, 0, 0, 0.3f);
            PanelObject.transform.SetAsLastSibling();
            Manager.System.PauseSystem();

            // 패널 클릭 이벤트에 OffMenu 연결
            panel.OnPanelClicked -= OffMenu;
            panel.OnPanelClicked += OffMenu;
        }
    }


    public virtual void OffMenu()
    {
        if (OnPenel)
        {
            Manager.UI.SwitchOnObject<UIPanel>("Panel", false);
            Manager.System.ResumeSystem();
            PanelObject = null;
            OnPenel = false;
        }
    }
}
