using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameDifficultyController : MonoBehaviour, IPunObservable
{
    [Header("Settings:")]
    [SerializeField] private List<UnitDifficultySettings> difficultySettings = new List<UnitDifficultySettings>();
    [Header("UI")]
    [SerializeField] private Button gameDifficultySettingsButton;
    [SerializeField] private List<GameObject> difficultyCornerInfo = new List<GameObject>();
    [Header("Events:")]
    [SerializeField] private GameEvent onDifficultyChanged;
    [Header("Listeners:")]
    [SerializeField] private GameEventListener onJoinedRoom;

    #region Singleton
    private static GameDifficultyController _instance;

    // Public reference to the singleton instance
    public static GameDifficultyController Instance
    {
        get
        {
            // If the instance doesn't exist, try to find it in the scene
            if (_instance == null)
            {
                _instance = FindObjectOfType<GameDifficultyController>();

                // If it's still null, create a new GameObject and add the component
                if (_instance == null)
                {
                    GameObject singletonObject = new GameObject(typeof(GameDifficultyController).Name);
                    _instance = singletonObject.AddComponent<GameDifficultyController>();
                }
            }
            return _instance;
        }
    }
    #endregion

    int currentDifficultyIndex;
    UnitDifficultySettings currentUnitDifficultySettings;

    // Optional: Implement any methods or properties you need for your singleton

    protected void Awake()
    {
        // Ensure there's only one instance
        if (_instance != null && _instance != this)
        {
            Destroy(gameObject);
            return;
        }

        // If this is the first instance, set it as the singleton
        _instance = this;
        DontDestroyOnLoad(gameObject);

        onJoinedRoom.Response.AddListener(Init);
        InitializeDifficultyLevel();
    }


    private void Init()
    {
        if (!PhotonNetwork.IsMasterClient)
        {
            gameDifficultySettingsButton.interactable = false;
            return;
        }
        gameDifficultySettingsButton.interactable = true;

        InitializeDifficultyLevel();
    }

    private void Update()
    {
        if (!PhotonNetwork.IsMasterClient) return;

        if (Input.GetKeyDown(KeyCode.Keypad0)) // Rank E
        {
            SetDifficultyLevel(0);
        }

        if (Input.GetKeyDown(KeyCode.Keypad1)) // Rank D
        {
            SetDifficultyLevel(1);
        }

        if (Input.GetKeyDown(KeyCode.Keypad2)) // Rank C
        {
            SetDifficultyLevel(2);
        }

        if (Input.GetKeyDown(KeyCode.Keypad3)) // Rank B
        {
            SetDifficultyLevel(3);
        }

        if (Input.GetKeyDown(KeyCode.Keypad4)) // Rank A
        {
            SetDifficultyLevel(4);
        }

        if (Input.GetKeyDown(KeyCode.Keypad5)) // Rank S
        {
            SetDifficultyLevel(5);
        }
    }

    public int GetCurrentDifficultyLevel()
    {
        return (currentDifficultyIndex + 1);
    }

    public void InitializeDifficultyLevel()
    {
        currentDifficultyIndex = 0;
        currentUnitDifficultySettings = difficultySettings[currentDifficultyIndex];
        onDifficultyChanged.Raise();

    }
    public void SetDifficultyLevel(int index)
    {
        if (index == currentDifficultyIndex) return;

        currentDifficultyIndex = index;
        currentUnitDifficultySettings = difficultySettings[currentDifficultyIndex];
        onDifficultyChanged.Raise();
    }
    public void SetDifficultyLevel(UnitDifficultySettings settings)
    {
        currentUnitDifficultySettings = settings;
        currentDifficultyIndex = difficultySettings.IndexOf(currentUnitDifficultySettings);
        onDifficultyChanged.Raise();
    }
    public UnitDifficultySettings GetCurrentDifficultySettings()
    {
        return currentUnitDifficultySettings;
    }

    int receivedDifficulty = 0;
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.IsWriting)
        {
            stream.SendNext(currentDifficultyIndex);
        }
        if (stream.IsReading)
        {
            receivedDifficulty = (int)stream.ReceiveNext();
            if(receivedDifficulty != currentDifficultyIndex)
            {
                currentDifficultyIndex = receivedDifficulty;
                onDifficultyChanged.Raise();
            }
            Debug.Log($"Networked Difficulty: Set to {difficultySettings[currentDifficultyIndex].name}");
        }
    }

    private void ToggleDifficultyCornerInfo(int index)
    {
        difficultyCornerInfo[index].SetActive(true);

        for (int i = 0; i < difficultyCornerInfo.Count; i++)
        {
            if (i == index) continue;
            difficultyCornerInfo[i].SetActive(false);
        }
    }
}
