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


namespace Machete {
  public class Clock : MonoBehaviour {
    [SerializeField]
    Transform smallTick;
    [SerializeField]
    Transform largeTick;
    GameManager game;

    Vector3 smallTickStart = new Vector3(1, 0, 0);
    Vector3 smallTickTarget = new Vector3(0.9f, -0.4f, 0);
    bool halfPassed = false;

    Quaternion largeTickStart = Quaternion.AngleAxis(90, Vector3.forward);
    Quaternion largeTickTarget = Quaternion.AngleAxis(-90, Vector3.forward);
    Quaternion largeTickEnd = Quaternion.AngleAxis(-270, Vector3.forward);

    void Start () {
      game = GameManager.Instance;

      Quaternion.AngleAxis(180, Vector3.forward);
    }

    private void Update() {
      float ratio = game.RemainingRatio;
      smallTick.right = Vector3.Lerp(smallTickStart, smallTickTarget, ratio);

      if (!halfPassed && ratio >= 0.5f) {
        largeTick.rotation = largeTickTarget;
        largeTickStart = largeTickTarget;
        largeTickTarget = largeTickEnd;
        halfPassed = true;
        return;
      }

      largeTick.rotation = Quaternion.Lerp(
        largeTickStart, largeTickTarget, !halfPassed ? ratio * 2f : (ratio - 0.5f) * 2f
      );
    }
  }
}
