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

import de.nplusc.izc.tools.UiToolz.DirectoryTree;
import de.nplusc.izc.tools.baseTools.Tools;
import java.util.ArrayList;
import javax.swing.SwingUtilities;

/**
 *
 * @author LH
 */
public class DirectoryDB extends DirectoryTree
{
    
    DBWriter dbc;
    public DirectoryDB(DBWriter dbcr,String dirlisttable,String filelisttable)
    {                                                                                                         //ignore müssen nullable sein da sie nicht verarbeitet werden
        //schema des structparameters: type:name|type:name (avail basetypes: id,parent,name,descript,realpath,ignore
        this(dbcr,dirlisttable,"id:ID|name:name|parent:parent",filelisttable,"id:ID|parent:Mountpoint|name:name|descript:Description|realpath:RealPath");
    }
    //private DirectoryTreeNode root;
    private String dirRowParentID="",dirRowID="",DirRowName="";
    private String dirTable;
                                                             //optional. kann bei nichtnotwendigkeit ersetzt werden
    private String fileRowParentID="",FileRowID="",FileRowName="",FileRowDesc="",FileRowRealPath="";
    private String fileTable;
    
    
    
    
    
    public DirectoryDB(DBWriter dbcr,String dirlisttable,String dirtableStruct,String filelisttable,String filetableStruct)
    {
        dbc = dbcr;
        dirTable = dirlisttable;
        fileTable = filelisttable;
        for (String fdata : dirtableStruct.split("\\|"))
        {
            String[] fdatas = fdata.split(":");
            switch(fdatas[0])
            {
                case "id":
                    dirRowID = fdatas[1];
                break;
                case "parent":
                    dirRowParentID = fdatas[1];
                break;
                case "name":
                    DirRowName = fdatas[1];
                break;
            }
            

            
        }
        for (String fdata : filetableStruct.split("\\|"))
        {
            String[] fdatas = fdata.split(":");
            switch(fdatas[0])
            {
                case "id":
                    FileRowID = fdatas[1];
                break;
                case "parent":
                    fileRowParentID = fdatas[1];
                break;
                case "name":
                    FileRowName = fdatas[1];
                break;
                case "descript":
                    FileRowDesc =  fdatas[1];
                break;
                case "realpath":
                    FileRowRealPath =  fdatas[1];
                break;
            }
        }
        rebuildDatabase();
    }
    
    @Override
    public String getRootPathValue()
    {
        return "root";
    }
           
    
    @Override
    protected void rebuildDatabase()
    {
        directoryTree = new ArrayList<>();//reset
        String[][] directoryTable = dbc.queryTable("Select "+dirRowID+","+dirRowParentID+","+DirRowName+" from "+dirTable);
        String[][] filetable = dbc.queryTable("Select "+FileRowID+","+fileRowParentID+","+FileRowName+","+FileRowRealPath+","+FileRowDesc+" from "+fileTable);  
        //files haben IDs mit F davor. wird von Scripterl benötigt um DUpeIDs zu verhindern.....
        for (String[] lines : directoryTable)
        {
            directoryTree.add(new DirectoryTreeNode(lines[0], lines[1],lines[2], true));
        }
        for (String[] lines : filetable)
        {//ID,ParentID;Name,Mountpoint,Description
            DirectoryTreeNode dtn = new DirectoryTreeNode("F"+lines[0], lines[1],lines[2], false);
            dtn.addAdditionalData(0, lines[3]);
            dtn.addAdditionalData(1, lines[4]);
            directoryTree.add(dtn);
        }

    }
    
    private DirectoryTreeNode ut;
    @Override
    public void insertDirectory(DirectoryTreeNode node)
    {
        String targetID = node.getParentID();
                    //insert into table (key1,key2,...) values(value1,value2,...)
        String inserterQuery = "Insert into "+dirTable+ " ("+
                               dirRowID+","+dirRowParentID+","+DirRowName+") VALUES(null,"+targetID+",'"+node.toString()+"')";
        Tools.DebugHelperPrint(inserterQuery, true, "Toolkit.enableFinerDebug");
        dbc.write(inserterQuery);
        rebuildDatabase();
        ut = null;
        for (DirectoryTreeNode directoryTreeNode : directoryTree)
        {
            if(directoryTreeNode.getNodeID().equals(node.getNodeID()))
            {
                ut=directoryTreeNode;
                Tools.DebugHelperPrint(ut+"|updateTrg", true, "Toolkit.enableFinerDebug");
            }
        }
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                updateTreeTo(ut);
                
            }
        });
    }
    

    
    
    @Override
    public void insertFile(DirectoryTreeNode node)
    {
        String targetID = node.getParentID();
                    //insert into table (key1,key2,...) values(value1,value2,...)
        node.addAdditionalData(0, node.getAdditionalData(0).replaceAll("\\\\", "\\\\\\\\"));
        String inserterQuery = "Insert into "+fileTable+ " ("+
                               FileRowID+","+fileRowParentID+","+FileRowName+","+FileRowRealPath+","+
                                FileRowDesc+") VALUES(null,"+targetID+",'"+node.toString()+"','"
                                    +node.getAdditionalData(0) +"','"+node.getAdditionalData(1) +"')";
        
        Tools.DebugHelperPrint(inserterQuery, true, "Toolkit.enableFinerDebug");
        dbc.write(inserterQuery);
        rebuildDatabase();
        ut = null;
        for (DirectoryTreeNode directoryTreeNode : directoryTree)
        {
            if(directoryTreeNode.getNodeID().equals(node.getNodeID()))
            {
                ut=directoryTreeNode;
                Tools.DebugHelperPrint(ut+"|updateTrg", true, "Toolkit.enableFinerDebug");
            }
        }
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                updateTreeTo(ut);
            }
        });
        //TODO insert nir bestimmte Werte......(die 3
                                                        //erforderlichen und sonst nix  
    }
}

