using UnityEngine;
using System.Collections.Generic;

namespace MaskShop
{
    [CreateAssetMenu(fileName = "RequestGenerator", menuName = "Mask Shop/Request Generator")]
    public class RequestGenerator : ScriptableObject
    {
        [Header("Available Parameters")]
        [SerializeField] private List<MaskMood> availableMoods = new List<MaskMood>();
        [SerializeField] private List<MaskPalette> availablePalettes = new List<MaskPalette>();
        [SerializeField] private List<MaskTexture> availableTextures = new List<MaskTexture>();
        [SerializeField] private List<MaskGimmick> availableGimmicks = new List<MaskGimmick>();

        [Header("Generation Settings")]
        [SerializeField, Range(0f, 1f)] private float omissionChance = 0.2f;

        /// <summary>
        /// Generates a random mask request with procedural text
        /// </summary>
        public MaskRequest GenerateRequest()
        {
            // Determinar qué parámetros se omiten (20% de probabilidad cada uno)
            MaskMood mood = ShouldOmitParameter() ? null : GetRandomMood();
            MaskPalette palette = ShouldOmitParameter() ? null : GetRandomPalette();
            MaskTexture texture = ShouldOmitParameter() ? null : GetRandomTexture();
            MaskGimmick gimmick = ShouldOmitParameter() ? null : GetRandomGimmick();

            // Crear el request
            MaskRequest request = new MaskRequest(mood, palette, texture, gimmick);

            // Generar el texto de la solicitud
            request.requestText = GenerateRequestText(request);

            return request;
        }

        private bool ShouldOmitParameter()
        {
            return Random.value < omissionChance;
        }

        private MaskMood GetRandomMood()
        {
            return availableMoods.Count > 0 ? availableMoods[Random.Range(0, availableMoods.Count)] : null;
        }

        private MaskPalette GetRandomPalette()
        {
            return availablePalettes.Count > 0 ? availablePalettes[Random.Range(0, availablePalettes.Count)] : null;
        }

        private MaskTexture GetRandomTexture()
        {
            return availableTextures.Count > 0 ? availableTextures[Random.Range(0, availableTextures.Count)] : null;
        }

        private MaskGimmick GetRandomGimmick()
        {
            return availableGimmicks.Count > 0 ? availableGimmicks[Random.Range(0, availableGimmicks.Count)] : null;
        }

        /// <summary>
        /// Generates the request text using different sentence structures
        /// </summary>
        private string GenerateRequestText(MaskRequest request)
        {
            // Seleccionar una estructura aleatoria
            int structureIndex = Random.Range(0, 10);

            return structureIndex switch
            {
                0 => GenerateStructure1(request),
                1 => GenerateStructure2(request),
                2 => GenerateStructure3(request),
                3 => GenerateStructure4(request),
                4 => GenerateStructure5(request),
                5 => GenerateStructure6(request),
                6 => GenerateStructure7(request),
                7 => GenerateStructure8(request),
                8 => GenerateStructure9(request),
                9 => GenerateStructure10(request),
                _ => GenerateStructure1(request)
            };
        }

        // Estructura 1: "Necesito una máscara que exprese {mood}, de colores {palette}, con textura {texture} y que además tenga {gimmick}"
        private string GenerateStructure1(MaskRequest request)
        {
            string text = "I need a mask";
            bool hasContent = false;

            if (request.HasMood())
            {
                text += $" that expresses {request.mood.ParameterName}";
                hasContent = true;
            }

            if (request.HasPalette())
            {
                text += hasContent ? ", with colors" : " with colors";
                text += $" {request.palette.ParameterName}";
                hasContent = true;
            }

            if (request.HasTexture())
            {
                text += hasContent ? ", with a texture of type" : " with a texture of type";
                text += $" {request.texture.ParameterName}";
                hasContent = true;
            }

            if (request.HasGimmick())
            {
                text += hasContent ? " and that also has" : " that has";
                text += $" {request.gimmick.ParameterName}";
            }

            return text + ".";
        }

