/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.InstallPak;

import de.nplusc.izc.tools.baseTools.Messagers;
import de.nplusc.izc.tools.baseTools.Tools;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 *
 * @author LH
 */
public class InstallManager
{
    public static final int 
            VERSION_MAJOR = 2,
            VERSION_MINOR=0,
            REVISION=0;

    public static final String VERSION_GUI_TXT="izSetup "+VERSION_MAJOR+"."+VERSION_MINOR+"."+REVISION+" (c) 2013 iZc";
    
    
    public static InstallManager Main;
    private String[][] InstallQueue;
    private String[] Groups;
    private String[] enabledGroupIDs;
    private HashMap<String,ArrayList<String[]>> groupsplitted = new HashMap<>();
    static{
        Main=  new InstallManager();
      //  Main.setInstallScript(ModeSel.filePath);
    }
    
    public String[] getToggleAblePrograms()
    {
        Arrays.sort(Groups);
        return Groups;
    }
    
    public void setInstallScript(String path)
    {
        String[][] data = izsetupReader.getSysSpezSetupFile(path);
        if(data==null)
        {
            Messagers.SingleLineMsg("WTF?!?!?!?!?!?!?!?", "...");
            System.exit(0xfa11);
        }
        String groupID="000"; 
        String previousGRPID ="000";
        
        ArrayList<String[]> SubGroup = new ArrayList<>();
        for (String[] line : data)
        {
            if(!(line==null))
            {
                //DBG_START
                try{
                Tools.DebugHelperPrint(line[0]+"|GRP", true, "iZsetup.enableFinerDebug");
                Tools.DebugHelperPrint(line[1]+"|KEY", true, "iZsetup.enableFinerDebug");
                Tools.DebugHelperPrint(line[2]+"|VAL", true, "iZsetup.enableFinerDebug");
                //Tools.DebugHelperPrint(splittedLine[3]+"|CMNT", true, "iZsetup.enableFinerDebug");
                //DBG_STOP
                }
                catch(Exception ignored)//wenn 4ter null da kein CMNT feld vorhanden solls nicht errorn
                {
                    
                }
                if(!line[0].equals("000"))
                {
                    groupID=line[0];
                    if(!previousGRPID.equals(groupID)&&!previousGRPID.equals("000"))
                    {
                        groupsplitted.put(previousGRPID, SubGroup);//Einlagern der abgeschlossenen gruppe
                        SubGroup = new ArrayList<>();//resetten für neue daten;
                    }
                    SubGroup.add(line);
                    previousGRPID=groupID;

                }
            }
        }
        groupsplitted.put(previousGRPID, SubGroup);//die letzte auch noch einflanschen
        String[] groupIDs = groupsplitted.keySet().toArray(new String[0]);
        ArrayList<String> lenabledGroupIDs = new ArrayList<>();
        ArrayList<String> lGroups = new ArrayList<>();
        for (String id : groupIDs)
        {
            lenabledGroupIDs.add(id);
            
            if(groupsplitted.get(id).get(0)[1].equalsIgnoreCase("grp")||groupsplitted.get(id).get(0)[1].equalsIgnoreCase("group"))
            {
                String groupname = groupsplitted.get(id).get(0)[2];//angelt sich erste zeile der Gruppe davon 3. Wert
                                                                                //wenn nicht mit grp oder group eingeleitetdann immer ausgeführt
                lGroups.add(id+"|"+groupname);
            }
        }
        Groups = lGroups.toArray(new String[0]);
        enabledGroupIDs=lenabledGroupIDs.toArray(new String[0]);
    }
    public void setGroupState(String grpid,boolean state)
    {
        boolean exists = false;
        for (int i = 0; i < enabledGroupIDs.length; i++)
        {
            String id = enabledGroupIDs[i];
            if(grpid.equals(id))
            {
                exists=true;
                if(!state)
                {
                    enabledGroupIDs[i]="";
                }
            }
        }
        ArrayList<String> newGrps = new ArrayList<>();
        for (String cnt : enabledGroupIDs)
        {
            if(cnt.length()>1)
            {
                newGrps.add(cnt);
            }
        }
        if(!exists&&state)
        {
            newGrps.add(grpid);
        }
        enabledGroupIDs = newGrps.toArray(new String[0]);
    }
    public String[][] getInstallQueue()
    {
        return InstallQueue;
    }
    
    
    public void createQueue()
    {
        ArrayList<String[]> TempQueue = new ArrayList<>();
        Arrays.sort(enabledGroupIDs);
        //System.err.println("nop");
        for (String id : enabledGroupIDs)
        {
            ArrayList<String[]> loadedGroup = groupsplitted.get(id);
            for (String[] lines : loadedGroup)
            {
                String[] linedata =lines;
                if(linedata[1].equalsIgnoreCase("msg")||linedata[1].equalsIgnoreCase("exec")||linedata[1].equalsIgnoreCase("requireExec"))
                {
                    TempQueue.add(Arrays.copyOfRange(lines, 2, lines.length));
                }
            }
        }
        //InstallQueue = TempQueue.toArray(new String[0]);
        InstallQueue=new String[TempQueue.size()][];
        for (int i = 0; i < InstallQueue.length; i++)
        {
            InstallQueue[i]=TempQueue.get(i);
            
        }
    }
}
