using System.CodeDom;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

[CreateAssetMenu(fileName = "PPValue.asset", menuName = "KernelPanic/PlayerPrefValue")]
public class PlayerPrefValue : ScriptableObject
{
    [SerializeField]
    private string KeyName;

    void Awake()
    {
        OnStringSet = new UnityEvent<string>();
        OnIntSet = new UnityEvent<int>();
        OnFloatSet = new UnityEvent<float>();
    }
    
    public UnityEvent<string> OnStringSet { get; private set; }
    public UnityEvent<int> OnIntSet { get; private set; }
    public UnityEvent<float> OnFloatSet { get; private set; }

    public bool HasValue
        => PlayerPrefs.HasKey( KeyName );

    public int GetInt( int defaultValue = int.MaxValue )
        => PlayerPrefs.GetInt( KeyName, defaultValue );
    
    public string GetString( string defaultValue = null )
        => PlayerPrefs.GetString( KeyName, defaultValue );
    
    public float GetFloat( float defaultValue = float.NaN )
        => PlayerPrefs.GetFloat( KeyName, defaultValue );

    public void SetInt( int value )
    {
        PlayerPrefs.SetInt( KeyName, value );
        OnIntSet.Invoke( value );
    }

    public void SetFloat( float value )
    {
        PlayerPrefs.SetFloat( KeyName, value );
        OnFloatSet.Invoke( value );
    }

    public void SetString( string value )
    {
        PlayerPrefs.SetString( KeyName, value );
        OnStringSet.Invoke( value );
    }

    public static void Save() 
        => PlayerPrefs.Save();
}
