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

/// <summary>
/// Отслеживание объекта
/// </summary>
public class CameraController : MonoBehaviour
{
    public enum RotationAxes { Mouse, TrackMarker }
    public RotationAxes axes = RotationAxes.Mouse;
    public float movementSpeed = 0.1f;
    public int mouseSpeed = 100;
    public float smoothness = 0.85f;

    Vector3 targetPosition;
    private Vector3 dragOrigin;

    public Transform target;
    
    void Start()
    {      
        targetPosition = transform.position;
    }

    void Update()
    {
        //if (!EventSystem.current.IsPointerOverGameObject())
        //{
        switch (axes)
        {
            case RotationAxes.Mouse:
                MouseCommands();
                break;
            case RotationAxes.TrackMarker:
                TrackMarker();
                break;
        }
        //}
    }  

    void ShowAndUnlockCursor()
    {
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
    }

    void HideAndLockCursor()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }     
   
    private void MouseCommands()
    {
        //if (Input.GetKey(KeyCode.W))
        //{
        //    targetPosition += transform.forward * movementSpeed;
        //    Camera.main.orthographicSize++;
        //}
        if (Input.GetKey(KeyCode.LeftArrow))
            targetPosition -= transform.right * movementSpeed;
        //if (Input.GetKey(KeyCode.S))
        //{
        //    targetPosition -= transform.forward * movementSpeed;
        //    if (Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
        //}
        if (Input.GetKey(KeyCode.RightArrow))
            targetPosition += transform.right * movementSpeed;
        if (Input.GetKey(KeyCode.DownArrow))
            targetPosition -= transform.up * movementSpeed;
        if (Input.GetKey(KeyCode.UpArrow))
            targetPosition += transform.up * movementSpeed;

        if (Input.GetMouseButtonDown(2))
        {
            dragOrigin = Input.mousePosition;
        }


        if (Input.GetMouseButton(1))
        {
            Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
            if (pos.x < 0)
                targetPosition -= transform.right * movementSpeed * 0.1f;
            if (pos.x > 0)
                targetPosition += transform.right * movementSpeed * 0.1f;
            if (pos.y < 0)
                targetPosition -= transform.up * movementSpeed * 0.1f;
            if (pos.y > 0)
                targetPosition += transform.up * movementSpeed * 0.1f;
        }

        if (Input.GetAxis("Mouse ScrollWheel") > 0 && !EventSystem.current.IsPointerOverGameObject())
        {
            targetPosition += transform.forward * movementSpeed* mouseSpeed;
            if (Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
        }
        else if (Input.GetAxis("Mouse ScrollWheel") < 0 && !EventSystem.current.IsPointerOverGameObject())
        {
            targetPosition -= transform.forward * movementSpeed*mouseSpeed;
            Camera.main.orthographicSize++;
        }

        transform.position = Vector3.Lerp(transform.position, targetPosition, (1.0f - smoothness));
    }

    private Vector3 screenPoint;
    private Vector3 offset;
    private Vector3 curScreenPoint;
    private Vector3 curPosition;
    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));
        
    }

    private void OnMouseDrag()
    {
        switch (axes)
        {
            case RotationAxes.Mouse:
                curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
                curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;

                transform.position = curPosition;
                break;
        }
    }    

    void TrackMarker()
    {
        //if (Input.GetKey(KeyCode.W))
        //{            
        //    targetPosition += transform.forward * movementSpeed;
        //    Camera.main.orthographicSize++;
        //}
        //if (Input.GetKey(KeyCode.S))
        //{
        //    targetPosition -= transform.forward * movementSpeed;
        //    if (Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;
        //}
        if (Input.GetAxis("Mouse ScrollWheel") > 0 && !EventSystem.current.IsPointerOverGameObject())
        {
            targetPosition += transform.forward * movementSpeed* mouseSpeed; 
            if(Camera.main.orthographicSize > 1) Camera.main.orthographicSize--;           
        }
        else if (Input.GetAxis("Mouse ScrollWheel") < 0 && !EventSystem.current.IsPointerOverGameObject())
        {
            targetPosition -= transform.forward * movementSpeed* mouseSpeed;
            Camera.main.orthographicSize++;
        }
        var marker_offset = new Vector3(target.position.x, targetPosition.y, target.position.z);
        targetPosition =Vector3.Lerp(transform.position, /*targetPosition + target.position - */marker_offset, (1.0f - smoothness));
        transform.position = targetPosition;
        if (Input.GetMouseButton(1))
        {
            axes = RotationAxes.Mouse;

            var player = PlayerController.instance;
            if (player.UserInfo.activeSelf)
            {
                var client = Client.instance;
                client.ImageStreamStopSend(player.user_broadcast.Value);
                player.user_broadcast = null;
                player.UserInfo.SetActive(false);
            }
        }
    }
}
