using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Linq;

public class UIManager : MonoBehaviour
{
    [SerializeField]
    Square square;
    [SerializeField]
    Camera playercamera;

    public Canvas canvas;
    public Text mytime;
    //public Planemovement clock1;
    //public Planemovement clock2;
    //public Text clock1time;
    //public Text clock2time;
    public Text wintext;
    public GameObject _pathUI;
    public LevelManager Levelmanager;
    private int prevSelectPathNum = -1;
    private Text pathName;
    private Vector3 middlePoint;
    
    // Start is called before the first frame update
    void Start()
    {
        _pathUI.SetActive(false);
        pathName = _pathUI.transform.Find("Name").GetComponent<Text>();
        middlePoint = _pathUI.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        mytime.text = Levelmanager.player.time.ToString() + " s";
        //clock1time.text = clock1.GetTime().ToString() + " s";
        //clock2time.text = clock2.GetTime().ToString() + " s";
        if(Levelmanager.winstate)
        {
            wintext.gameObject.SetActive(true);
        }
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit1;
            RaycastHit hit2;
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            
            if (Physics.Raycast(ray, out hit1))
            {
                ray = playercamera.ViewportPointToRay(hit1.textureCoord);
                if (Physics.Raycast(ray, out hit2))
                {
                    var obj = hit2.collider.gameObject;

                    if (obj.tag == "path")
                    {
                        int pathNum = int.Parse(obj.name.Substring(13));

                        if (pathNum != prevSelectPathNum)
                        {
                            pathName.text = obj.name;
                            //_pathUI.transform.position = middlePoint;
                            var tmp = Camera.main.WorldToScreenPoint(hit1.point);
                            float width = Screen.width * _pathUI.GetComponent<RectTransform>().anchorMin.x;
                            float height = Screen.height * _pathUI.GetComponent<RectTransform>().anchorMin.y;
                            
                            _pathUI.GetComponent<RectTransform>().anchoredPosition = new Vector2(tmp.x - width, tmp.y - height);
                            // see for reference: https://sushanta1991.blogspot.com/2016/04/how-to-move-unity-ui-using-mouse.html
                            // Also try using Camera.WorldtoViewportPoint() and Camera.WorldtoScreenPoint() to convert world coordinates to
                            // normalized viewport (x,y) or pixel coordinates)
                            _pathUI.SetActive(true);
                            prevSelectPathNum = pathNum;
                        }
                        else
                        {
                            _pathUI.SetActive(false);
                            prevSelectPathNum = -1;
                        }
                    }
                    else
                    {
                        _pathUI.SetActive(false);
                        prevSelectPathNum = -1;
                    }
                }
            }
        }
        else if (Input.GetMouseButtonDown(1))
        {
            _pathUI.SetActive(false);
            prevSelectPathNum = -1;
        }
    }
}
