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

import de.nplusc.izc.tools.IOtools.FileTK;
import de.nplusc.izc.tools.IOtools.iZformats.IZTile;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author LH
 */
public class TileRegistry
{
    private HashMap<String,IZTile>  tileBuffer = new HashMap<>();
    private HashMap<String,Boolean> Removables = new HashMap<>();
    private HashMap<String,String>  rmPaths = new HashMap<>();
    private String location;
    /**
     * Create/Load TileRegistry
     * @param path path of Registryfile and its cahced tiles incase of them on removable Media
     */
    public TileRegistry(String path)
    {
        location = path;
        //Zeilenformat name|path|isRemovableMedia (boolean)
        //FileTK.ensuredirExistence(path);
        FileTK.ensuredirExistence(path+"\\cache+\\cache.map");
        if(new File(path+"\\tiles.iZreg").exists()&&(new File(path+"\\tiles.iZreg").length()>3))
        {
            String[] reglines = FileTK.fileload(path+"\\tiles.iZreg");
            for (String line : reglines)
            {
                if(line.length()>5)
                {
                    String[] linedata = line.split("\\|");
                    boolean isRM = Boolean.valueOf(linedata[2]);
                    Removables.put(linedata[0], isRM);
                    if(isRM)
                    {
                        if(new File(linedata[1]).exists())
                        {
                            tileBuffer.put(linedata[0],new IZTile(linedata[1]));
                            FileTK.kopierpaste(new File(linedata[1]), new File(path+"\\cache+\\"+linedata[0]));//cahce aktualisieren
                        }
                        else
                        {
                            tileBuffer.put(linedata[0],new IZTile(path+"\\cache+\\"+linedata[0]));
                            rmPaths.put(linedata[0],linedata[1]);//remap um cahceumleitung bein speixxern zu killen
                        }
                    }
                    else
                    {
                        tileBuffer.put(linedata[0],new IZTile(linedata[1]));
                    }
                }
                
            }
        }
        else
        {
            tileBuffer = new HashMap<>();
            Removables = new HashMap<>(); 
        }
        
        
    }       
    
    
    public void addTile(String TilePath,String name,boolean isOnRemovableMedia)
    {
        tileBuffer.put(name,new IZTile(TilePath));
        Removables.put(name, isOnRemovableMedia);
        if(isOnRemovableMedia)
        {
            FileTK.kopierpaste(new File(TilePath), new File(location+"\\cache+\\"+name));//cahce aktualisieren
            rmPaths.put(name,TilePath);
                   
        }
        saveRegistry();
    }
    
    public void saveRegistry()
    {
        String[] names = tileBuffer.keySet().toArray(new String[]{});
        ArrayList<String> outFiles = new ArrayList<>();
        for (String tile : names)
        {
            String tilePath = tileBuffer.get(tile).getStoragePath();
            if(rmPaths.containsKey(tile))
            {
                tilePath = rmPaths.get(tile);
            }
            boolean isRemMed = Removables.get(tile);
            String line = tile+"|"+tilePath+"|"+isRemMed;
            outFiles.add(line);
        }
        String writedown = "";
        for (String ln : outFiles)
        {
            writedown+=ln+"\n";
        }
        FileTK.writeFile(writedown.trim(), location+"\\tiles.iZreg");
    }
    
    public IZTile getTile(String name)
    {
        return tileBuffer.get(name);
    }
    public String[] getNames()
    {
        return tileBuffer.keySet().toArray(new String[]{});
    }
}