        // Estructura 2: "Vengo a por mi máscara de {mood}, con los colores de {palette}, que se sienta como {texture} y no olvides añadir {gimmick}"
        private string GenerateStructure2(MaskRequest request)
        {
            string text = "I'm here for my mask";
            bool hasContent = false;

            if (request.HasMood())
            {
                text += $" of {request.mood.ParameterName}";
                hasContent = true;
            }

            if (request.HasPalette())
            {
                text += hasContent ? ", with colors from" : " with colors from";
                text += $" {request.palette.ParameterName}";
                hasContent = true;
            }

            if (request.HasTexture())
            {
                text += hasContent ? ", that feels like" : " that feels like";
                text += $" {request.texture.ParameterName}";
                hasContent = true;
            }

            if (request.HasGimmick())
            {
                text += hasContent ? " and don't forget to add" : " with";
                text += $" {request.gimmick.ParameterName}";
            }

            return text + ".";
        }

        // Estructura 3: "Quiero una máscara {texture}, con algo de {gimmick}, que represente {mood}, con colores de tipo {palette}"
        private string GenerateStructure3(MaskRequest request)
        {
            string text = "I want a mask";
            bool hasContent = false;

            if (request.HasTexture())
            {
                text += $" {request.texture.ParameterName}";
                hasContent = true;
            }

            if (request.HasGimmick())
            {
                text += hasContent ? ", with some" : " with some";
                text += $" {request.gimmick.ParameterName}";
                hasContent = true;
            }

            if (request.HasMood())
            {
                text += hasContent ? ", that represents" : " that represents";
                text += $" {request.mood.ParameterName}";
                hasContent = true;
            }

            if (request.HasPalette())
            {
                text += hasContent ? ", with colors of type" : " with colors of type";
                text += $" {request.palette.ParameterName}";
            }

            return text + ".";
        }

        // Estructura 4: "Hazme una máscara con {palette} y {texture}, debe transmitir {mood} e incluir {gimmick}"
        private string GenerateStructure4(MaskRequest request)
        {
            string text = "Make me a mask";
            bool hasContent = false;

            if (request.HasPalette())
            {
                text += $" with {request.palette.ParameterName}";
                hasContent = true;
            }

            if (request.HasTexture())
            {
                text += hasContent ? $" and {request.texture.ParameterName}" : $" {request.texture.ParameterName}";
                hasContent = true;
            }

            if (request.HasMood())
            {
                text += hasContent ? ", it should convey" : " that conveys";
                text += $" {request.mood.ParameterName}";
                hasContent = true;
            }

            if (request.HasGimmick())
            {
                text += hasContent ? " and include" : " with";
                text += $" {request.gimmick.ParameterName}";
            }

            return text + ".";
        }

        // Estructura 5: "Me gustaría una máscara que tenga {gimmick}, con acabado {texture}, tonos {palette} y expresión de {mood}"
        private string GenerateStructure5(MaskRequest request)
        {
            string text = "I would like a mask";
            bool hasContent = false;

            if (request.HasGimmick())
            {
                text += $" that has {request.gimmick.ParameterName}";
                hasContent = true;
            }

            if (request.HasTexture())
            {
                text += hasContent ? ", with a finish of" : " with a finish of";
                text += $" {request.texture.ParameterName}";
                hasContent = true;
            }

            if (request.HasPalette())
            {
                text += hasContent ? ", tones" : " in tones";
                text += $" {request.palette.ParameterName}";
                hasContent = true;
            }

            if (request.HasMood())
            {
                text += hasContent ? " and an expression of" : " with an expression of";
                text += $" {request.mood.ParameterName}";
            }

            return text + ".";
        }

        // Estructura 6: "Búscame una máscara de aspecto {texture}, {mood}, decorada con {gimmick} y paleta {palette}"
        private string GenerateStructure6(MaskRequest request)
        {
            string text = "Find me a mask";
            bool hasContent = false;

            if (request.HasTexture())
            {
                text += $" with a {request.texture.ParameterName} look";
                hasContent = true;
            }

            if (request.HasMood())
            {
                text += hasContent ? $", {request.mood.ParameterName}" : $" {request.mood.ParameterName}";
                hasContent = true;
            }

            if (request.HasGimmick())
            {
                text += hasContent ? ", decorated with" : " decorated with";
                text += $" {request.gimmick.ParameterName}";
                hasContent = true;
            }

            if (request.HasPalette())
            {
                text += hasContent ? " and palette" : " with palette";
                text += $" {request.palette.ParameterName}";
            }

            return text + ".";
        }

