using Adplay.Model;
using Adplay.Utility;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Adplay.DataContainer
{
    [CreateAssetMenu(fileName = nameof(ActiveAds), menuName = PathUtility.DataContainerPath + nameof(ActiveAds))]
    public class ActiveAds : ScriptableObject
    {
        [field: SerializeField] private List<Creative> Creatives { get; set; } = new();

        [SerializeField] private SoundAds soundAds;
        [SerializeField] private TextureAds textureAds;

        public void Clear()
        {
            Creatives.Clear();
            soundAds.Clear();
            textureAds.Clear();

        }

        public List<string> GetUrls()
        {
            return Creatives.Where(c => c.AdPlacementType == AdPlacementType.ProductPlacement).Select(c => c.AssetUrl).ToList();
        }

        public void SetActiveCreatives(List<Creative> creatives)
        {
            Creatives = creatives;
        }


        public void AddCreative(Creative creative, Texture2D texture, AudioClip audioClip)
        {
            if (creative.AdPlacementType == AdPlacementType.ProductPlacement)
            {
                textureAds.Add(creative, texture);
            }
            else if (creative.AdPlacementType == AdPlacementType.VoicePlacement)
            {
                soundAds.Add(creative, audioClip);
            }
            else if (creative.AdPlacementType == AdPlacementType.VideoPlacement)
            {

            }
        }

        public TextureAdInfo GetActiveAd(AdType creativeAdType)
        {
            return textureAds.GetSprite(creativeAdType);
        }


    }

    [System.Serializable]
    public class AdInfo
    {
        [field: SerializeField] public int Id { get; private set; }
        [field: SerializeField] public Creative Creative { get; set; }
        [field: SerializeField] public Texture2D Texture { get; set; }
        [field: SerializeField] public Sprite Sprite { get; private set; }

        public AdInfo(int id, Creative creative, Texture2D texture)
        {
            Id = id;
            Creative = creative;
            Texture = texture;
            if (texture != null)
                Sprite = texture.TextureToSprite();
        }
    }
}
