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

public class GameDifficultyController : MonoBehaviour, IPunObservable
{
    [Header("Settings:")]
    [SerializeField] private List<UnitDifficultySettings> difficultySettings = new List<UnitDifficultySettings>();

    #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);

        SetDifficultyLevel(0);
    }

    // Start is called before the first frame update
    void Start()
    {
        if (!PhotonNetwork.IsMasterClient) return;

        SetDifficultyLevel(0);
    }

    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 SetDifficultyLevel(int index)
    {
        currentDifficultyIndex = index;
        currentUnitDifficultySettings = difficultySettings[currentDifficultyIndex];
    }
    public void SetDifficultyLevel(UnitDifficultySettings settings)
    {
        currentUnitDifficultySettings = settings;
        currentDifficultyIndex = difficultySettings.IndexOf(currentUnitDifficultySettings);
    }
    public UnitDifficultySettings GetCurrentDifficultySettings()
    {
        return currentUnitDifficultySettings;
    }

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