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

import de.nplusc.izc.tools.IOtools.FileTK;
import de.nplusc.izc.tools.IOtools.registryReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 *
 * @author LH
 */
public class PackManager
{
    private HashMap<String,ArrayList<String[]>> groupsplitted = new HashMap<>();
    private String[] installingPacks,AllPacks,DetectedPacks,queue;
    
    /**
     * Lädt die Liste aller Packs aus der aktiven Datei
     * @return PAckliste im FOrmat ID|Name
     * 
     */
    public String[] getPackList()
    {
        String[] ret = new String[AllPacks.length];
        for (int i=0;i<AllPacks.length;i++)
        {
            String id=AllPacks[i];
            ArrayList<String[]> group=groupsplitted.get(id);
            for (String row[] : group)
            {
                String[] rowSplit = row;
                if(rowSplit[1].equalsIgnoreCase("pack"))
                    ret[i] = id+"|"+rowSplit[2];
            }
        }
        return ret;
    }
    
    public void loadPackageData(String path)
    {
        String[][] data = izsetupReader.getSysSpezSetupFile(path,true,true);
        String groupID="000"; 
        String previousGRPID ="000";
        
        ArrayList<String[]> SubGroup = new ArrayList<>();
        for (String[] line : data)
        {
            if(!(line==null))
            {
                String[] splittedLine=line;
                if(!splittedLine[0].equals("000"))
                {
                    groupID=splittedLine[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]);
        Arrays.sort(groupIDs);
        AllPacks = groupIDs;
        checkForpackInstalled();
        ArrayList<String> installables = new ArrayList<>();
        for (String id : AllPacks)
        {
            if(!packIsInstalled(id))
                installables.add(id);
        }
        installingPacks = installables.toArray(new String[0]);
        Arrays.sort(installingPacks);
    }

    public void setPackInstallState(String packid, boolean state)
    {
        boolean exists = false;
        for (int i = 0; i < installingPacks.length; i++)
        {
            String id = installingPacks[i];
            if(packid.equals(id))
            {
                exists=true;
                if(!state)
                {
                    installingPacks[i]="";
                }
            }
        }
        ArrayList<String> newGrps = new ArrayList<>();
        for (String cnt : installingPacks)
        {
            if(cnt.length()>1)
            {
                newGrps.add(cnt);
            }
        }
        if(!exists&&state)
        {
            newGrps.add(packid);
        }
        installingPacks = newGrps.toArray(new String[0]);
        newGrps = new ArrayList<>();
        for (String cnt : installingPacks)
        {
            if(packIsInstallable(cnt))
                newGrps.add(cnt);
            
        }
        installingPacks = newGrps.toArray(new String[0]);
    }
    public boolean packIsInstallable(String packid)
    {
        return packIsInstallable(packid, false);
    }
    public boolean packIsInstallable(String packid,boolean blocksItself)
    {
        String reqPack = packDependency(packid);
        
        for (String pack : DetectedPacks)//Detektiere obDependency installiert(dependency erfüllt wenn pack bereits installiert
        {
            if(reqPack.equalsIgnoreCase(pack))
                return true;
        }
        if(blocksItself)
        {
            for (String pack : installingPacks)//detektiere obDependency in installierliste
            {
                if(packid.equalsIgnoreCase(pack))
                    return false;
            }
        }
        for (String pack : installingPacks)//detektiere obDependency in installierliste
        {
            if(reqPack.equalsIgnoreCase(pack)&&packIsInstallable(reqPack))
                return true;
        }
        if(reqPack.equals("none"))
            return true;
        return false;
    }
    
    public boolean packIsInstalled(String packid)
    {
        for (String cntnt : DetectedPacks)
        {
            if(cntnt.equalsIgnoreCase(packid))
                return true;
        }
        return false;
    }
    
    
    
    public boolean packIsScheduled(String packid)
    {
        for (String cntnt : installingPacks)
        {
            if(cntnt.equalsIgnoreCase(packid))
                return true;
        }
        return false;
    }
   
    public String packDependency(String packid)
    {
        ArrayList<String[]> selectedPack = groupsplitted.get(packid);
        //002|req|001|nummer des benötigten packs//kann weitere erfordernwenn
        for (String[] line : selectedPack)
        {
            String[] linesegs = line;
            if(linesegs[1].equalsIgnoreCase("req"))
                return linesegs[2];
        }
        
        
        return "none";
    }

    //005|Description|Tools für die games aus dem Package
    public String getPackDescription(String packid)
    {
        ArrayList<String[]> selectedPack = groupsplitted.get(packid);
        for (String[] line : selectedPack)
        {
            String[] linesegs = line;
            if(linesegs[1].equalsIgnoreCase("Description"))
                return linesegs[2];
        }
        return "Error 0x0404: keine Beschreiubung gefunden";
    }
    
    public String getPackName(String packid)
    {
        ArrayList<String[]> selectedPack = groupsplitted.get(packid);
        for (String line[] : selectedPack)
        {
            String[] linesegs = line;
            if(linesegs[1].equalsIgnoreCase("pack"))
                return linesegs[2];
        }
        return "Error 0x0404: kein Name gefunden";
    }
    
    
    private void checkForpackInstalled()
    {
        ArrayList<String> installedPack = new ArrayList<>();
        for (int i=0;i<AllPacks.length;i++)
        {
            String id=AllPacks[i];
            ArrayList<String[]> group=groupsplitted.get(id);
            for (String[] row : group)
            {
                String[] rowSplit = row;
                if(rowSplit[1].equalsIgnoreCase("detectInstall"))
                {
                    String detecString = rowSplit[2];
                    //CMM|File:// benutzt den DateiExistiert-operator
                    //CMM|Reg:// detektiert unter Win auf Exitenz von Regwert
                    if(detecString.substring(0,7).equalsIgnoreCase("File://"))
                    {
                        String detecpart = detecString.substring(7);
                        if(FileTK.checkDirExist(detecpart))
                        {
                            installedPack.add(id);
                        }
                    }
                    if(detecString.substring(0,6).equalsIgnoreCase("Reg://"))
                    {
                        String detecpart = detecString.substring(6);
                        if(registryReader.checkExistenceOfRegKey(detecpart)||registryReader.checkExistenceOfRegKeyEntry(detecpart))
                        {
                            installedPack.add(id);
                        }
                    }
                }
            }
        }
        DetectedPacks = installedPack.toArray(new String[0]);
    }
    public void createQueue()
    {
        Arrays.sort(installingPacks);
        ArrayList<String> queuetmp = new ArrayList<>();
        for (String id : installingPacks)
        {
            ArrayList<String[]> grpldr =groupsplitted.get(id);
            for (String[] line : grpldr)
            {
                String[] data = line;
                if(data[1].equalsIgnoreCase("packfile"))
                {
                    queuetmp.add("packfile:"+data[2]);
                }
                if(data[1].startsWith("waitdisk:"))
                {
                    queuetmp.add("waitdisk:"+data[2]+"|"+data[1].substring(9));
                }
            }
        }
        queue=queuetmp.toArray(new String[0]);
    }
    public String[] getQueue()
    {
        return queue;
    } 
}
