using System;
using Sources;
using UnityEngine;

namespace Cybe
{
    public class AudioManager : MonoBehaviour
    {
        [SerializeField] private AudioSource _fxSource;
        [SerializeField] private AudioSource _musicSource;
        
        [SerializeField] private AudioClip[] _stepSounds;
        [SerializeField] private AudioClip[] _swordHitSounds;
        [SerializeField] private AudioClip[] _swordSwooshSounds;
        [SerializeField] private AudioClip[] _blasterShootSounds;
        [SerializeField] private AudioClip _bulldozerChargingSound;
        [SerializeField] private AudioClip _bulldozerAttackSound;
        [SerializeField] private AudioClip _portalSound;
        [SerializeField] private AudioClip _ricochetSound;

        [Header("Music")]
        [SerializeField] private AudioClip _mainTheme;
        [SerializeField] private AudioClip _finalTheme;

        public float Volume => _fxSource.volume;

        public void PlayFx(SoundType type)
        {
            switch (type)
            {
                case SoundType.Step:
                    _fxSource.PlayOneShot(_stepSounds.GetRandomElement());
                    break;
                case SoundType.SwordHit:
                    _fxSource.PlayOneShot(_swordHitSounds.GetRandomElement());
                    break;
                case SoundType.SwordSwoosh:
                    _fxSource.PlayOneShot(_swordSwooshSounds.GetRandomElement());
                    break;
                case SoundType.BlasterShot:
                    _fxSource.PlayOneShot(_blasterShootSounds.GetRandomElement());
                    break;
                case SoundType.BulldozerCharging:
                    _fxSource.PlayOneShot(_bulldozerChargingSound);
                    break;
                case SoundType.BulldozerAttack:
                    _fxSource.PlayOneShot(_bulldozerAttackSound);
                    break;
                case SoundType.Portal:
                    _fxSource.PlayOneShot(_portalSound);
                    break;
                case SoundType.Ricochet:
                    _fxSource.PlayOneShot(_ricochetSound);
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
        
        public void PlayMusic(MusicType type)
        {
            switch (type)
            {
                case MusicType.MainTheme:
                    _musicSource.clip = _mainTheme;
                    break;
                case MusicType.FinalTheme:
                    _musicSource.clip = _finalTheme;
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
            
            _musicSource.Play();
        }

        public void SetVolume(float volume)
        {
            _fxSource.volume = volume;
            _musicSource.volume = volume * 0.8f;
        }
    }
}