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

public class Map : MonoBehaviour
{

    GameObject LocalPlayer;
    List<GameObject> Players = new List<GameObject>();

    public GameObject[] Tiles;
    public List<GameObject> Landmarks;

    public int TileSize = 100;
    public Vector2 MapDimensions;

    public List<GameObject> Veggies;
    //Minimum and maximum values for the map
    public float minX { get { return (-MapDimensions.x * (TileSize / 2)); } }
    public float maxX { get { return (MapDimensions.x * (TileSize / 2)); } }
    public float minZ { get { return (-MapDimensions.y * (TileSize / 2)); } }
    public float maxZ { get { return (MapDimensions.y * (TileSize / 2)); } }


    #region StartupFunctions
    private void Start()
    {
        LocalPlayer = Multiplayer.PlayersManager.Instance.LocalPlayer;
        Players.Add(LocalPlayer);
        foreach (GameObject curPlayer in Multiplayer.PlayersManager.Instance.RemotePlayers.Values)
        {
            Players.Add(curPlayer);
        }
        GenerateTiles();
        CreateDummys();

    }

    //Create dummy players for the world wrapping
    void CreateDummys()
    {
        foreach (GameObject Player in Players)
        {
            Player PS = Player.GetComponent<Player>();
            PS.CreateDummies(this);  
        }
    }


    public void GenerateTiles()
    {
        float HalfSize = TileSize / 2;
        Vector3 startPoint = new Vector3(-(MapDimensions.x * HalfSize), 0.0f, -(MapDimensions.y * HalfSize));


        for (int x = 0; x < MapDimensions.x; x++)
        {
            for (int z = 0; z < MapDimensions.y; z++)
            {
                int randIndex = Random.Range(0, Tiles.Length);
                float RandomRotation = Random.Range(0, 4) * 90;
                Vector3 position = startPoint + new Vector3(TileSize * x + HalfSize, 0.0f, TileSize * z + HalfSize);
                Debug.Log(position);
                GameObject Tile = Instantiate(Tiles[randIndex]);
                Tile.transform.position = position;
                Tile.transform.Rotate(Vector3.up, RandomRotation);
                Tile.name = "Tile [" + x + "," + z + "]";

                if (x == 0)
                {
                    Vector3 newPos = position;
                    newPos.x += TileSize * MapDimensions.x;

                    GameObject MirrorTile = Instantiate(Tiles[randIndex]);
                    MirrorTile.transform.position = newPos;
                    MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
                    MirrorTile.name = Tile.name + "(Mirror)";
                }

                if (z == 0)
                {
                    Vector3 newPos = position;
                    newPos.z += TileSize * MapDimensions.y;

                    GameObject MirrorTile = Instantiate(Tiles[randIndex]);
                    MirrorTile.transform.position = newPos;
                    MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
                    MirrorTile.name = Tile.name + "(Mirror)";
                }

                if (x == 0 && z == 0)
                {
                    Vector3 newPos = position;
                    newPos.x += TileSize * MapDimensions.x;
                    newPos.z += TileSize * MapDimensions.y;

                    GameObject MirrorTile = Instantiate(Tiles[randIndex]);
                    MirrorTile.transform.position = newPos;
                    MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
                    MirrorTile.name = Tile.name + "(Mirror)";
                }

                if (x == MapDimensions.x - 1)
                {
                    Vector3 newPos = position;
                    newPos.x -= TileSize * MapDimensions.x;

                    GameObject MirrorTile = Instantiate(Tiles[randIndex]);
                    MirrorTile.transform.position = newPos;
                    MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
                    MirrorTile.name = Tile.name + "(Mirror)";
                }

                if (z == MapDimensions.y - 1)
                {
                    Vector3 newPos = position;
                    newPos.z -= TileSize * MapDimensions.y;

                    GameObject MirrorTile = Instantiate(Tiles[randIndex]);
                    MirrorTile.transform.position = newPos;
                    MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
                    MirrorTile.name = Tile.name + "(Mirror)";
                }

                if (x == MapDimensions.x - 1 && z == MapDimensions.y - 1)
                {
                    Vector3 newPos = position;
                    newPos.x -= TileSize * MapDimensions.x;
                    newPos.z -= TileSize * MapDimensions.y;

                    GameObject MirrorTile = Instantiate(Tiles[randIndex]);
                    MirrorTile.transform.position = newPos;
                    MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
                    MirrorTile.name = Tile.name + "(Mirror)";
                }

                if (x == 0 && z == MapDimensions.y - 1)
                {
                    Vector3 newPos = position;
                    newPos.x += TileSize * MapDimensions.x;
                    newPos.z -= TileSize * MapDimensions.y;

                    GameObject MirrorTile = Instantiate(Tiles[randIndex]);
                    MirrorTile.transform.position = newPos;
                    MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
                    MirrorTile.name = Tile.name + "(Mirror)";
                }

                if (x == MapDimensions.x - 1 && z == 0)
                {
                    Vector3 newPos = position;
                    newPos.x -= TileSize * MapDimensions.x;
                    newPos.z += TileSize * MapDimensions.y;

                    GameObject MirrorTile = Instantiate(Tiles[randIndex]);
                    MirrorTile.transform.position = newPos;
                    MirrorTile.transform.Rotate(Vector3.up, RandomRotation);
                    MirrorTile.name = Tile.name + "(Mirror)";
                }
            }
        }
    }

