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


package engine.ConfigParsing;
import engine.ConfigParsing.EffectsData.Condition;
import engine.ConfigParsing.EffectsData.EffectData;
import engine.ConfigParsing.EffectsData.Mod;
import engine.gameManager.ConfigManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class EffectsParser {
    public static String EffectsPath = ConfigManager.DEFAULT_DATA_DIR + "Effects.cfg";
    public static void init() throws IOException {
        ArrayList<ArrayList<String>> compiledData = new ArrayList<>();
        String[] lines = readLines(EffectsPath);
        ArrayList<String> data = new ArrayList<>();
        for(String line : lines)
        {
            if (line.contains("EFFECTBEGIN"))
            {
                data = new ArrayList<>();
            } else if(!line.contains("EFFECTEND"))
            {
                data.add(line);
            }
            else if (line.contains("EFFECTEND"))
            {
                compiledData.add(data);
            }
        }
        CreateBlocks(compiledData);
    }
    public static String[] readLines(String filename) throws IOException {
        File effectsCfg = new File(EffectsPath);
        for(String line : effectsCfg.list()){
            int i = 0;
        }
        return effectsCfg.list();


        //FileReader fileReader = new FileReader(filename);
        //BufferedReader bufferedReader = new BufferedReader(fileReader);
        //List<String> lines = new ArrayList<>();
       // String line;
        //while ((line = bufferedReader.readLine()) != null) {
       //     lines.add(line);
        //}
        //bufferedReader.close();
        //return lines.toArray(new String[0]);
    }
    public static void CreateBlocks(ArrayList<ArrayList<String>> compiledData)
    {
        for (ArrayList<String> lines : compiledData)
        {
            EffectData data = new EffectData();
            data.id = lines.get(1).replace("     ", "").split(" ")[0];
            try
            {
                data.name = lines.get(1).replace("     ", "").split(" ")[1];
            }
            catch(Exception e)
            {

            }
            try
            {
                data.icon = Integer.parseInt(lines.get(1).replace("     ", "").split(" ")[2]);
            }
            catch(Exception e)
            {

            }
            //log all sources
            data.sources = new ArrayList<>();
            int index = 0;
            for(String line : lines) {
            if (line.contains("SOURCEBEGIN"))
            {
                data.sources.add(lines.get(index).replace("          ", ""));
            }
            index++;
        }

            //log all mods
            data.mods = new ArrayList<>();
            index = 0;


            for (String line : lines)
            {
                if (line.contains("MODSBEGIN"))
                {
                    int extra = 1;
                    Mod mod = new Mod();
                    while(!lines.get(index + extra).contains("MODSEND"))
                    {
                        //data.mods.add(lines[index + extra].Replace("          ", ""));
                        mod.type = lines.get(index + extra).replace("          ", "").split(" ")[0];
                        GenerateModValues(mod, lines.get(index + extra).replace("          ", "").replace("    ", "").split(" "));
                        extra++;
                    }
                    data.mods.add(mod);
                }
                index++;
            }

            //log all conditions
            data.conditions = new ArrayList<>();
            index = 0;
            for (String line : lines)
            {
                if (line.contains("CONDITIONBEGIN"))
                {
                    int extra = 1;
                    while (!lines.get(index + extra).contains("CONDITIONEND"))
                    {
                        if (!lines.get(index + extra).contains("#"))
                        {
                            //data.conditions.add(lines[index + extra].Replace("          ", ""));
                            Condition condition = new Condition();
                            condition.type = lines.get(index + extra).replace("          ", "").split(" ")[0];
                            condition.value = lines.get(index + extra).replace("          ", "").split(" ")[1];
                        }
                        extra++;
                    }
                }
                index++;
            }
        }
    }
    public static void GenerateModValues(Mod inMod, String[] data)
    {
        inMod.values = new ArrayList<>();
        for(int i = 1; i < data.length; i++)
        {
            if(data[i].length() != 0)
            {
                inMod.values.add(data[i]);
            }
        }
    }
}