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

public class AudioManager : MonoBehaviour
{
    public static AudioManager instance { get; private set;}
    void Awake(){
        instance =this;
    }
    [SerializeField]private AudioSource sfxSource;
    [SerializeField]private AudioSource dropSfxSource;
    [SerializeField]private AudioClip[] lowHits, midHits, hardHits;
    [SerializeField]private AudioClip[] drop;
    [SerializeField]private float dropMinPitch = 0.5f;
    [SerializeField]private float dropMaxPitch = 1.5f;
    public void PlaySFX(AudioClip clip){
        sfxSource.PlayOneShot(clip);
    }

    public static void PlaySfx(AudioClip clip){
        instance.PlaySFX(clip);
    }


    public static void HitSfx(float magnitude){
        instance.hitSfx(magnitude);
    }
    public static void DropSfx(float magnitude){
        instance.dropSfx(magnitude);
    }

    void hitSfx(float magnitude){
        AudioClip selectedClip;

        if(magnitude < 0.5f){
            selectedClip = lowHits[Random.Range(0,lowHits.Length)];
        }else if(magnitude < 1){
            selectedClip = midHits[Random.Range(0,midHits.Length)];
        }else{
            selectedClip = hardHits[Random.Range(0,hardHits.Length)];
        }
        Debug.Log("Playing hit sfx for " + magnitude);
        sfxSource.PlayOneShot(selectedClip);
    }

    void dropSfx(float magnitude){
        float diff = dropMaxPitch - dropMinPitch;
        float mult = magnitude / 10f;
        dropSfxSource.pitch = dropMinPitch + (diff * mult);
        dropSfxSource.volume = mult;
        Debug.Log($"Playing drop sfx for { magnitude } : {mult}, pitch: {dropSfxSource.pitch}, volume: {dropSfxSource.volume}");
        dropSfxSource.Play();
    }
}
