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

namespace Adplay.Behaviours.Actions
{
    public class AdPlaySplash : AdPlayAction
    {
        private List<SplashInfo> splashDurationInfos = new();

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

            var ad = GetCreative(creativeShowType);
            int counter = 0;
            while (ad == null && counter < 10)
            {
                await Task.Delay(AdPlayUtility.DelayMilliTime);
                ad = GetCreative(creativeShowType);
                counter++;
            }
            if (ad == null)
            {
                return new AdRequestInfo(AdRequestResultState.NoActiveAd);
            }


            Sprite sprite = activeAds.GetSprite(ad.Id);
            template.Hide();
            template.SetSprite(sprite);
            engagementData.CreateEngagement(ad.Id, EngagementType.IMP, ad);

            var splashInfo = CreateSplashInfo(ad.Id);
            template.OnShow += () =>
            {
                splashInfo.LastShowTime = Time.time;
            };

            template.OnDeactive += () =>
            {
                splashInfo.CalculateDuration();
                engagementData.UpdateDuration(ad.Id, splashInfo.Duration);
            };
            template.Show();

            return new AdRequestInfo(AdRequestResultState.Success);
        }


        private SplashInfo CreateSplashInfo(int adId)
        {
            var info = new SplashInfo()
            {
                AdId = adId,
            };
            splashDurationInfos.Add(info);
            return info;
        }


        protected Creative GetCreative(CreativeShowType creativeShowType)
        {
            if (creativeShowType == CreativeShowType.Permanent)
            {
                return activeAds.GetPermanentCreatives(AdPlacementType.ProductPlacement, AdType.Splash, creativeShowType)
                    .FirstOrDefault();
            }
            else if (creativeShowType == CreativeShowType.WeightBased)
            {
                return activeAds.GetWeightBaseCreative(AdPlacementType.ProductPlacement, AdType.Splash, creativeShowType);
            }
            else
                return null;

        }


        [System.Serializable]
        private class SplashInfo
        {
            [field: SerializeField] public int AdId { get; set; }
            [field: SerializeField] public float LastShowTime { get; set; }
            [field: SerializeField] public float LastHideTime { get; set; }
            [field: SerializeField] public float Duration { get; private set; }

            public void CalculateDuration()
            {
                LastHideTime = Time.time;
                Duration += LastHideTime - LastShowTime;
            }
        }

    }
}
