using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.SceneManagement;

public class MapScript : MonoBehaviour {
	//[ReadOnly]
	public GameObject miniUI, bigUI;
	//[ReadOnly]
	public Transform followTarget, marker, pin;
	//[ReadOnly]
	public RenderTexture miniMap, bigMap;
	public Camera miniCam, bigCam;
	Vector3 markerRot, markerPos, mapPos;
	public static bool toggleMap = false;

    Vector3 pinWorldPosition;
    Vector3 pinCanvasPosition;
    bool pinPlaced = false;

    void Awake() {
        mapPos = transform.position;
        pinPlaced = false;
		if(marker && SceneManager.GetActiveScene().name == "GameIsland")
		    marker.gameObject.SetActive (false);
    }
	
	// Update is called once per frame
	void Update () {
        HandleBigMapPin();
        if (toggleMap) {
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }
        if (toggleMap && pinPlaced) {
            pin.position = pinCanvasPosition;
            pin.gameObject.SetActive(true);
        }
        else {
            if(pin)
                pin.gameObject.SetActive(false);
        }

        if (!followTarget) {
            GetFollowTarget();
            return;
        }

		mapPos = new Vector3 (followTarget.position.x, miniCam.transform.position.y, followTarget.position.z);
		markerPos = new Vector3 (followTarget.position.x, marker.position.y, followTarget.position.z);
		markerRot = new Vector3 (90, followTarget.eulerAngles.y, 0);

		if (Input.GetKeyDown (KeyCode.M) && SceneManager.GetActiveScene().name != "Launcher")
			AdjustMaps (!toggleMap);

		marker.position = markerPos;
        marker.eulerAngles = markerRot;
		miniCam.transform.position = mapPos;
    }

    void HandleBigMapPin() {
        if (toggleMap) {
            RectTransform imgRectTransform = bigUI.GetComponent<RectTransform>();
            Vector2 localMousePosition = imgRectTransform.InverseTransformPoint(Input.mousePosition);
            if (imgRectTransform.rect.Contains(localMousePosition)) {
                if (Input.GetMouseButtonDown(0)) {
                    Vector2 viewSpaceMousePosition = new Vector2(((localMousePosition.x) / (imgRectTransform.rect.width / 2) + 1) / 2, ((localMousePosition.y / (imgRectTransform.rect.height / 2)) + 1) / 2);

                    Ray ray = bigCam.ViewportPointToRay(viewSpaceMousePosition);
                    RaycastHit hit = new RaycastHit();
                    if (Physics.Raycast(ray, out hit)) {
                        pinWorldPosition = hit.point;
                        pinCanvasPosition = Input.mousePosition;
                        pinPlaced = true;
                    }
                }
                if (Input.GetMouseButtonDown(1)) {
                    pinPlaced = false;
                }
            }
        }
    }

    void AdjustMaps (bool fullMap) {
        bigUI.SetActive (fullMap);
		bigCam.gameObject.SetActive (fullMap);
		toggleMap = fullMap;
	}

    void GetFollowTarget() {
        GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
        for (int i = 0; i < objs.Length; i++) {
            PhotonView view = objs[i].GetComponent<PhotonView>();
            if (view && !view.IsMine && PhotonNetwork.IsConnected) {
                continue;
            }

            followTarget = objs[i].transform;
        }
    }

    public bool HasPinBeenPlaced() {
        return pinPlaced;
    }

    public Vector3 GetWorldPositionOfPin() {
        return pinWorldPosition;
    }

    public void OpenMapForPlayerToChooseLocationForDrop() {
        AdjustMaps(true);
    }

    public void CloseMapForPlayerToChooseLocationForDrop() {
        AdjustMaps(false);
		marker.gameObject.SetActive (true);
    }
}
