using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public enum EnemyType
{
    ENEMYDUMB,
    ENEMYCOWARD,
    ENEMYCHASE,
    ENEMYDROP
}

public class GameManager : MonoBehaviour
{
    private static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            return _instance;
        }
    }

    // Dictionary to hold the enemy counts and type
    public Dictionary<EnemyType, Dictionary<int, int>> enemyList = new Dictionary<EnemyType, Dictionary<int, int>>();

    public AudioMixer currentAudioMixer;

    public bool godMode = false;

    public int maxLives = 3;
    public int lives = 3;

    public bool controller;

    public int firstLevel = 0;
    public int level = 0;

    public int blackHoleSize = 0;

    public int startCores = 0;
    private int _coreCount = 0;
    public int coreNeeded = 5;

    public bool boss = false;
    public bool isGameOver = false;

    private void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            _instance = this;
            DontDestroyOnLoad(gameObject);
        }
    }

    private void Start()
    {        
        // Load the saved audio levels into the mixer
        if (PlayerPrefs.HasKey("currentMasterVol"))
        {
            float currentMasterVol = PlayerPrefs.GetFloat("currentMasterVol");
            float currentMusicVol = PlayerPrefs.GetFloat("currentMusicVol");
            float currentSFXVol = PlayerPrefs.GetFloat("currentSFXVol");

            currentAudioMixer.SetFloat("masterVol", SharedMethods.ConvertToDecibels(currentMasterVol));
            currentAudioMixer.SetFloat("musicVol", SharedMethods.ConvertToDecibels(currentMusicVol));
            currentAudioMixer.SetFloat("sfxVol", SharedMethods.ConvertToDecibels(currentSFXVol));
        }
        else
        {
            float currentMasterVol = 1f;
            float currentMusicVol = 1f;
            float currentSFXVol = 1f;

            currentAudioMixer.SetFloat("masterVol", SharedMethods.ConvertToDecibels(currentMasterVol));
            currentAudioMixer.SetFloat("musicVol", SharedMethods.ConvertToDecibels(currentMusicVol));
            currentAudioMixer.SetFloat("sfxVol", SharedMethods.ConvertToDecibels(currentSFXVol));

            PlayerPrefs.SetFloat("currentMasterVol", currentMasterVol);
            PlayerPrefs.SetFloat("currentMusicVol", currentMusicVol);
            PlayerPrefs.SetFloat("currentSFXVol", currentSFXVol);
        }

    }

    public void SetupLevel()
    {
        lives = maxLives;
        _coreCount = startCores;
        level = firstLevel;
        isGameOver = false;

        if (enemyList.Count == 0)
        {
            // Set up the enemy level and count dictionaries
            Dictionary<int, int> enemyDumbSetup = new Dictionary<int, int>();
            Dictionary<int, int> enemyCowardSetup = new Dictionary<int, int>();
            Dictionary<int, int> enemyChaseSetup = new Dictionary<int, int>();
            Dictionary<int, int> enemyDropSetup = new Dictionary<int, int>();

            // Level 1 numbers
            enemyDumbSetup.Add(0, 6);
            enemyCowardSetup.Add(0, 0);
            enemyChaseSetup.Add(0, 0);
            enemyDropSetup.Add(0, 0);

            // Level 2 numbers
            enemyDumbSetup.Add(1, 8);
            enemyCowardSetup.Add(1, 2);
            enemyChaseSetup.Add(1, 0);
            enemyDropSetup.Add(1, 0);

            // Level 3 numbers
            enemyDumbSetup.Add(2, 8);
            enemyCowardSetup.Add(2, 3);
            enemyChaseSetup.Add(2, 1);
            enemyDropSetup.Add(2, 0);

            // Level 4 numbers
            enemyDumbSetup.Add(3, 10);
            enemyCowardSetup.Add(3, 3);
            enemyChaseSetup.Add(3, 2);
            enemyDropSetup.Add(3, 1);

            // Add the counts to the enemy type dictionaries
            enemyList.Add(EnemyType.ENEMYDUMB, enemyDumbSetup);
            enemyList.Add(EnemyType.ENEMYCOWARD, enemyCowardSetup);
            enemyList.Add(EnemyType.ENEMYCHASE, enemyChaseSetup);
            enemyList.Add(EnemyType.ENEMYDROP, enemyDropSetup);
        }
    }

    public int GetCurrentLevelEnemyTypeCount(EnemyType enemyType)
    {
        int result;
        // Check if the current level is higher than the max we have set up (we should probs cap it)
        if (level > enemyList[enemyType].Count - 1)
        {
            // Get the "last" levels numbers
            result = enemyList[enemyType][enemyList[enemyType].Count - 1];
        }
        else
        {
            // Get the current levels numbers
            result = enemyList[enemyType][level];
        }

        return result;
    }

    // Update is called once per frame
    void Update()
    {
        if(_coreCount >= coreNeeded)
        {            
            _coreCount = 0;
            level++;
        }
        BadGuyCount();
    }
    
    public void toggleBike()
    {
        if (controller == true)
        {
            controller = false;
        }
        else
        {
            controller = true;
        }
    }

    public int getCoreCount()
    {
        return _coreCount;
    }

    // This is dealt with in the enemy dictionary
    private void BadGuyCount()
    {
        switch (level)
        {
            case 0:                
                break;
            case 1:                
                break;
            case 2:                
                break;
            case 3:                
                break;
            case 4:
                //Temp until boss added, then add new case 5: with DoGameWon(); and uncomment boss = true;
                if (!isGameOver)
                {
                    DoGameWon();
                }
                //boss = true;
                break;
        }
    }
    public void LostLife()
    {
        lives -= 1;
        if (lives <= 0)
        {
            DoGameOver();
        }
    }

    public int GetLives()
    {
        return lives;
    }

    public void AddCore()
    {
        _coreCount += 1;
    }

    public void AddLife()
    {
        lives += 1;
    }
    public void DoGameOver()
    {
        isGameOver = true;        
        SceneManager.LoadScene("Dead");
    }

    public void DoGameWon()
    {        
        isGameOver = true;
        SceneManager.LoadScene("Win");
    }
}
