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

public class Player : MonoBehaviour
{
    public int activeBubble;
    public int bubbleChoice;
    public float radius;
    public Material theMaterial;
    public Material[] myMaterial;
    public float delay;
    public Slider slider;
    public GameObject myBottle;
    public Shoot shootScript;
    public float myForceBuild;
    public float minForce;
    public float maxForce;
    public float normForce;
    public GameObject highlight1;
    public GameObject highlight2;
    public GameObject highlight3;

    public AudioSource audioSource;
    public AudioClip boomSound;

    //public GameObject[] cannonList;
    //public int nextCannon;

    public int bank;
    public TextMeshProUGUI bankText;
    public TextMeshProUGUI priceStandardText;
    public TextMeshProUGUI priceBubbaText;
    public TextMeshProUGUI priceMeteorText;
    public int cost;
    public bool isLoaded;

   
    // Start is called before the first frame update
    void Start()
    {
        activeBubble = 1;
        bubbleChoice = 1;
        shootScript = myBottle.GetComponent<Shoot>();
        isLoaded = true;
        minForce = 0f;
        maxForce = 15f;
        highlight1.SetActive(true);
        highlight2.SetActive(false);
        highlight3.SetActive(false);

        audioSource = GetComponent<AudioSource>();

        bank = 100;
        bankText.text = bank.ToString();
        cost = int.Parse(priceStandardText.text);

        //nextCannon = 0;
        //cannonList[nextCannon].SetActive(true);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W) && gameObject.tag == "left")
        {
            activeBubble = activeBubble + 1;
            if (activeBubble > 3)
            {
                activeBubble = 1;
            }
        }

        if (Input.GetKeyDown(KeyCode.O) && gameObject.tag == "right")
        {
            activeBubble = activeBubble + 1;
            if (activeBubble > 3)
            {
                activeBubble = 1;
            }
        }

        switch (activeBubble)
        {
            case 1:
                cost = int.Parse(priceStandardText.text);
                
                if (bank - cost > 0)
                {
                    highlight1.SetActive(true);
                    highlight2.SetActive(false);
                    highlight3.SetActive(false);

                    if(Input.GetKeyDown (KeyCode.Q) && gameObject.tag == "left")
                    {
                        bank = bank - cost;
                        bankText.text = bank.ToString();
                        bubbleChoice = activeBubble;
                        isLoaded = true;
                    }

                    if (Input.GetKeyDown(KeyCode.P) && gameObject.tag == "right")
                    {
                        bank = bank - cost;
                        bankText.text = bank.ToString();
                        bubbleChoice = activeBubble;
                        isLoaded = true;
                    }
                }
                break;

            case 2:
                cost = int.Parse(priceBubbaText.text);

                if (bank - cost > 0)
                {
                    highlight1.SetActive(false);
                    highlight2.SetActive(true);
                    highlight3.SetActive(false);

                    if (Input.GetKeyDown(KeyCode.Q) && gameObject.tag == "left")
                    {
                        bank = bank - cost;
                        bankText.text = bank.ToString();
                        bubbleChoice = activeBubble;
                        isLoaded = true;
                    }

                    if (Input.GetKeyDown(KeyCode.P) && gameObject.tag == "right")
                    {
                        bank = bank - cost;
                        bankText.text = bank.ToString();
                        bubbleChoice = activeBubble;
                        isLoaded = true;
                    }
                }
                break;

                case 3:
                cost = int.Parse(priceMeteorText.text);

                if (bank - cost > 0)
                {
                    highlight1.SetActive(false);
                    highlight2.SetActive(false);
                    highlight3.SetActive(true);

                    if (Input.GetKeyDown(KeyCode.Q) && gameObject.tag == "left")
                    {
                        bank = bank - cost;
                        bankText.text = bank.ToString();
                        bubbleChoice = activeBubble;
                        isLoaded = true;
                    }

                    if (Input.GetKeyDown(KeyCode.P) && gameObject.tag == "right")
                    {
                        bank = bank - cost;
                        bankText.text = bank.ToString();
                        bubbleChoice = activeBubble;
                        isLoaded = true;
                    }
                }
                break;
        }

        switch (bubbleChoice)
        {
            case 1:

                radius = 0.5f;
                theMaterial = myMaterial[0];
                delay = 0f;
                myForceBuild = shootScript.forceBuild;
                //highlight1.SetActive (true);
                //highlight2.SetActive (false);
                //highlight3.SetActive (false);
                myBottle.GetComponent<Renderer>().material = theMaterial;
                break;
            case 2:
                radius = 2.0f;
                theMaterial = myMaterial[1];
                delay = 0.1f;
                myForceBuild = shootScript.forceBuild;
                /*highlight1.SetActive(false);
                highlight2.SetActive(true);
                highlight3.SetActive(false);*/
                myBottle.GetComponent<Renderer>().material = theMaterial;
                break;
            case 3:
                radius = 0.5f;
                theMaterial = myMaterial[2];
                delay = 0.5f;
                myForceBuild = shootScript.forceBuild;
               /* highlight1.SetActive(false);
                highlight2.SetActive(false);
                highlight3.SetActive(true);*/
                myBottle.GetComponent<Renderer>().material = theMaterial;
                break;
        }

        if(myForceBuild > 15.0f)
        {
            myForceBuild = 15.0f;
        }

        normForce = Mathf.InverseLerp(minForce, maxForce, myForceBuild);
        slider.value = normForce;
    }

    public void PlaySounds()
    {
        audioSource.clip = boomSound;
        audioSource.Play();
    }
}
