using Kryz.CharacterStats.Examples;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerDataHandler : MonoBehaviour
{
    #region Singleton
    private static PlayerDataHandler _instance;

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

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

    public StringSharedField currentPlayerName;
    public StringSharedField currentCharacterName;
    public StringSharedField currentCharacterClass;

    PlayerAccountData playerAccountData = new PlayerAccountData();
    string playerAccountDataKey;

    CharacterData characterData = new CharacterData();
    string characterDataKey;

    CoinData coinData = new CoinData();
    string playerCoinKey;

    EquipmentData equipmentData = new EquipmentData();
    string equipmentDataKey;

    InventoryData inventoryData = new InventoryData();
    string inventoryDataKey;

    private void Awake()
    {
        DontDestroyOnLoad(this);
    }

    // Player account data (user name, character names list)
    public PlayerAccountData LoadPlayerAccountData(string playerName)
    {
        playerAccountDataKey = GameConstants.PlayerPrefsKeys.GetPlayerAccountDataKey(playerName);

        if (GameStatePersistenceManager.Instance.HasDataForKey(playerAccountDataKey))
        {
            playerAccountData = GameStatePersistenceManager.Instance.LoadData<PlayerAccountData>(playerAccountDataKey);
            return playerAccountData;
        }
        else return null;
    }
    public void SavePlayerAccountData(string playerName, PlayerAccountData playerAccountData)
    {
        playerAccountDataKey = GameConstants.PlayerPrefsKeys.GetPlayerAccountDataKey(playerName);

        GameStatePersistenceManager.Instance.SaveData(playerAccountDataKey, playerAccountData);
    }

    // Character Data (Level, exp etc)
    public CharacterData LoadCharacterData(string playerName, string characterName)
    {
        characterDataKey = GameConstants.PlayerPrefsKeys.GetCharacterDataKey(playerName, characterName);

        if (GameStatePersistenceManager.Instance.HasDataForKey(characterDataKey))
        {
            characterData = GameStatePersistenceManager.Instance.LoadData<CharacterData>(characterDataKey);
            return characterData;
        }
        else return null;
    }
    public void SaveCharacterData(string playerName, string characterName, CharacterData characterData)
    {
        characterDataKey = GameConstants.PlayerPrefsKeys.GetCharacterDataKey(playerName, characterName);

        characterData.characterName = currentCharacterName.Value;
        characterData.characterClass = currentCharacterClass.Value;

        GameStatePersistenceManager.Instance.SaveData(characterDataKey, characterData);
    }

    // Player Coin Data (global coin amount)
    public CoinData LoadPlayerCoinData(string playerName)
    {
        playerCoinKey = GameConstants.PlayerPrefsKeys.GetPlayerCoinKey(playerName);

        if (GameStatePersistenceManager.Instance.HasDataForKey(playerCoinKey))
        {
            coinData = GameStatePersistenceManager.Instance.LoadData<CoinData>(playerCoinKey);
            return coinData;
        }
        else
            return null;
    }
    public void SavePlayerCoinData(string playerName, CoinData coinData)
    {
        playerCoinKey = GameConstants.PlayerPrefsKeys.GetPlayerCoinKey(playerName);

        GameStatePersistenceManager.Instance.SaveData(playerCoinKey, coinData);
    }


    // Character Equipment Data (equipped items)
    public EquipmentData LoadCharacterEquipmentData(string playerName, string characterName)
    {
        equipmentDataKey = GameConstants.PlayerPrefsKeys.GetCharacterEquipmentDataKey(playerName, characterName);

        if (GameStatePersistenceManager.Instance.HasDataForKey(equipmentDataKey))
        {
            equipmentData = GameStatePersistenceManager.Instance.LoadData<EquipmentData>(equipmentDataKey);
            return equipmentData;
        }
        else return null;
    }
    public void SaveCharacterEquipmentData(string playerName, string characterName, EquipmentData equipmentData)
    {
        equipmentDataKey = GameConstants.PlayerPrefsKeys.GetCharacterEquipmentDataKey(playerName, characterName);

        GameStatePersistenceManager.Instance.SaveData(equipmentDataKey, equipmentData);
    }


    // Character Inventory Data (items on inventory)
    public InventoryData LoadCharacterInventoryData(string playerName, string characterName)
    {
        inventoryDataKey = GameConstants.PlayerPrefsKeys.GetCharacterInventoryDataKey(playerName, characterName);

        if (GameStatePersistenceManager.Instance.HasDataForKey(inventoryDataKey))
        {
            inventoryData = GameStatePersistenceManager.Instance.LoadData<InventoryData>(inventoryDataKey);
            return inventoryData;
        }
        else return null;
    }
    public void SaveCharacterInventoryData(string playerName, string characterName, InventoryData inventoryData)
    {
        inventoryDataKey = GameConstants.PlayerPrefsKeys.GetCharacterInventoryDataKey(playerName, characterName);

        GameStatePersistenceManager.Instance.SaveData(inventoryDataKey, inventoryData);
    }
}
