using System;
using UniRx;
using UnityEngine;

namespace MyGame.Scenes.marketing.Scripts
{
    public class ClockView : MonoBehaviour
    {
        [SerializeField] private Transform shortNeedle;
        [SerializeField] private Transform longNeedle;
        private readonly float timing = 60f;

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

        private void SetClock(DateTime dateTime)
        {
            shortNeedle.localRotation = Quaternion.Euler(Vector3.back * (dateTime.Hour * 30 + dateTime.Minute / 2));
            longNeedle.localRotation = Quaternion.Euler(Vector3.back * dateTime.Minute * 6);
        }
    }
}