using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using Networking.Server;
using Networking;
using UnityEngine.SceneManagement;

public class blockSpawn : MonoBehaviour
{
    [SerializeField]
    public Inventory.Data[] spawnLogicList;
    public List<ClientData> ConnectedClients;
    public ClientList clientDataList;
    public Block[] SpawnBlocks;
	bool spawned = false;

    // Start is called before the first frame update
    void Awake()
    {
        ConnectedClients = clientDataList.ConnectedClients;
        SpawnBlocks = FindObjectsOfType<Block>().Where(p => p.is_Walkable).ToArray();
    }

    public void getPlayerScores()
    {
        ConnectedClients.Sort((b, a) => a.SceneScore.CompareTo(b.SceneScore));
    }

    public void checkValid(Vector3 spawnposition)
    {
        for(int i = 0; i < SpawnBlocks.Length; i++)
        {
			if(spawned == false){
				if (SpawnBlocks[i].position.x == spawnposition.x && SpawnBlocks[i].position.z == spawnposition.z)
				{
					GameObject prefab = Resources.Load("Logic Block") as GameObject;
					GameObject block = Instantiate(prefab);
                    
					int number = (int)Random.Range(1.0f, spawnLogicList.Length);
					block.GetComponent<LogicCollectable_Multiplayer>().Collectable.element = spawnLogicList[number].element;
					block.GetComponent<LogicCollectable_Multiplayer>().Collectable.Count = spawnLogicList[number].Count;
					block.transform.position = spawnposition;
                    Debug.Log("Instantiated new logic block: " + spawnLogicList[number].element + " at position: " + block.transform.position);
                    spawned = true;
					break;
				}
			}
        }
		if(spawned == false){
			spawnposition.z -= 1;
			checkValid(spawnposition);
		} 
    }

    public void getPlayerLocations()
    {
        Vector3 spawnposition = new Vector3(0, 1, 0);
        Vector2 playerTwo = new Vector2(ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[1].playerCharacter.CurrentBlock.VisualPosition.z);
        Vector2 playerOne = new Vector2(ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.x, ConnectedClients[0].playerCharacter.CurrentBlock.VisualPosition.z);
        //x
        if (playerOne.x > playerTwo.x)
        {
            if ((playerOne.x - playerTwo.x) <= 1)
            {
                spawnposition.x = playerOne.x;
            }
            else
            {
                spawnposition.x = (playerOne.x - playerTwo.x) + playerOne.x;
            }
        }
        else
        {
            if ((playerTwo.x - playerOne.x) <= 1)
            {
                spawnposition.x = playerTwo.x;
            }
            else
            {
                spawnposition.x = (playerTwo.x - playerOne.x) + playerTwo.x;
            }
        }

        //z
        if (playerOne.y > playerTwo.y)
        {
            if ((playerOne.y - playerTwo.y) <= 1)
            {
                spawnposition.z = playerOne.y;
            }
            else
            {
                spawnposition.z = (playerOne.y - playerTwo.y) + playerOne.y;
            }
        }
        else
        {
            if ((playerTwo.y - playerOne.y) <= 1)
            {
                spawnposition.z = playerTwo.y;
            }
            else
            {
                spawnposition.z = (playerTwo.y - playerOne.y) + playerTwo.y;
            }
        }

        checkValid(spawnposition);
    }

}
