// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2024
//                www.magicbane.com

package engine.wpak;

import engine.gameManager.ConfigManager;
import engine.mbEnums;
import engine.wpak.data.EffectEntry;
import engine.wpak.data.EffectModifier;
import org.pmw.tinylog.Logger;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EffectsParser {

    public static String effectsPath = ConfigManager.DEFAULT_DATA_DIR + "wpak/Effects.cfg";
    public static HashMap<String, EffectEntry> effect_data = new HashMap<>();
    private static final Pattern EFFECT_REGEX = Pattern.compile("(?<=EFFECTBEGIN)(.+?)(?=EFFECTEND)", Pattern.DOTALL);
    private static final Pattern SOURCE_REGEX = Pattern.compile("(?<=SOURCEBEGIN)(.+?)(?=SOURCEEND)", Pattern.DOTALL);
    private static final Pattern MODS_REGEX = Pattern.compile("(?<=MODSBEGIN)(.+?)(?=MODSEND)", Pattern.DOTALL);
    private static final Pattern CONDITIONS_REGEX = Pattern.compile("(?<=CONDITIONBEGIN)(.+?)(?=CONDITIONEND)", Pattern.DOTALL);
    private static final Pattern STRSPLIT_REGEX = Pattern.compile("([^\"]\\S*|\"[^\"]*\")\\s*"); // Regex ignores spaces within quotes

    public static void parseWpakFile() {

        // Read .wpak file from disk

        byte[] fileData = null;

        try {
            fileData = Files.readAllBytes(Paths.get(effectsPath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String fileContents = new String(fileData);

        // Iterate over effect entries from .wpak data

        Matcher matcher = EFFECT_REGEX.matcher(fileContents);

        while (matcher.find()) {
            EffectEntry effectEntry = parseEffectEntry(matcher.group());
            effect_data.put(effectEntry.effect_id, effectEntry);
        }
    }

    private static EffectEntry parseEffectEntry(String effectData) {
        
        EffectEntry effectEntry = new EffectEntry();

        // Remove all lines that contain a # and leading/trailing blank lines

        effectData = effectData.replaceAll("(?m)^(\\s*#.*|\\s*)\r?\n?", "");
        effectData = effectData.trim();

        // Parse effect entry header

        String firstLine;
        ArrayList<String> effectHeader = new ArrayList<>();

        // Some effects exist without sources/mods or conditions
        // (ACID "MOB" 0)

        if (effectData.indexOf('\n') > 0)
            firstLine = effectData.substring(0, effectData.indexOf('\n'));
        else
            firstLine = effectData;

        Matcher matcher = STRSPLIT_REGEX.matcher(firstLine);

        while (matcher.find())
            effectHeader.add(matcher.group().trim());

        effectEntry.effect_id = effectHeader.get(0);
        effectEntry.effect_name = effectHeader.get(1);
        effectEntry.effect_name = effectEntry.effect_name.replaceAll("\"", "");

        // Some effect mods have no icon
        // (SEEINVIS-SHADE "See Invis")

        if (effectHeader.size() == 3)
            effectEntry.icon = Integer.parseInt(effectHeader.get(2));
        else
            effectEntry.icon = 0;

        // Parse source entries

        matcher = SOURCE_REGEX.matcher(effectData);

        while (matcher.find())
            effectEntry.sources.add(matcher.group().trim());

        // Parse modifier entries

        matcher = MODS_REGEX.matcher(effectData);

        // Iterate effect entries from .wpak config data

        while (matcher.find()) {
            EffectModifier effectModifier = parseModEntry(matcher.group());
            effectEntry.mods.add(effectModifier);
        }

        // Parse Conditions

        matcher = CONDITIONS_REGEX.matcher(effectData);

        while (matcher.find()) {
            String[] conditions = matcher.group().trim().split("\n");

            for (String condition : conditions) {
                String[] parameters = condition.trim().split("\\s+");
                effectEntry.conditions.put(parameters[0], Float.parseFloat(parameters[1]));
            }
        }

        return effectEntry;
    }

    private static EffectModifier parseModEntry(String modData) {

        EffectModifier effectModifier = new EffectModifier();

        String[] modEntries = modData.trim().split("\n");

        for (String modEntry : modEntries) {

            ArrayList<String> modValues = new ArrayList<>();
            Matcher matcher = STRSPLIT_REGEX.matcher(modEntry.trim());

            while (matcher.find())
                modValues.add(matcher.group().trim());

            effectModifier.type = mbEnums.ModType.valueOf(modValues.get(0).trim());

            switch (effectModifier.type) {
                case BladeTrails:  // No parm modifiers
                case ImmuneToAttack:
                case ImmuneToPowers:
                case Ambidexterity:
                case Silenced:
                case IgnorePassiveDefense:
                case Stunned:
                case PowerCostHealth:
                case Charmed:
                case Fly:
                case CannotMove:
                case CannotTrack:
                case CannotAttack:
                case CannotCast:
                case SpireBlock:
                case Invisible:
                case SeeInvisible:
                    break;
                case AnimOverride:
                    effectModifier.min = Float.parseFloat(modValues.get(1).trim());
                    effectModifier.max = Float.parseFloat(modValues.get(2).trim());
                    break;
                case Health:
                case Mana:
                case Stamina:
                    effectModifier.min = Float.parseFloat(modValues.get(1).trim());
                    effectModifier.max = Float.parseFloat(modValues.get(2).trim());
                    effectModifier.scale = Float.parseFloat(modValues.get(3).trim());
                    // Parameter 4 is always 0.
                    effectModifier.compoundCurveType = mbEnums.CompoundCurveType.valueOf(modValues.get(5).trim());
                    effectModifier.arg1 = modValues.get(6).trim();
                    break;
                case Attr:
                case Resistance:
                case Skill:
                case HealthRecoverRate:
                case ManaRecoverRate:
                case StaminaRecoverRate:
                case DamageShield:
                case HealthFull:
                case ManaFull:
                case StaminaFull:
                case Slay:
                case Fade:
                case Durability:
                    effectModifier.min = Float.parseFloat(modValues.get(1).trim());
                    effectModifier.scale = Float.parseFloat(modValues.get(2).trim());
                    effectModifier.compoundCurveType = mbEnums.CompoundCurveType.valueOf(modValues.get(3).trim());

                    if (modValues.size() > 4)
                        effectModifier.arg1 = modValues.get(4).trim();   // Some HeathFull entries do not have an argument
                    break;
                case MeleeDamageModifier:
                case OCV:
                case DCV:
                case AttackDelay:
                case AdjustAboveDmgCap:
                case DamageCap:
                case ArmorPiercing:
                case Speed:
                case PowerDamageModifier:
                case DR:
                case PassiveDefense:
                case MaxDamage:
                case Value:
                case WeaponSpeed:
                case MinDamage:
                case PowerCost:
                case Block:
                case Parry:
                case Dodge:
                case ScanRange:
                case ScaleHeight:
                case ScaleWidth:
                case WeaponRange:
                    effectModifier.min = Float.parseFloat(modValues.get(1).trim());
                    effectModifier.scale = Float.parseFloat(modValues.get(2).trim());
                    effectModifier.compoundCurveType = mbEnums.CompoundCurveType.valueOf(modValues.get(3).trim());
                    break;
                case ItemName:
                case BlockedPowerType:
                case ImmuneTo:
                case BlackMantle:
                    effectModifier.arg1 = modValues.get(1).trim();

                    // Some BlockedPowerType entries have only one argument

                    if (modValues.size() > 2)
                        effectModifier.arg2 = modValues.get(2).trim();
                    break;
                case NoMod:
                case ConstrainedAmbidexterity:
                case ProtectionFrom:
                case ExclusiveDamageCap:
                case IgnoreDamageCap:
                    effectModifier.arg1 = modValues.get(1).trim();
                    break;
                case WeaponProc:
                    effectModifier.min = Float.parseFloat(modValues.get(1).trim());
                    effectModifier.arg1 = modValues.get(2).trim();
                    effectModifier.scale = Float.parseFloat(modValues.get(3).trim());
                    break;
                default:
                    Logger.error("Unhandled type: " + effectModifier.type);
                    break;
            }
        }

        return effectModifier;
    }

}
