using System;
using UniRx;
using UnityEngine;

namespace MyGame.Scenes.marketing.Scripts
{
    public class ClockView : MonoBehaviour
    {
        [SerializeField] private Transform shortNeedle;
        [SerializeField] private Transform longNeedle;

        private void Start()
        {
            Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1f))
                .Select(_ => DateTime.Now)
                .DistinctUntilChanged(x => x.Minute)
                .Subscribe(x =>
                {
                    SetClock(x);
                }).AddTo(this);
        }

        private void SetClock(DateTime dateTime)
        {
            shortNeedle.localRotation = Quaternion.Euler(Vector3.forward * -dateTime.Hour * 30);
            longNeedle.localRotation = Quaternion.Euler(Vector3.forward * -dateTime.Minute * 6);
        }
    }
}