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

public class WaiterHandler : MonoBehaviour
{
    public GameObject[] foodObjects;
    public GameObject waiterObject;
    public GameObject arrowObject;
    private int noOfPlayers;
    public Color[] playerColors;
    public Sprite[] bowTies;

    public float spawnInterval;
    public float spawnHeight;

    private CameraMovement cam;
    private ActiveWaiterArrow[] arrows;

    private List<WaiterMovement>[] waiters;
    private int[] activeWaiter;

	// Use this for initialization
	void Start ()
    {
        noOfPlayers = FindObjectOfType<GlobalVariables>().noOfPlayers;

        cam = FindObjectOfType<CameraMovement>();
        waiters = new List<WaiterMovement>[noOfPlayers];
        activeWaiter = new int[noOfPlayers];
        arrows = new ActiveWaiterArrow[noOfPlayers];

        for (int i = 0; i < waiters.Length; i++)
        {
            waiters[i] = new List<WaiterMovement>();
        }

        for (int i = 0; i < noOfPlayers; i++)
        {
            SpawnWaiter(true);
            arrows[i] = Instantiate(arrowObject, FindObjectOfType<Canvas>().transform).GetComponent<ActiveWaiterArrow>();
            arrows[i].GetComponent<Image>().color = playerColors[i];
            arrows[i].follow = waiters[i][0];
        }

        cam.SetTargets(waiters[0][activeWaiter[0]].transform, waiters[noOfPlayers - 1][activeWaiter[noOfPlayers - 1]].transform);

        Instantiate(foodObjects[Mathf.RoundToInt(Random.Range(0, foodObjects.Length - 1))], new Vector3(0.0f, 7.0f, -5.0f), Quaternion.identity);
    }

    // Update is called once per frame
    void Update ()
    {
		if(Input.GetKeyDown(KeyCode.Joystick1Button5))
        {
            SwitchActiveWaiter(0, true);
        }
        
        if(Input.GetKeyDown(KeyCode.Joystick1Button4))
        {
            SwitchActiveWaiter(0, false);
        }

        if (Input.GetKeyDown(KeyCode.Joystick2Button5))
        {
            SwitchActiveWaiter(1, true);
        }

        if (Input.GetKeyDown(KeyCode.Joystick2Button4))
        {
            SwitchActiveWaiter(1, false);
        }

        if (Input.GetKeyDown(KeyCode.Joystick3Button5))
        {
            SwitchActiveWaiter(2, true);
        }

        if (Input.GetKeyDown(KeyCode.Joystick3Button4))
        {
            SwitchActiveWaiter(2, false);
        }
    }

    private void SwitchActiveWaiter(int player, bool right)
    {
        if(player < noOfPlayers)
        {
            if (right && activeWaiter[player] < waiters[player].Count - 1)
            {
                waiters[player][activeWaiter[player]].enabled = false;
                activeWaiter[player]++;
                waiters[player][activeWaiter[player]].enabled = true;
                arrows[player].follow = waiters[player][activeWaiter[player]];
            }
            else if (!right && activeWaiter[player] > 0)
            {
                waiters[player][activeWaiter[player]].enabled = false;
                activeWaiter[player]--;
                waiters[player][activeWaiter[player]].enabled = true;
                arrows[player].follow = waiters[player][activeWaiter[player]];
            }

            Transform leftMost = waiters[0][activeWaiter[0]].transform;
            Transform rightMost = waiters[0][activeWaiter[0]].transform;

            if(noOfPlayers > 1)
            {
                for (int i = 0; i < noOfPlayers; i++)
                {
                    if (waiters[i][activeWaiter[i]].transform.position.x < leftMost.position.x)
                    {
                        leftMost = waiters[i][activeWaiter[i]].transform;
                    }
                    else if (waiters[i][activeWaiter[i]].transform.position.x > rightMost.position.x)
                    {
                        rightMost = waiters[i][activeWaiter[i]].transform;
                    }
                }

            }

            cam.SetTargets(leftMost, rightMost);
        }
    }

    public void SpawnWaiter(bool startActive)
    {
        int spawnPointIndex = 0;
        int player = 0;

        for (int i = 0; i < noOfPlayers; i++)
        {
            spawnPointIndex = spawnPointIndex + waiters[i].Count;

            if(waiters[player].Count > waiters[i].Count)
            {
                player = i;
            }
        }

        WaiterMovement tempWaiter = Instantiate(waiterObject, new Vector3(spawnPointIndex * spawnInterval, spawnHeight, 0), Quaternion.identity).GetComponent<WaiterMovement>();
        waiters[player].Add(tempWaiter);
        tempWaiter.SetPlayerBelonging(player);
        tempWaiter.bowTie.sprite = bowTies[player];
        tempWaiter.enabled = startActive;
    }
}
