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

/// <summary>
/// Размещение маяков
/// Используется в редакторе и в плеере
/// </summary>
public class BeaconController : MonoBehaviour
{
    public float Height = 0.5f;
    public int Rounding = 1;
    //public bool move = false;
    public bool info = false; // вывод таблицы с информацией 
    public enum Mode { Player, Editor};
    public Mode mode = Mode.Player;
    public string text_info;
    public string id;
    Text text;
    GameObject canvas;

    private Vector3 screenPoint;
    private Vector3 offset;
    private Vector3 curScreenPoint;
    private Vector3 curPosition;

    // Start is called before the first frame update
    void Start()
    {
        text = transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>();
        text.text = text_info;    
        canvas = transform.GetChild(0).gameObject;
        canvas.SetActive(info);          
    }

    // Update is called once per frame
    void Update()
    {
        if (info != transform.GetChild(0).gameObject.activeSelf)
        {
            canvas.SetActive(info);
        }
    }

    void OnMouseDown()
    {
        if(mode == Mode.Player) info = !info;
        //Debug.Log(StagesEditorController.indexCursor);
        if (mode == Mode.Editor)
        {
            screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
            offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
            info = true;
            text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 100);
            text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 15);
            transform.GetComponent<LabelObjectScript>().enabled = false;
        }        
    }

    // Перемещение
    void OnMouseDrag()
    {
        if (mode == Mode.Editor)
        {
            curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
            curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;

            var x =(float) Math.Round(curPosition.x, Rounding);
            var z = (float)Math.Round(curPosition.z, Rounding);
            transform.position = new Vector3(x, Height, z);
            text.text = $"id={id} x={x} y={z}";
            canvas.transform.GetChild(0).transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y+20, screenPoint.z);
        }
    }

    private void OnMouseUp()
    {
        if (mode == Mode.Editor)
        {
            info = false;
            text.text = text_info;
            text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 120);
            text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 75);
            transform.GetComponent<LabelObjectScript>().enabled = true;
        }
    }
}
