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

public class UIManager : MonoBehaviour
{
    public SelectedPanel selectedPanel;

    [SerializeField] GameObject scrapPanel;
    [SerializeField] GameObject waterPanel;
    [SerializeField] GameObject soapPanel;
    [SerializeField] GameObject machinePanel;
    [SerializeField] GameObject barragePanel;
    [SerializeField] GameObject blastPanel;
    [SerializeField] GameObject bathPanel;

    [SerializeField] Color selectedColor;
    [SerializeField] Color defaultColor;

    private Image scrapImage;
    private Image waterImage;
    private Image soapImage;
    private Image machineImage;
    private Image barrageImage;
    private Image blastImage;
    private Image bathImage;

    [SerializeField] GameObject audioManager;
    void Start()
    { 
        GetImageComponents();
    }

    void GetImageComponents()
    {
        scrapImage = scrapPanel.GetComponent<Image>();
        waterImage = waterPanel.GetComponent<Image>();
        soapImage = soapPanel.GetComponent<Image>();
        machineImage = machinePanel.GetComponent<Image>();
        barrageImage = barragePanel.GetComponent<Image>();
        blastImage = blastPanel.GetComponent<Image>();
        bathImage = bathPanel.GetComponent<Image>();
    }

    void Update()
    {
        CheckInput();
        ChangeColor();
    }
    void CheckInput()
    {
        string decision = "none";
        if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
        {
            decision = "left";
            audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.SWITCHSELECTION);
        }
        else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            decision = "right";
            audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.SWITCHSELECTION);
        }
        if ((Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) && (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)))
        {
            decision = "none";
        }
        CheckCurrentSelection(decision);
        CheckNumbers();
    }
    void CheckCurrentSelection(string decision)
    {
        switch (selectedPanel)
        {
            case SelectedPanel.SCRAP:
                if (decision == "left")
                {
                    selectedPanel = SelectedPanel.BATH;
                }
                if (decision == "right")
                {
                    selectedPanel = SelectedPanel.WATER;
                }
                break;
            case SelectedPanel.WATER:
                if (decision == "left")
                {
                    selectedPanel = SelectedPanel.SCRAP;
                }
                if (decision == "right")
                {
                    selectedPanel = SelectedPanel.SOAP;
                }
                break;
            case SelectedPanel.SOAP:
                if (decision == "left")
                {
                    selectedPanel = SelectedPanel.WATER;
                }
                if (decision == "right")
                {
                    selectedPanel = SelectedPanel.MACHINE;
                }
                break;
            case SelectedPanel.MACHINE:
                if (decision == "left")
                {
                    selectedPanel = SelectedPanel.SOAP;
                }
                if (decision == "right")
                {
                    selectedPanel = SelectedPanel.BARRAGE;
                }
                break;
            case SelectedPanel.BARRAGE:
                if (decision == "left")
                {
                    selectedPanel = SelectedPanel.MACHINE;
                }
                if (decision == "right")
                {
                    selectedPanel = SelectedPanel.BLAST;
                }
                break;
            case SelectedPanel.BLAST:
                if (decision == "left")
                {
                    selectedPanel = SelectedPanel.BARRAGE;
                }
                if (decision == "right")
                {
                    selectedPanel = SelectedPanel.BATH;
                }
                break;
            case SelectedPanel.BATH:
                if (decision == "left")
                {
                    selectedPanel = SelectedPanel.BLAST;
                }
                if (decision == "right")
                {
                    selectedPanel = SelectedPanel.SCRAP;
                }
                break;
            default:
                selectedPanel = SelectedPanel.SCRAP;
                break;
        }
    }
    void ChangeColor()
    {
        switch (selectedPanel)
        {
            case SelectedPanel.SCRAP:
                scrapImage.color = selectedColor;
                waterImage.color = defaultColor;
                soapImage.color = defaultColor;
                machineImage.color = defaultColor;
                barrageImage.color = defaultColor;
                blastImage.color = defaultColor;
                bathImage.color = defaultColor;
                break;
            case SelectedPanel.WATER:
                scrapImage.color = defaultColor;
                waterImage.color = selectedColor;
                soapImage.color = defaultColor;
                machineImage.color = defaultColor;
                barrageImage.color = defaultColor;
                blastImage.color = defaultColor;
                bathImage.color = defaultColor;
                break;
            case SelectedPanel.SOAP:
                scrapImage.color = defaultColor;
                waterImage.color = defaultColor;
                soapImage.color = selectedColor;
                machineImage.color = defaultColor;
                barrageImage.color = defaultColor;
                blastImage.color = defaultColor;
                bathImage.color = defaultColor;
                break;
            case SelectedPanel.MACHINE:
                scrapImage.color = defaultColor;
                waterImage.color = defaultColor;
                soapImage.color = defaultColor;
                machineImage.color = selectedColor;
                barrageImage.color = defaultColor;
                blastImage.color = defaultColor;
                bathImage.color = defaultColor;
                break;
            case SelectedPanel.BARRAGE:
                scrapImage.color = defaultColor;
                waterImage.color = defaultColor;
                soapImage.color = defaultColor;
                machineImage.color = defaultColor;
                barrageImage.color = selectedColor;
                blastImage.color = defaultColor;
                bathImage.color = defaultColor;
                break;
            case SelectedPanel.BLAST:
                scrapImage.color = defaultColor;
                waterImage.color = defaultColor;
                soapImage.color = defaultColor;
                machineImage.color = defaultColor;
                barrageImage.color = defaultColor;
                blastImage.color = selectedColor;
                bathImage.color = defaultColor;
                break;
            case SelectedPanel.BATH:
                scrapImage.color = defaultColor;
                waterImage.color = defaultColor;
                soapImage.color = defaultColor;
                machineImage.color = defaultColor;
                barrageImage.color = defaultColor;
                blastImage.color = defaultColor;
                bathImage.color = selectedColor;
                break;
        }
    }

    public string GetSelection()
    {
        string selection;
        switch (selectedPanel)
        {
            case SelectedPanel.SCRAP:
                selection = "scrap";
                break;
            case SelectedPanel.WATER:
                selection = "water";
                break;
            case SelectedPanel.SOAP:
                selection = "soap";
                break;
            case SelectedPanel.MACHINE:
                selection = "machine";
                break;
            case SelectedPanel.BARRAGE:
                selection = "barrage";
                break;
            case SelectedPanel.BLAST:
                selection = "blast";
                break;
            case SelectedPanel.BATH:
                selection = "bath";
                break;
            default:
                selection = "none";
                break;
        }
        return selection;
    }

    void CheckNumbers()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            selectedPanel = SelectedPanel.SCRAP;
            audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.SWITCHSELECTION);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            selectedPanel = SelectedPanel.WATER;
            audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.SWITCHSELECTION);
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            selectedPanel = SelectedPanel.SOAP;
            audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.SWITCHSELECTION);
        }
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            selectedPanel = SelectedPanel.MACHINE;
            audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.SWITCHSELECTION);
        }
        if (Input.GetKeyDown(KeyCode.Alpha5))
        {
            selectedPanel = SelectedPanel.BARRAGE;
            audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.SWITCHSELECTION);
        }
        if (Input.GetKeyDown(KeyCode.Alpha6))
        {
            selectedPanel = SelectedPanel.BLAST;
            audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.SWITCHSELECTION);
        }
        if (Input.GetKeyDown(KeyCode.Alpha7))
        {
            selectedPanel = SelectedPanel.BATH;
            audioManager.GetComponent<AudioManager>().PlaySound(AudioManager.SoundType.SWITCHSELECTION);
        }
    }

    public enum SelectedPanel
    {
        SCRAP,
        WATER,
        SOAP,
        MACHINE,
        BARRAGE,
        BLAST,
        BATH
    }


}
