﻿using AsmodeeDigital.CrossPromo.Plugin.Utils;
using AsmodeeDigital.CrossPromo.Plugin.View;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace AsmodeeDigital.CrossPromo.Plugin.UI
{
    public class InterstitialPopup : MoreGamesPopup
    {
        [Serializable]
        public class UIInterstitial
        {
            public Button SkipButton;
            public TextMeshProUGUI TextSkipButton;
        }

        public UIInterstitial uiInterstitial;

        public int DelayBeforeSkip = 3;

        private float _startTime;

        private void OnEnable()
        {
            _startTime = Time.time;
        }

        private void Start()
        {
            DeviceScaler.Instance.DeviceOrientationChangedEvent += Refresh;
        }

        private void Update()
        {
            int delaySkip = (int)(DelayBeforeSkip - (Time.time - _startTime));

            if (delaySkip > 0)
            {
                uiInterstitial.SkipButton.interactable = false;
                uiInterstitial.TextSkipButton.text = string.Format("Skip ({0})", delaySkip);
            }
            else
            {
                uiInterstitial.SkipButton.interactable = true;
                uiInterstitial.TextSkipButton.text = "Skip";
            }
        }

        public override void Show()
        {
            base.Show();

            Refresh();
        }

        protected override void Refresh()
        {
            if (this.gameObject.activeInHierarchy)
                CrossPromoView.Instance.CrossPromoLogic.InterstitialRequest();
        }
    }
}
