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

public class ColorGameMode : GameMode
{

    public int MaxRound = 5;
    private int RoundCount = 0;

	public GameObject counterScore;
	public GameObject colorCube;
	private int counter = 1;
	private GameObject[] objs;
	public Texture playerTexture;
	private LayerMask Ignore;

	public List<ClientData> ConnectedClients;
	public GameObject levelScoreboard;
	public GameObject[] players = new GameObject[4];
	public GameObject[] scores = new GameObject[4];

	public ScoreBoard mainscoreboard;

	void Start(){
		counterScore.GetComponent<TextMeshProUGUI>().text = counter.ToString();
		objs = GameObject.FindGameObjectsWithTag("Coloured");
	}

    /// <summary>
    /// Called once all players have finished their moves but before the Objective is checked
    /// </summary>
    public override void OnRoundFinished()
    {
        RoundCount++;
    }

    /// <summary>
    /// Checks if the Game is finished
    /// </summary>
    /// <returns>returns if game is finished</returns>
    public override bool isGameOver()
    {
		return (RoundCount <= MaxRound);
    }

	/// <summary>
	/// couroutine to display level scores
	/// </summary>
	IEnumerator displayforSeconds(GameObject display, float time)
	{
		display.SetActive (true);
		yield return new WaitForSeconds(time);
		display.SetActive (false);
		mainscoreboard.endGame();
	}

	/// <summary>
	/// calculates and assigns scores in desc order, call coroutine
	/// </summary>
	public void levelComplete(ClientList clientDataList)
	{
		ConnectedClients = clientDataList.ConnectedClients;
		ConnectedClients.Sort((a, b) => b.collected.CompareTo(a.collected));

		for (int i = 0; i<ConnectedClients.Count; i++){
			players[i].GetComponent<TextMeshProUGUI>().text = ConnectedClients[i].Name;
			scores[i].GetComponent<TextMeshProUGUI>().text = ConnectedClients[i].collected.ToString();
		}
		StartCoroutine(displayforSeconds(levelScoreboard, 10.0f));
	}
		
    
    /// <summary>
    /// Called once per player after they have moved onto a block
    /// </summary>
    /// <param name="character">Character which moved</param>
    /// <param name="client">Client of the character</param>
    /// <param name="currentBlock">Block moved onto</param>
    public override void OnPlayerFinishedMove(Character character, ClientData client,  Block currentBlock)
    {
		string clientTexture = client.Color + "Grass";
		playerTexture = Resources.Load<Texture>(clientTexture);
		//Resources.Load<Texture2D>(clientTexture);


        //Code for changing block color;
		//Block.isBlockAtPosition(transform.position + Vector3.down / 2, 0.5f, ~Ignore, out CurrentBlock);

		Renderer abc = currentBlock.GetComponent<Renderer>();
		if(abc.material.GetTexture("_Albedo") != playerTexture){
			abc.material.SetTexture("_Albedo", playerTexture);
		}

		//Code for updating player scores;
		colorCube.GetComponent<Image>().color = client.Color;
		counter = 1;

		foreach(GameObject a in objs){
			abc = a.GetComponent<Renderer>();
			if (abc.material.GetTexture("_Albedo") == playerTexture)
			{
				counter++;
			}
		}
		counterScore.GetComponent<TextMeshProUGUI>().text = counter.ToString();
		client.collected = counter;
    }
}
