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

public class P1control : MonoBehaviour
{

    public string myColor;
    public bool start = false;
    bool player1Active = false;
    int startCount = 0;

    public AudioSource toneP1, toneP1under, movementSnap, movementSnapPoints, badChoice;
    float timeDelay1 = 1.5f;
    bool boop1 = true;
    float timeDelay2 = 1.8f;
    bool boop2 = true;

    public Material red, blue, green, purple, neutral;

    public GameObject firstHub;
    GameObject currentHub = null;
    GameObject nDestinationP, sDestinationP, eDestinationP, wDestinationP;
    GameObject nPathP, sPathP, ePathP, wPathP;

    bool isUpDown, isDownDown, isLeftDown, isRightDown;

    // Use this for initialization
    void Start()
    {
        currentHub = firstHub;

        /*if (this.GetComponent<Renderer>().material = redP)
        {
            myColor = "red";
        }
        else if (this.GetComponent<Renderer>().material = blueP)
        {
            myColor = "blue";
        }
        else if (this.GetComponent<Renderer>().material = greenP)
        {
            myColor = "green";
        }
        else if (this.GetComponent<Renderer>().material = purpleP)
        {
            myColor = "purple";
        }*/

    }

    // Update is called once per frame
    void Update()
    {
        if (start == true)
        {
            player1Active = true;
        }

        if (player1Active == true && startCount == 0)
        {
            Debug.Log("player1 is active");
            startCount++;
        }

        if (player1Active == true)
        {
            //modify countdowns
            timeDelay1 -= Time.deltaTime;
            timeDelay2 -= Time.deltaTime;

            //player controls go here until death
            if (Input.GetKey(KeyCode.UpArrow))
            {
                isUpDown = true;
                if (isUpDown == true)
                {
                    if (nDestinationP != null && nPathP.GetComponent<RotateColor>().currentColor == myColor)
                    {
                        transform.position = nDestinationP.transform.position;
                    }
                    else if (nDestinationP != null && nPathP.GetComponent<RotateColor>().currentColor == "neutral")
                    {
                        transform.position = nDestinationP.transform.position;
                    }
                    else
                    {
                        badChoice.Play();
                    }
                    isUpDown = false;
                }
            }

            if (Input.GetKey(KeyCode.DownArrow))
            {
                isDownDown = true;
                if (isDownDown == true)
                {
                    if (sDestinationP != null && sPathP.GetComponent<RotateColor>().currentColor == myColor)
                    {
                        transform.position = sDestinationP.transform.position;
                    }
                    else if (sDestinationP != null && sPathP.GetComponent<RotateColor>().currentColor == "neutral")
                    {
                        transform.position = sDestinationP.transform.position;
                    }
                    else
                    {
                        badChoice.Play();
                    }
                    isDownDown = false;
                }
            }

            if (Input.GetKey(KeyCode.LeftArrow))
            {
                isLeftDown = true;
                if (isLeftDown == true)
                {
                    Debug.Log("myColor is: " + myColor);
                    //Debug.Log("currentColor is " + wPathP.GetComponent<RotateColor>().currentColor);
                    Debug.Log("The left arrow has been pressed down");
                    if (wDestinationP != null && wPathP.GetComponent<RotateColor>().currentColor == myColor)
                    {
                        this.transform.position = wDestinationP.transform.position;
                        Debug.Log("Transforrm attempted");
                    }
                    else if (wDestinationP != null && wPathP.GetComponent<RotateColor>().currentColor == "neutral")
                    {
                        this.transform.position = wDestinationP.transform.position;
                        Debug.Log("Transforrm attempted");
                    }
                    else
                    {
                        badChoice.Play();
                    }
                    isLeftDown = false;
                    Debug.Log("leftdown reset");
                }
            }


            if (Input.GetKey(KeyCode.RightArrow))
            {
                isRightDown = true;
                if (isRightDown == true)
                {
                    if (eDestinationP != null && ePathP.GetComponent<RotateColor>().currentColor == myColor)
                    {
                       this. transform.position = eDestinationP.transform.position;
                    }
                    else if (eDestinationP != null && ePathP.GetComponent<RotateColor>().currentColor == "neutral")
                    {
                        this.transform.position = eDestinationP.transform.position;
                    }
                    else
                    {
                        badChoice.Play();
                    }
                    isRightDown = false;
                }

            }
        }

                    if (timeDelay1 <= 0 && boop1 == true)
        {
            transform.position = firstHub.transform.position;
            toneP1.Play();
            movementSnap.Play();
            boop1 = false;
        }

        if (timeDelay2 <= 0 && boop2 == true)
        {
            toneP1under.Play();
            boop2 = false;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        currentHub = other.gameObject;
        if (nDestinationP != null)
        {
            nDestinationP.transform.position = currentHub.GetComponent<HubControl>().nDestinationH.transform.position;
        }

        if (sDestinationP != null)
        {
            sDestinationP.transform.position = currentHub.GetComponent<HubControl>().sDestinationH.transform.position;
        }

        if (eDestinationP != null)
        {
            eDestinationP.transform.position = currentHub.GetComponent<HubControl>().eDestinationH.transform.position;
        }

        if (wDestinationP != null)
        {
            wDestinationP.transform.position = currentHub.GetComponent<HubControl>().wDestinationH.transform.position;
        }

        if (nPathP != null)
        {
            nPathP.transform.position = currentHub.GetComponent<HubControl>().nPathH.transform.position;
        }

        if (sPathP != null)
        {
            sPathP.transform.position = currentHub.GetComponent<HubControl>().sPathH.transform.position;
        }

        if (ePathP != null)
        {
            ePathP.transform.position = currentHub.GetComponent<HubControl>().ePathH.transform.position;
        }

        if (wPathP != null)
        {
            wPathP.transform.position = currentHub.GetComponent<HubControl>().wPathH.transform.position;
        }

        if (currentHub.GetComponent<HubControl>().currentHubValue >0)
        {
            movementSnapPoints.Play();
        }
        else if (currentHub.GetComponent<HubControl>().currentHubValue == 0)
        {
            movementSnap.Play();
        }
    }
}
