using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterSoundFXManager : MonoBehaviour
{
    private AudioSource audioSource;
    [SerializeField]
    private AudioClip dropSound, punchSound, kickSound, whooshSound, blockSound, screamSound, punchFaceHitSound, punchBodyHitSound;

    private void Awake()
    {
        audioSource = GetComponent<AudioSource>();
        if (audioSource == null)
        {
            Debug.LogError("AudioSource component missing on " + gameObject.name + ". Please add an AudioSource.");
        }
        // Warn if any AudioClip is not assigned
        if (dropSound == null) Debug.LogWarning("dropSound AudioClip not assigned in CharacterSoundFXManager on " + gameObject.name);
        if (punchSound == null) Debug.LogWarning("punchSound AudioClip not assigned in CharacterSoundFXManager on " + gameObject.name);
        if (kickSound == null) Debug.LogWarning("kickSound AudioClip not assigned in CharacterSoundFXManager on " + gameObject.name);
        if (whooshSound == null) Debug.LogWarning("whooshSound AudioClip not assigned in CharacterSoundFXManager on " + gameObject.name);
        if (blockSound == null) Debug.LogWarning("blockSound AudioClip not assigned in CharacterSoundFXManager on " + gameObject.name);
        if (screamSound == null) Debug.LogWarning("screamSound AudioClip not assigned in CharacterSoundFXManager on " + gameObject.name);
        if (punchFaceHitSound == null) Debug.LogWarning("punchFaceHitSound AudioClip not assigned in CharacterSoundFXManager on " + gameObject.name);
        if (punchBodyHitSound == null) Debug.LogWarning("punchBodyHitSound AudioClip not assigned in CharacterSoundFXManager on " + gameObject.name);
    }

    public void PlayHitSound()
    {
        audioSource.clip = dropSound;
        setVolumeValue();
        audioSource.Play();
        Debug.Log("Playing Hit Sound");
    }

    public void PlayPunchLightHitSound()
    {
        audioSource.clip = punchSound;
        setVolumeValue();
        audioSource.Play();
        Debug.Log("Playing Punch Light Hit Sound");
    }
    public void PlayPunchFaceHitSound()
    {
        audioSource.clip = punchFaceHitSound;
        setVolumeValue();
        audioSource.Play();
        Debug.Log("Playing Punch Face Hit Sound");
    }
    public void PlayBodyHitSound()
    {
        audioSource.clip = punchBodyHitSound;
        setVolumeValue();
        audioSource.Play();
        Debug.Log("Playing Body Hit Sound");
    }
    public void PlayKickSound()
    {
        audioSource.clip = kickSound;
        setVolumeValue();
        audioSource.Play();
        Debug.Log("Playing Kick Sound");
    }

    public void PlayWhooshSound()
    {
        audioSource.clip = whooshSound;
        setVolumeValue();
        audioSource.Play();
        Debug.Log("Playing Whoosh Sound");
    }

    public void PlayBlockSound()
    {
        audioSource.clip = blockSound;
        setVolumeValue();
        audioSource.Play();
        Debug.Log("Playing Block Sound");
    }

    public void PlayScreamSound()
    {
        audioSource.clip = screamSound;
        setVolumeValue();
        audioSource.Play();
        Debug.Log("Playing Scream Sound");
    }

    private float volumeValue;
    private GameSettings gameSettings;
    void setVolumeValue(){
        gameSettings = LoadGameSettings();
        volumeValue = gameSettings.SFXVolume_value;
        if (volumeValue <= 0f) volumeValue = 1f;
        audioSource.volume = volumeValue;
        Debug.Log($"[CharacterSoundFXManager] Loaded SFXVolume_value: {gameSettings.SFXVolume_value}, Applied: {audioSource.volume}");
    }

    //Saving and Loading Game Settings
    #region
        private const string GameSettingsKey = "GameSettings";

        public static void SaveGameSettings(GameSettings settings){
            string json = JsonUtility.ToJson(settings);
            PlayerPrefs.SetString(GameSettingsKey, json);
            PlayerPrefs.Save();
        }

        public static GameSettings LoadGameSettings(){
            if (PlayerPrefs.HasKey(GameSettingsKey))
            {
                string json = PlayerPrefs.GetString(GameSettingsKey);
                return JsonUtility.FromJson<GameSettings>(json);
            }
            return new GameSettings(); // Return default settings if none are saved
        }

    #endregion
}