        // Estructura 7: "Confecciona una máscara pintada en {palette}, con {gimmick}, sensación {texture} y que evoque {mood}"
        private string GenerateStructure7(MaskRequest request)
        {
            string text = "Craft a mask";
            bool hasContent = false;

            if (request.HasPalette())
            {
                text += $" painted in {request.palette.ParameterName}";
                hasContent = true;
            }

            if (request.HasGimmick())
            {
                text += hasContent ? ", with" : " with";
                text += $" {request.gimmick.ParameterName}";
                hasContent = true;
            }

            if (request.HasTexture())
            {
                text += hasContent ? ", with a" : " with a";
                text += $" {request.texture.ParameterName} feel";
                hasContent = true;
            }

            if (request.HasMood())
            {
                text += hasContent ? " and that evokes" : " that evokes";
                text += $" {request.mood.ParameterName}";
            }

            return text + ".";
        }

        // Estructura 8: "Prepara mi máscara con toques de {palette}, {gimmick} visible, textura {texture} y sentimiento de {mood}"
        private string GenerateStructure8(MaskRequest request)
        {
            string text = "Prepare my mask";
            bool hasContent = false;

            if (request.HasPalette())
            {
                text += $" with touches of {request.palette.ParameterName}";
                hasContent = true;
            }

            if (request.HasGimmick())
            {
                text += hasContent ? $", {request.gimmick.ParameterName} visible" : $" with {request.gimmick.ParameterName} visible";
                hasContent = true;
            }

            if (request.HasTexture())
            {
                text += hasContent ? ", texture" : " with texture";
                text += $" {request.texture.ParameterName}";
                hasContent = true;
            }

            if (request.HasMood())
            {
                text += hasContent ? " and a feeling of" : " with a feeling of";
                text += $" {request.mood.ParameterName}";
            }

            return text + ".";
        }

        // Estructura 9: "Elabora una máscara que refleje {mood}, superficie {texture}, adornada con {gimmick} en tonos {palette}"
        private string GenerateStructure9(MaskRequest request)
        {
            string text = "Create a mask";
            bool hasContent = false;

            if (request.HasMood())
            {
                text += $" that reflects {request.mood.ParameterName}";
                hasContent = true;
            }

            if (request.HasTexture())
            {
                text += hasContent ? ", surface" : " with surface";
                text += $" {request.texture.ParameterName}";
                hasContent = true;
            }

            if (request.HasGimmick())
            {
                text += hasContent ? ", adorned with" : " adorned with";
                text += $" {request.gimmick.ParameterName}";
                hasContent = true;
            }

            if (request.HasPalette())
            {
                text += hasContent ? " in tones of" : " in tones of";
                text += $" {request.palette.ParameterName}";
            }

            return text + ".";
        }

        // Estructura 10: "Crea una máscara impregnada de {mood}, con materiales {texture}, colores {palette} y el detalle de {gimmick}"
        private string GenerateStructure10(MaskRequest request)
        {
            string text = "Create a mask";
            bool hasContent = false;

            if (request.HasMood())
            {
                text += $" infused with {request.mood.ParameterName}";
                hasContent = true;
            }

            if (request.HasTexture())
            {
                text += hasContent ? ", with materials" : " with materials";
                text += $" {request.texture.ParameterName}";
                hasContent = true;
            }

            if (request.HasPalette())
            {
                text += hasContent ? ", colors" : " with colors";
                text += $" {request.palette.ParameterName}";
                hasContent = true;
            }

            if (request.HasGimmick())
            {
                text += hasContent ? " and the detail of" : " with the detail of";
                text += $" {request.gimmick.ParameterName}";
            }

            return text + ".";
        }
    }
}