    //Populate the tile with landmarks
    void PopulateTile(Tile tile)
    {
        foreach (GameObject LP in tile.LandmarkPoints)
        {
            if (Landmarks.Count > 0)
            {
                int rand = Random.Range(0, Landmarks.Count);
                Instantiate(Landmarks[rand], LP.transform.position, Quaternion.identity, tile.transform.parent);
            }
        }

    }

    #endregion


}
//EXTRA STUFF, PLEASE IGNORE

/*
 * //IN UPDATE
    HandleCameraMovement(TopCam, true);
    HandleCameraMovement(BotCam, true);
    HandleCameraMovement(LeftCam, false);
    HandleCameraMovement(RightCam, false);
    HandleCamSizing();
    */

/*
/// <summary>
/// 
/// </summary>
/// <param name="Cam"> The camera to move</param>
/// <param name="Xmove">Do we move in the X axis or the Z?</param>
void HandleCameraMovement(Camera Cam, bool Xmove)
{
    if (Xmove == true)
    {
        Cam.transform.position = new Vector3(LocalPlayer.transform.position.x, Cam.transform.position.y, Cam.transform.position.z);
    }
    else
    {
        Cam.transform.position = new Vector3(Cam.transform.position.x, Cam.transform.position.y, LocalPlayer.transform.position.z);
    }
}

void HandleCamSizing()
{
    Transform PT = LocalPlayer.transform;
    if (PT.position.x > maxX - camWidth)
    {
        float dif = ((maxX - PT.position.x) / camWidth) / 2;
        SetCam(dif, 0, RightCam);
    }
    else if (PT.position.x < minX + camWidth)
    {
        float dif = ((minX - PT.position.x) / camWidth) / 2;
        SetCam(dif, 0, LeftCam);
    }
    else
    {
        SetCam(1, 1, RightCam);
        SetCam(1, 1, LeftCam);
    }
    if (PT.position.z > maxZ - camHeight)
    {
        float dif = ((maxZ - PT.position.z) / camHeight) / 2;
        SetCam(0, dif, TopCam);
    }
    else if (PT.position.x < + camHeight)
    {
        float dif = ((minZ - PT.position.z) / camHeight) / 2;
        SetCam(0, dif, BotCam);
    }
    else
    {
        SetCam(1, 1, RightCam);
        SetCam(1, 1, LeftCam);
    }
}

void SetCam(float x, float y, Camera cam)
{
    cam.rect = new Rect(x, y, 1, 1);
}
*/
