﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using com.ootii.Input;
using DarkTonic.MasterAudio;

public class UI : MonoBehaviour
{
    public Snake snake;
    public GameState gameState;

    public Text textSnakeLength;
    public GameObject panelGameOver;

    public GameObject switcherUIElement;
    public Text textSwitcherTimeRemaining;

    public GameObject speedUIElement;
    public Text textSpeed;

    public GameObject minimap;

    [SoundGroupAttribute] public string sndButtonPress;
    [SoundGroupAttribute] public string sndGameOver;


    int minimapEnabled;

    // Use this for initialization
    void Start ()
    {
        if (!PlayerPrefs.HasKey("minimapOn")) PlayerPrefs.SetInt("minimapOn", 1);
        minimapEnabled = PlayerPrefs.GetInt("minimapOn");

        SetMinimap();

        switcherUIElement.SetActive(false);
	    speedUIElement.SetActive(false);
    }

    public void SetMinimap()
    {
        if (minimapEnabled == 1)
        {
            minimap.SetActive(true);
        }
        else if (minimapEnabled == 0)
        {
            minimap.SetActive(false);
        }

        PlayerPrefs.SetInt("minimapOn", minimapEnabled);
    }

    // Update is called once per frame
    void Update()
    {

        if (InputManager.IsJustReleased("Minimap"))
        {
            Debug.Log("Switch on/off Minimap");
            if (minimapEnabled == 1)
            {
                minimapEnabled = 0;
            }
            else if (minimapEnabled == 0)
            {
                minimapEnabled = 1;

            }
            SetMinimap();
        }

        if (gameState.state == GameState.GameStates.Play)
        {
            textSnakeLength.text = snake.snakeLength.ToString();
            speedUIElement.SetActive(true);
            textSpeed.text = snake.controls.moveSpeed.ToString();

            if (snake.switchedControls)
            {
                switcherUIElement.SetActive(true);
                textSwitcherTimeRemaining.text = (snake.switchDuration - snake.switchTimer).ToString();
            }
            else
            {
                switcherUIElement.SetActive(false);
            }

        }
    }

    public void GameStart()
    {
        panelGameOver.SetActive(false);
    }

    public void GameOver()
    {
        MasterAudio.PlaySoundAndForget(sndGameOver);
        panelGameOver.SetActive(true);
        switcherUIElement.SetActive(false);
        speedUIElement.SetActive(false);
    }

    public void OnButtonStartGame()
    {
        PlayButtonSound();
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void OnButtonQuitGame()
    {
        PlayButtonSound();
        Application.Quit();
    }

    public void PlayButtonSound()
    {
        MasterAudio.PlaySoundAndForget(sndButtonPress);
    }
}
