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

public class ToyotaController : MonoBehaviour
{
    public static ToyotaController instance;

    public FixedJoystick fixedJoystick;

    public List<ToyotaRuedasController> toyotaRuedasControllers;

    public LayerMask layerMask;

    public GameObject ui_joystickHandle;
    public GameObject ui_manubrio;

    public GameObject canvas;

    private void Start()
    {
        instance = this;
    }
    void Update()
    {
        this.ControlarMovimiento();
        this.TouchController();
        this.RotateUIManubrio();
    }

    public void ControlarMovimiento()
    {
        if (this.fixedJoystick.input.x > 0)
        {
            foreach (ToyotaRuedasController item in toyotaRuedasControllers)
            {
                item.rodar = true;
                item.atras = false;
                item.adelante = true;
            }
        }
        else
        {
            if (this.fixedJoystick.input.x < 0)
            {
                foreach (ToyotaRuedasController item in toyotaRuedasControllers)
                {
                    item.rodar = true;
                    item.atras = true;
                    item.adelante = false;
                }
            }
            else
            {
                foreach (ToyotaRuedasController item in toyotaRuedasControllers)
                {
                    item.rodar = false;
                    item.atras = false;
                    item.adelante = false;
                }
            }
        }

        if (this.fixedJoystick.input.y > 0.5f)
        {
            foreach (ToyotaRuedasController item in toyotaRuedasControllers)
            {
                item.derecha = true;
                item.izquierda = false;
            }
        }
        else
        {
            if (this.fixedJoystick.input.y < -0.5f)
            {
                foreach (ToyotaRuedasController item in toyotaRuedasControllers)
                {
                    item.derecha = false;
                    item.izquierda = true;
                }
            }
            else
            {
                foreach (ToyotaRuedasController item in toyotaRuedasControllers)
                {
                    item.derecha = false;
                    item.izquierda = false;
                }
            }
        }
    }

    public void TouchController()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            Ray ray = new Ray();

            if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor)
            {
                ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            }
            else
            {
                if (Input.touchCount > 0)
                {
                    ray = Camera.main.ScreenPointToRay(Input.touches.First().position);
                }
            }
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 999, this.layerMask))
            {
                ToyotaRaycastClickToAction hit_result = hit.collider.gameObject.GetComponent<ToyotaRaycastClickToAction>();
                if (hit_result != null)
                {
                    hit_result.AccionarSwitch();
                }
            }
        }
    }

    public void RotateUIManubrio()
    {
        Vector3 diff = this.ui_joystickHandle.transform.position - this.ui_manubrio.transform.position;
        diff.Normalize();

        float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
        this.ui_manubrio.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
    }

    public void Acelerar()
    {
        this.transform.Translate(Vector3.back * 0.02f, Space.Self);

        foreach (ToyotaRuedasController item in toyotaRuedasControllers)
        {
            if (item.derecha)
            {
                this.transform.Rotate(Vector3.up * 0.2f, Space.Self);
            }
            if (item.izquierda)
            {
                this.transform.Rotate(Vector3.down * 0.2f, Space.Self);
            }
        }
    }

    public void Reversa()
    {
        this.transform.Translate(Vector3.forward * 0.02f, Space.Self);
        foreach (ToyotaRuedasController item in toyotaRuedasControllers)
        {
            if (item.derecha)
            {
                this.transform.Rotate(Vector3.down * 0.2f, Space.Self);
            }
            if (item.izquierda)
            {
                this.transform.Rotate(Vector3.up * 0.2f, Space.Self);
            }
        }
    }

    public void SwitchCanvas()
    {
        this.canvas.SetActive(!this.canvas.activeSelf);
    }
}
