using Adplay.DataContainer;
using Adplay.Model;
using Adplay.Utility;
using Adplay.View;
using System;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

namespace Adplay.Behaviours.Actions
{
    public class AdPlayPopUpButton : AdPlayAction
    {

        public async Task<AdRequestInfo> GetAd(AdPlayTemplate template, CreativeShowType creativeShowType, Action<int> onClick)
        {
            if (template.ClickButton == null)
            {
                Debug.LogError($"Adplay popup button request failed: ClickButton is null");
                return new AdRequestInfo(AdRequestResultState.MissingRequiredComponent, "Adplay popup button request failed: ClickButton is null");
            }
            if (template.Image == null)
            {
                Debug.LogError($"Adplay popup button request failed: Image is null");
                return new AdRequestInfo(AdRequestResultState.MissingRequiredComponent, "Adplay popup button request failed: Image is null");
            }

            var popUpButtonResult = GetPopUpButtonCreative(creativeShowType, out int popUpId);
            var popUpResult = GetPopUpCreative(popUpId);
            int counter = 0;
            while (counter < 10)
            {
                counter++;
                if (popUpResult != null && popUpButtonResult != null)
                    break;
                await Task.Delay(AdPlayUtility.DelayMilliTime);
                popUpResult = GetPopUpCreative(popUpId);
                popUpButtonResult = GetPopUpButtonCreative(creativeShowType, out popUpId);
            }
            if (popUpButtonResult == null && popUpResult == null)
                return new AdRequestInfo(AdRequestResultState.NoActiveAd);

            var sprite = activeAds.GetSprite(popUpButtonResult.Id);
            template.SetSprite(sprite);
            template.AddClick(() =>
            {
                onClick?.Invoke(popUpId);
            });
            engagementData.CreateEngagement(IdGenerator.NewId(), EngagementType.IMP, popUpButtonResult);


            string extraOptionKey = AdExtraOptionType.PopUpButtonText.ToString();
            string buttonText = popUpButtonResult.Config.ContainsKey(extraOptionKey) ?
                    popUpButtonResult.Config[extraOptionKey].ToString() : "";
            if (template.ClickButtonText != null)
                template.ClickButtonText.text = buttonText;

            template.Show();
            return new AdRequestInfo(AdRequestResultState.Success);
        }

        private Creative GetPopUpButtonCreative(CreativeShowType creativeShowType, out int popUpId)
        {
            popUpId = -1;
            if (creativeShowType == CreativeShowType.WeightBased)
            {
                var popUpButtonResult = activeAds.GetWeightBaseCreative(AdPlacementType.ProductPlacement, AdType.PopupButton, CreativeShowType.WeightBased);
                if (popUpButtonResult != null)
                {
                    popUpId = popUpButtonResult.GetRelatedAdId();
                    return popUpButtonResult;
                }
            }
            else if (creativeShowType == CreativeShowType.Permanent)
            {
                var popUpButtonResult = activeAds.GetPermanentCreatives(AdPlacementType.ProductPlacement, AdType.PopupButton, CreativeShowType.Permanent);
                if (popUpButtonResult.Count != 0)
                {
                    var creative = popUpButtonResult.FirstOrDefault();
                    popUpId = creative.GetRelatedAdId();
                    return creative;
                }
            }

            return null;
        }

        private Creative GetPopUpCreative(int adId)
        {
            return activeAds.GetCreativeById(adId);
        }


    }


}
