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

public class SpinDial : MonoBehaviour {
    private float MouseX, MouseY;
    public float presure = 0;
    public int rounded = 0;
    public Text tens;
    public Text ones;

    void Update () {
        presure -= Time.deltaTime;
        if (presure < 0)
        {
            presure = 0;
        }
        if (presure > 99)
        {
            presure = 99;
        }
        int tenval = (int)Mathf.Min(Mathf.Max(Mathf.Floor(presure / 10), 0), 9);
        int oneval = (int)Mathf.Min(Mathf.Max(Mathf.Floor(presure % 10), 0), 9);
        tens.text = ""+tenval;
        ones.text = ""+oneval;
        rounded = (int)presure;
    }

    public void MouseDown() {
        MouseX = Input.mousePosition.x;
        MouseY = Input.mousePosition.y;
    }

    public void MouseHold() {
        Vector2 center = GetComponent<RectTransform>().position;
        float xtmp, ytmp;
        if (Input.mousePosition.y > center.y)
        {
            xtmp = (MouseX - Input.mousePosition.x);
        }
        else
        {
            xtmp = -1*(MouseX - Input.mousePosition.x);
        }
        if (Input.mousePosition.x < center.x)
        {
            ytmp = (MouseY - Input.mousePosition.y);
        }
        else
        {
            ytmp = -1 * (MouseY - Input.mousePosition.y);
        }
        Rotate(xtmp, ytmp);
        MouseX = Input.mousePosition.x;
        MouseY = Input.mousePosition.y;
    }
    
    void Rotate (float X, float Y) {
        presure -= (X + Y) / 20f;
        GetComponent<RectTransform>().Rotate(0, 0, (X + Y)/5f);
    }
}
