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

public class _WheelController : MonoBehaviour
{
    public Transform WheelPreFab;
    private Transform Wheel;
    private Transform canvas;
    public static float WheelSpeed = 15;
    private float IsWheelTurningRight = 0;
    private float IsWheelTurningLeft = 0;
    public int WheelDirection; // 1 = ClockWise | 0 = Counter ClockWise 

    void Start()
    {
        // Display Wheel on Canvas UI
        canvas = GameObject.Find("CanvasUI").transform;
        Wheel = Instantiate(WheelPreFab);
        Wheel.transform.SetParent(canvas);

        // Set ClockWise as default direction
        WheelDirection = 1;
        
        // Set Wheel at original position
        Wheel.rotation = Quaternion.Slerp(Wheel.rotation, Quaternion.Euler(0, 0, 0), Time.deltaTime * WheelSpeed);
        Wheel.transform.localPosition = new Vector3(
        												(-Screen.width/2)+(Wheel.GetComponent<RectTransform>().rect.width/2)+30,
        												(-Screen.height/2)+5,
        												0
        											);
    }

    void Update()
    {
        /*  Reset Posiction for test

        if (Input.GetKey(KeyCode.S)){
            Wheel.rotation = Quaternion.Slerp(Wheel.rotation, Quaternion.Euler(0, 0, 0), Time.deltaTime * WheelSpeed);
        }
        
        END Reset Posiction for test  */
    
        // Turn RIGHT and then to back to original
        
        // Check if Wheel is turning to unsure that the movement is not interrupted
        if(Input.GetKey(KeyCode.D)){
            
            if(IsWheelTurningRight == 0){
                IsWheelTurningRight = 1;
                IsWheelTurningLeft = 0;
            }

       }

        if(IsWheelTurningRight == 1){

            // Debug.Log(Quaternion.Angle(Wheel.rotation,Quaternion.Euler(0, 0, 0)));
            // Debug.Log(WheelDirection);
        
            // Check if Wheel direction is ClockWise
            if(WheelDirection == 1){
                // Turn right
                Wheel.rotation = Quaternion.Slerp(Wheel.rotation, Quaternion.Euler(0, 0, -90), Time.deltaTime * WheelSpeed);
                    
                // Check if Wheel finished turning right
                if(Quaternion.Angle(Wheel.rotation,Quaternion.Euler(0, 0, 0)) > 89){
                    // Change direction
                    WheelDirection = 0;
                }
            }
            // Check if Wheel direction is Counter ClockWise
            if(WheelDirection == 0){
                // Turn back to original position
                Wheel.rotation = Quaternion.Slerp(Wheel.rotation, Quaternion.Euler(0, 0, 0), Time.deltaTime * WheelSpeed);
                // Check if Wheel reached original position
                if(Quaternion.Angle(Wheel.rotation,Quaternion.Euler(0, 0, 0)) < 1){
                    // Change direction
                    WheelDirection = 1;
                    IsWheelTurningRight = 0;
                }
            }
        }

        // Check if Wheel is turning to unsure that the movement is not interrupted
        if(Input.GetKey(KeyCode.A)){
            
            if(IsWheelTurningLeft == 0){
                IsWheelTurningLeft = 1;
                IsWheelTurningRight = 0;
            }

        }

        if(IsWheelTurningLeft == 1){

            // Debug.Log(Quaternion.Angle(Wheel.rotation,Quaternion.Euler(0, 0, 0)));
            // Debug.Log(WheelDirection);
            
            // Check if Wheel direction is Counter ClockWise
            if(WheelDirection == 0){
                // Turn left
                Wheel.rotation = Quaternion.Slerp(Wheel.rotation, Quaternion.Euler(0, 0, 90), Time.deltaTime * WheelSpeed);
                // Check if Wheel reached original position
                if(Quaternion.Angle(Wheel.rotation,Quaternion.Euler(0, 0, 0)) > 89){
                    // Change direction
                    WheelDirection = 1;
                }
            }

            // Check if Wheel direction is ClockWise
            if(WheelDirection == 1){
                // Turn right
                Wheel.rotation = Quaternion.Slerp(Wheel.rotation, Quaternion.Euler(0, 0, 0), Time.deltaTime * WheelSpeed);
                    
                // Check if Wheel finished turning right
                if(Quaternion.Angle(Wheel.rotation,Quaternion.Euler(0, 0, 0)) < 1){
                    // Change direction
                    WheelDirection = 0;
                    IsWheelTurningLeft = 0;
                }
            }
        }
    }
}