using Assets.Scripts.Models;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/// <summary>
/// Взаимодействие с GameObject.
/// Перемещение, поворот, пропорциональное изменение размера.
/// </summary>
public class TouchScript : MonoBehaviour
{
    // назначается в StagesEditorController при редактировании курсора-маркера
    public enum Tools { Move = 0, Rotate = 1, Scale = 2, Rectangle = 3 }
    public static Tools tools = Tools.Scale;
    public bool Resize = true;
    // Start is called before the first frame update
    void Start()
    {
    }

    void Update()
    {
        if (Input.touchCount > 0)
        {
            Debug.Log("Global touch!");

        }
    }

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

    public float horizontalSpeed = 1.0F;
    public float verticalSpeed = 1.0F;

    private Vector3 scaleChange = new Vector3(-0.001f, -0.001f, -0.001f);
    private Vector3 positionChange = new Vector3(0.0f, -0.0005f, 0.0f);
    private bool increase = false;
    public GameObject panel;
    float deltaScale = 0.001f;

    // Нажатие
    void OnMouseDown()
    {
        //Debug.Log(StagesEditorController.indexCursor);
        if (Resize && !EventSystem.current.IsPointerOverGameObject())
            switch (tools)
            {
                case Tools.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));
                    //panel.transform.position = transform.position;
                    break;
                case Tools.Rotate:
                    //Debug.Log("MouseDown: Rotate");
                    break;
                case Tools.Scale:
                    increase = !increase;
                    //Debug.Log("MouseDown: Scale");
                    break;
                    //case Tools.Rectangle:
                    //    Debug.Log("MouseDown: Rectangle");
                    //    break;
            }
    }

    // Перемещение
    void OnMouseDrag()
    {
        float h = horizontalSpeed * Input.GetAxis("Mouse X");
        float v = verticalSpeed * Input.GetAxis("Mouse Y");
        if (Resize && !EventSystem.current.IsPointerOverGameObject())
            switch (tools)
            {
                case Tools.Move:
                    curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
                    curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
                    transform.position = curPosition;
                    panel.transform.position = transform.position;
                    break;
                case Tools.Rotate:
                    //Debug.Log("MouseDrag: Rotate");
                    //var rotation_x = StagesEditorController.sRotation.transform.GetChild(0).gameObject.transform;
                    //var rotation_y = StagesEditorController.sRotation.transform.GetChild(1).gameObject.transform;
                    //var rotation_z = StagesEditorController.sRotation.transform.GetChild(2).gameObject.transform;

                    //if (rotation_x == transform)
                    //{
                    //    StagesEditorController.selectCursor.panel_cursor.transform.Rotate(0, 0, h + v); // h v 0

                    //}
                    //if (rotation_y == transform)
                    //{
                    //    StagesEditorController.selectCursor.panel_cursor.transform.Rotate(0, h + v, 0);
                    //}
                    //if (rotation_z == transform)
                    //{
                    //    StagesEditorController.selectCursor.panel_cursor.transform.Rotate(-(h + v), 0, 0);

                    //}
                    //var Rotation = transform.parent.GetChild(2).gameObject;
                    //Rotation.SetActive(true);

                    // Вращение по всем осям
                    //Debug.Log("y=" +Input.GetAxis("Mouse Y") + " x="+ Input.GetAxis("Mouse X"));
                    //transform.localScale = new Vector3(mouse_axis_y + Input.GetAxis("Mouse Y"), 1f, mouse_axis_x + Input.GetAxis("Mouse X")) * 10f;
                    //StagesEditorController.selectCursor.panel_cursor.transform.Rotate(h, v, 0);

                    //StagesEditorController.ToolsValue(tools, StagesEditorController.selectCursor.panel_cursor.transform);
                    break;
                case Tools.Scale:
                    //var scale = transform.localScale;
                    //transform.localScale = new Vector3(scale.x + (h + v), scale.z + (h + v), scale.z);
                    var proportions = transform.transform.localScale.z / transform.transform.localScale.x;
                    var z = proportions * deltaScale;

                    //transform.transform.position = new Vector3(scale.x *5, 0, scale.z *5);
                    if (increase)
                    {
                        scaleChange = new Vector3(deltaScale, 0, z);
                        positionChange = new Vector3(deltaScale * 5, 0, z * 5);
                    }
                    else
                    {
                        scaleChange = new Vector3(-deltaScale, 0, -z);
                        positionChange = new Vector3(-deltaScale * 5, 0, -z * 5);
                    }
                    transform.transform.localScale += scaleChange;
                    transform.transform.position += positionChange;

                    // Move upwards when the sphere hits the floor or downwards
                    // when the sphere scale extends 1.0f.
                    if (transform.transform.localScale.y < 0.01f || transform.transform.localScale.y > 10.0f)
                    {
                        increase = !increase;
                    }
                    //Debug.Log("MouseDrag: Scale");
                    panel.transform.localScale = transform.localScale;
                    break;
                case Tools.Rectangle:
                    //Debug.Log("MouseDrag: Rectangle");
                    // Вращение по всем осям
                    //Debug.Log("y=" + Input.GetAxis("Mouse Y") + " x=" + Input.GetAxis("Mouse X"));
                    //transform.localScale = new Vector3(mouse_axis_y + Input.GetAxis("Mouse Y"), 1f, mouse_axis_x + Input.GetAxis("Mouse X")) * 10f;
                    //StagesEditorController.selectCursor.panel_cursor.transform.Rotate(h, v, 0);
                    break;
            }
    }
    void OnMouseUp()
    {

    }
}
