using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Text.RegularExpressions;

public class TimeInputField : MonoBehaviour
{
    [SerializeField]
    private TMP_InputField inputField;

    private int hours;
    private int minutes;
    private string newValue;

    private void Start()
    {
        inputField.onValueChanged.AddListener(OnValueChanged);
    }

    private void OnValueChanged(string value)    
    {
        // Check if the input contains only digits
        if (!Regex.IsMatch(value, @"^\d *$"))
        {
            // Remove any non-digit characters from the input
            value = Regex.Replace(value, @"\D", "");
            newValue = value;
            //return;
        }
        else
        {
            newValue = value;
        }

        // Check the input length for the first two characters
        if (newValue.Length == 2)
        {

            // Try parsing the hours
            if (int.TryParse(newValue, out hours))
            {
                // Check if hours is not greater than 24
                if (hours > 24)
                {
                    hours = 24;
                }

                // Update the input field with the formatted hours
                inputField.text = string.Format("{0:D2} (ч) :", hours);
                return;
            }
        }

        // Check the input length for the next two characters
        if (newValue.Length == 4)
        {

            // Extract minutes
            string minutesStr = newValue.Substring(2, 2);

            // Try parsing the minutes
            if (int.TryParse(minutesStr, out minutes))
            {
                // Check if minutes is greater than 60
                if (minutes > 60)
                {
                    // Calculate the exceeding minutes and add them to hours
                    int exceedingMinutes = minutes - 60;
                    int existingHours = 0;

                    // Extract existing hours from the input field
                    if (inputField.text.Length >= 9)
                    {
                        string existingHoursStr = inputField.text.Substring(0, 2);
                        int.TryParse(existingHoursStr, out existingHours);
                    }

                    // Add the exceeding minutes to the existing hours
                    int newHours = existingHours + 1;
                    int newMinutes = exceedingMinutes;

                    // Check if new hours is not greater than 24
                    if (newHours > 24)
                    {
                        newHours = 24;
                    }

                    // Apply the desired format
                    string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", newHours, newMinutes);
                    inputField.text = formattedValue;
                    return;
                }
                else
                {
                    // Apply the desired format
                    string formattedValue = string.Format("{0} : {1:D2} (мин)", inputField.text, minutes);
                    inputField.text = formattedValue;
                    return;
                }
            }
        }
    }
}
в этом коде баг что при вводе минут значения дублируются "02 (ч) :01 (мин) : 01 (мин)" можно ли как-то избежать этого дублирования?