using Domain;
using Domain.Input;
using Presenter;
using TMPro;
using UnityEngine;
using UnityEngine.Serialization;

namespace View.UI {
	public class ExpressionInput : MonoBehaviour, IExpressionInput {
		[SerializeField] private TMP_Text text;

		private ExpressionClick _click;
		private CustomInput _customInput;
		private Model _model;

		[SerializeField] private FramedAnimator.FramedAnimator animator;

		private void Start() {
			_model = FindObjectOfType<Dependencies>().Model;

			_click = FindObjectOfType<Dependencies>().ExpressionClick;
			_customInput = FindObjectOfType<Dependencies>().CustomInput;
		}

		private void Update() {
			CheckInput();
		}

		private void CheckInput() {
			_customInput.UpdateInput();

			if (_customInput.KeyDown(typeof(GeminadaReader)))
				_click.Execute();
			if (_customInput.KeyDown(typeof(CedillaReader)))
				_click.Execute();
			if (_customInput.KeyDown(typeof(IxReader)))
				_click.Execute();
			if (_customInput.KeyDown(typeof(LlReader)))
				_click.Execute();
			if (_customInput.KeyDown(typeof(NyReader)))
				_click.Execute();
			if (_customInput.KeyDown(typeof(SsReader)))
				_click.Execute();
			if (_customInput.KeyDown(typeof(TgReader)))
				_click.Execute();
			if (_customInput.KeyDown(typeof(TjReader)))
				_click.Execute();
			if (_customInput.KeyDown(typeof(TxReader)))
				_click.Execute();
		}

		public void UpdateView(int score) {
			int frameDifference = score % _model.SpawnRate;
			animator.PlayUntil(frameDifference / (float)_model.SpawnRate);

			text.text = score.ToString();
		}
	}
}