package com.tiedup.remake.dialogue;

import com.tiedup.remake.personality.PersonalityType;
import java.util.EnumSet;
import java.util.Set;
import org.jetbrains.annotations.Nullable;

/**
 * Conditions for when a dialogue entry can be used.
 *
 * Personality System: Data-driven dialogue
 */
public class DialogueCondition {

    /** Personality types this dialogue applies to (null = all) */
    @Nullable
    private final Set<PersonalityType> personalities;

    /** Minimum mood value (-100 to 100) */
    private final int moodMin;

    /** Maximum mood value (-100 to 100) */
    private final int moodMax;

    private DialogueCondition(Builder builder) {
        this.personalities = builder.personalities;
        this.moodMin = builder.moodMin;
        this.moodMax = builder.moodMax;
    }

    /**
     * Check if this condition matches the given context.
     */
    public boolean matches(DialogueContext context) {
        // Check personality
        if (personalities != null && !personalities.isEmpty()) {
            if (!personalities.contains(context.getPersonality())) {
                return false;
            }
        }

        // Check mood
        if (context.getMood() < moodMin || context.getMood() > moodMax) {
            return false;
        }

        return true;
    }

    /**
     * Create a condition that matches all contexts (no restrictions).
     */
    public static DialogueCondition any() {
        return new Builder().build();
    }

    /**
     * Create a condition for specific personalities.
     */
    public static DialogueCondition forPersonalities(PersonalityType... types) {
        return new Builder().personalities(types).build();
    }

    public static Builder builder() {
        return new Builder();
    }

    // Getters
    @Nullable
    public Set<PersonalityType> getPersonalities() {
        return personalities;
    }

    public int getMoodMin() {
        return moodMin;
    }

    public int getMoodMax() {
        return moodMax;
    }

    /**
     * Builder for DialogueCondition.
     */
    public static class Builder {

        private Set<PersonalityType> personalities = null;
        private int moodMin = -100;
        private int moodMax = 100;

        public Builder personalities(PersonalityType... types) {
            this.personalities = EnumSet.noneOf(PersonalityType.class);
            for (PersonalityType type : types) {
                this.personalities.add(type);
            }
            return this;
        }

        public Builder personalities(Set<PersonalityType> types) {
            this.personalities = types;
            return this;
        }

        /**
         * No-op: training conditions are ignored (training system removed).
         * Kept for backward compatibility with JSON files that still reference training_min.
         */
        public Builder trainingMin(Object level) {
            return this;
        }

        /**
         * No-op: training conditions are ignored (training system removed).
         * Kept for backward compatibility with JSON files that still reference training_max.
         */
        public Builder trainingMax(Object level) {
            return this;
        }

        public Builder moodMin(int min) {
            this.moodMin = min;
            return this;
        }

        public Builder moodMax(int max) {
            this.moodMax = max;
            return this;
        }

        public Builder moodRange(int min, int max) {
            this.moodMin = min;
            this.moodMax = max;
            return this;
        }

        /**
         * No-op: resentment conditions are ignored (training system removed).
         */
        public Builder resentmentMin(int min) {
            return this;
        }

        public Builder resentmentMax(int max) {
            return this;
        }

        public Builder resentmentRange(int min, int max) {
            return this;
        }

        public DialogueCondition build() {
            return new DialogueCondition(this);
        }
    }
}
