using UnityEngine;
using System;

public class Clock : MonoBehaviour {
    public Action onFinish;
    private float Length;
    private float currentTime = 0;
    private bool hasStarted = false;
    private GameObject arrow;

    public void Awake() {
        arrow = gameObject.FindChildByName("r_Arrow");
    }

    public void Show() {
        LeanTween.moveLocalY(gameObject,6,0.5f).setEase(LeanTweenType.easeInBounce);
    }

    public void Hide(bool animated=true) {
        if (!animated) {
            transform.localPosition = new Vector3(transform.localPosition.x,10,transform.localPosition.z);
            return;
        }

        LeanTween.moveLocalY(gameObject, 10, 0.2f).setEase(LeanTweenType.easeOutCubic);
    }

    public void Stop() {
        hasStarted = false;
    }

    public void StartClock(float length) {
        Length = length;
        hasStarted = true;
        currentTime = 0;
    }

    private void Update() {
        if (!hasStarted) {
            return; }

        currentTime += Time.deltaTime;

        if (currentTime < Length) {
            arrow.transform.localRotation = Quaternion.Euler(360 * (currentTime / Length) - 90, 0, 0);
        } else {
            if (onFinish != null) {
                onFinish(); }

            hasStarted = false;
        }
    }
}
