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

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

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

    // Start is called before the first frame update
    void Start()
    {
        transform.GetChild(0).gameObject.SetActive(info);
    }

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

    void OnMouseDown()
    {
        if(mode == Mode.Player) info = !info;
        //Debug.Log(StagesEditorController.indexCursor);
        if (move)
        {
            screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
            offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
        }
    }

    // Перемещение
    void OnMouseDrag()
    {
        if (move)
        {
            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);
        }
    }
}
