/*
 * 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.io.File;
import java.util.HashMap;

/**
 *
 * @author LH
 */
public class ExplorerTree extends DirectoryTree
{
    
    String basePath;
    public ExplorerTree(String basepath)
    {
        basePath=basepath;
        rebuildDatabase();
    }
    @Override
    protected void rebuildDatabase()
    {
        System.err.println("Scanning directory.... please wait");
        String[] dirlist = FileTK.walkDirectoryRecursively(basePath);
        HashMap<String,String>Path2ID=new HashMap<>();
        DirectoryTreeNode temp = null;
        System.err.println(basePath);
        temp = new DirectoryTreeNode("0", "0", (basePath), true);
        Path2ID.put(basePath.substring(0,basePath.length()-1), "0");
        directoryTree.add(temp);
        String fs = Tools.getFormatStrforThisAsMax(dirlist.length);
        System.err.println("Creating  TreeStructure........");
        for (int i = 1; i < dirlist.length; i++)
        {

            String path = dirlist[i];   
            if(path==null)
                continue;
            //System.err.println(i+"|"+path+"||");
            String parpath = FileTK.getFilePath(path);
            String parentID= Path2ID.get(parpath);
            //System.err.println(i+"|"+path+"|"+parentID+"|"+parpath);
            String filename = FileTK.getFileName(path);
            temp = new DirectoryTreeNode(i+"", parentID, filename, FileTK.isDir(path));
            directoryTree.add(temp);
            if(new File(path).isDirectory())
            {
                Path2ID.put(path, i+"");
            }
            if(i%1000==0)
            {
                String op = String.format(fs, i);
                System.err.print(op+"processed");
            }
        }
        System.err.println(dirlist.length+" processed");
    }
    private DirectoryTreeNode ut;
    @Override
    public void insertDirectory(DirectoryTreeNode node)
    {
        //TODO later
    }
    
    @Override
    public String getRootPathValue()
    {
        return getRoot()+"";
    }
    
    @Override
    public void insertFile(DirectoryTreeNode node)
    {
       //NOP
    }
}