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

public class Map : MonoBehaviour {

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

    public GameObject[] MapGrid;
    public GameObject[] WorldWrapGrid;



    //Minimum and maximum values for the map
    public float minX;
    public float maxX;
    public float minZ;
    public float maxZ;

    /*
    public Camera TopCam;
    public Camera BotCam;
    public Camera LeftCam;
    public Camera RightCam;
    */

    #region StartupFunctions
    private void Start()
    {
        //GenerateMap();
        CreateDummys();
        LocalPlayer = Multiplayer.PlayersManager.Instance.LocalPlayer;

    }

    //Create dummy players for the world wrapping
    void CreateDummys()
    {
        List<GameObject> Players = new List<GameObject>();
        foreach (GameObject Player in Players)
        {
            Player PS = Player.GetComponent<Player>();
            Transform PT = Player.transform;
            Transform model = PT.Find("Model");
            PS.dummies.Add(Instantiate(model, new Vector3(PT.position.x + minX, PT.position.y, PT.position.z), transform.rotation, PT).gameObject);
            PS.dummies.Add(Instantiate(model, new Vector3(PT.position.x + maxX, PT.position.y, PT.position.z), transform.rotation, PT).gameObject);
            PS.dummies.Add(Instantiate(model, new Vector3(PT.position.x, PT.position.y, PT.position.z + minZ), transform.rotation, PT).gameObject);
            PS.dummies.Add(Instantiate(model, new Vector3(PT.position.x, PT.position.y, PT.position.z + maxZ), transform.rotation, PT).gameObject);
        }
    }


    void GenerateMap()
    {
        GameObject[] gameTiles = new GameObject[4];
        for (int i = 0; i < 4; i++)
        {
            int rand = Random.Range(0, Tiles.Length);
            GameObject tile = Instantiate(Tiles[rand], MapGrid[i].transform.position, Quaternion.identity);
            PopulateTile(tile.GetComponent<Tile>());
            gameTiles[i] = tile;
        }
        //Create the fake tiles for the world wrapping (This is done super poorly and quickly, don't judge me :)
        Instantiate(gameTiles[0], WorldWrapGrid[6].transform);
        Instantiate(gameTiles[0], WorldWrapGrid[10].transform);
        Instantiate(gameTiles[0], WorldWrapGrid[12].transform);
        Instantiate(gameTiles[1], WorldWrapGrid[5].transform);
        Instantiate(gameTiles[1], WorldWrapGrid[9].transform);
        Instantiate(gameTiles[1], WorldWrapGrid[11].transform);
        Instantiate(gameTiles[2], WorldWrapGrid[2].transform);
        Instantiate(gameTiles[2], WorldWrapGrid[4].transform);
        Instantiate(gameTiles[2], WorldWrapGrid[8].transform);
        Instantiate(gameTiles[3], WorldWrapGrid[1].transform);
        Instantiate(gameTiles[3], WorldWrapGrid[3].transform);
        Instantiate(gameTiles[3], WorldWrapGrid[7].transform);
    }

    //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

    void Update()
    {
        Teleport();
    }

    void Teleport()
    {
        Transform PT = LocalPlayer.transform;
        if (PT.position.x > maxX)
        {
            PT.position = new Vector3(minX, PT.position.y, PT.position.z);
        }
        else if (PT.position.x < minX)
        {
            PT.position = new Vector3(maxX, PT.position.y, PT.position.z);
        }
        if (PT.position.z > maxZ)
        {
            PT.position = new Vector3(PT.position.x, PT.position.y, minZ);
        }
        else if (PT.position.z < minZ)
        {
            PT.position = new Vector3(PT.position.x, PT.position.y, maxZ);
        }
    }


}
        //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);
        }
        */