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


/// <summary>
/// Создание комнаты в редакторе 
/// не используется
/// </summary>
public class RoomController : MonoBehaviour
{
    public float Height = 1f;
    public int Rounding = 1;
    public bool move = true;
    public bool BoxColliderEnabled = true;
    //GameObject cursor_end;

    private Vector3 screenPoint;
    private Vector3 offset;
    private Vector3 curScreenPoint;
    private Vector3 curPosition;
    public Vector3 start_pos = Vector3.zero; // координаты левого нижнего угла

    // Start is called before the first frame update
    void Start()
    {
        gameObject.GetComponent<BoxCollider>().enabled = BoxColliderEnabled;
        //cursor_end = transform.GetChild(6).gameObject;
    }

    // Update is called once per frame
    void Update()
    {
        if (gameObject.GetComponent<BoxCollider>().enabled != BoxColliderEnabled) gameObject.GetComponent<BoxCollider>().enabled = BoxColliderEnabled;

    }

    void OnMouseDown()
    {
        //Debug.Log(StagesEditorController.indexCursor);
       
            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()
    {
        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); 
        var boxCollider = gameObject.GetComponent<BoxCollider>();
        if (!move)
        {           
            //transform.position = new Vector3(x, Height, z);
            
             boxCollider.size = new Vector3(x, Height, z);
            
            LocationController.Cube(gameObject, new Vector3(x, z, Height), new Vector2(start_pos.x, start_pos.z),0.05f);
        }
        else
        {
            start_pos = new Vector3(x, Height*0.5f, z);
            transform.position = start_pos;
            //var size = boxCollider.size;
            //start_pos = new Vector3(x - size.x/2, Height, z - size.z / 2);
        }
        
    }

    private void OnMouseUp()
    {
        //boxCollider.enabled=false;
        if (move) move = false;
    }
}
