using System;
using UnityEngine;
using UnityEngine.UI;

public class RealTimeTimeLimitChanger : MonoBehaviour
{
    public GameObject Agent;

    public InputField TimeLimInputField;
    public Text TimeLimPlaceholder;

    public void BTPressed()
    {
        AgentWithGun agentWithGun = Agent.GetComponent<AgentWithGun>();
        InGameMessages messenger = gameObject.GetComponent<InGameMessages>();
        int timeLimit = Math.Abs(int.Parse(TimeLimInputField.GetComponent<InputField>().text));

        if (TimeLimInputField.GetComponent<InputField>().text == "-")
        {
            // input chara not illegal
            TimeLimPlaceholder.color = Color.red;
            TimeLimPlaceholder.text = "Wrong Type!";
            TimeLimInputField.GetComponent<InputField>().text = "";
            messenger.SendMessagetoBox("Wrong timeLimit Type!", Message.MessageType.error);
        }
        else if (TimeLimInputField.GetComponent<InputField>().text == "")
        {
            // empty chara
            TimeLimPlaceholder.color = Color.gray;
            TimeLimPlaceholder.text = "TimeLim";
        }
        else
        {
            int remainTime = agentWithGun.remainTime;
            // make sure new timeLimit is greater than remainTime;
            if (timeLimit <= remainTime)
            {
                TimeLimPlaceholder.color = Color.red;
                TimeLimPlaceholder.text = "Error";
                messenger.SendMessagetoBox($"New time should greater than remainTime({remainTime})",Message.MessageType.error);
            }
            else
            {
                // good to go~
                TimeLimPlaceholder.color = Color.gray;
                TimeLimPlaceholder.text = "TimeLim";
                agentWithGun.timeLimit = timeLimit;
                TimeLimInputField.GetComponent<InputField>().text = "";
                messenger.SendMessagetoBox($"Time Limit changed to {timeLimit}",Message.MessageType.success);
            }
            
        }
    }
}