using UnityEngine;
using UnityEngine.UI;

namespace Innoactive.AccessControl
{
    /// <summary>
    /// Exits the application when the authorization process failed.
    /// </summary>
    public class DisplayErrorOnAuthorizationFailed : MonoBehaviour
    {
        /// <summary>
        /// Class taking care of the authorization process.
        /// </summary>
        [SerializeField]
        private AuthorizationManager authorizationManager;

        [SerializeField]
        private Text textField;

        private void Awake()
        {
            authorizationManager.OnAuthorizationFailed += ((sender, args) =>
            {
                Debug.LogError("Authorization failed.");
                AnimateTextDots textAnimator = textField.gameObject.GetComponent<AnimateTextDots>();
                if (textAnimator != null)
                {
                    textAnimator.enabled = false;
                }
#if UNITY_ANDROID
                textField.text = "Authorization failed.\nPlease make sure the Access Control Client is installed and that you are authorized to run the application.\nIn case of doubt, please contact your administrator";
#else
                textField.text = "Authorization failed.\nPlease contact your administrator.";
#endif
                Camera.main.backgroundColor = Color.red;
            });
        }

        private void Reset()
        {
            if (authorizationManager == null)
            {
                authorizationManager = GetComponent<AuthorizationManager>();
            }
        }
    }
}
