/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 20-Dec-2004 by toa02
 *
 */
package uk.ac.ic.doc.kevlar.gui.internal.taskpane.directorypane;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;


/**
 * @author toa02
 *
 */
public class DirectoryNodeProvider {

    
    /**
     * @param root
     * @return An IDirectoryNode pointing to the root current working directory
     * (calculates it new each time). 
     */
    public static IDirectoryNode getRootDirectoryNode(File root) {
        
        Stack<File> fileStack = new Stack<File>();
        
        fileStack.push(root);
        
        File parent = root.getParentFile();
        
        while( parent != null ) {
            fileStack.push(parent);
            parent = parent.getParentFile();
        }
        
        parent = fileStack.pop();
        
        IDirectoryNode wholeTree = makeDirectoryNode(parent, null);
        
        IDirectoryNode retTree = wholeTree;
        
        while ( !fileStack.isEmpty() ) {
            
            retTree = findChild( retTree, fileStack.pop() );
            if( retTree == null ) {
                return wholeTree;
            }
        }
        
        
        return retTree;
    }
    
    
    private static IDirectoryNode findChild( IDirectoryNode cPos, File target ) {
        
        for( IDirectoryTreeNode child : cPos.getChildren() ){
            
            if( child.getFile().equals(target) && child instanceof IDirectoryNode){
                return (IDirectoryNode) child;
            }
            
        }
        
        return null;
        
    }
    
    
    static IDirectoryNode makeDirectoryNode(final File f, final IDirectoryNode parent) {
        
        
        return new IDirectoryNode() {

            public ArrayList<IDirectoryTreeNode> getChildren() {

                if( f == null ) {
                    return new ArrayList<IDirectoryTreeNode>();
                }
                
                ArrayList<IDirectoryTreeNode> list = new ArrayList<IDirectoryTreeNode>();
                
                File[] listA = f.listFiles();
                if(  listA == null ) {
                    return new ArrayList<IDirectoryTreeNode>();
                }
                
                for( File ff : listA ){
                    
                    if( ff.isDirectory() ) {
                        list.add(makeDirectoryNode(ff,this));
                    } else {
                        list.add(makeFileNode(ff));
                    }
                    
                }
                
                Collections.sort(list);
                
                return list;
            }

            public IDirectoryNode getParent() {
                return parent;
            }

            public File getFile() {
                return f;
            }

            public int compareTo(IDirectoryTreeNode arg0) {
                return f.compareTo(arg0.getFile());
            }
        };
        
    }
    
    
    static IFileNode makeFileNode(final File f) {
        return new IFileNode() {
            public File getFile() {
                return f;
            }

            public int compareTo(IDirectoryTreeNode arg0) {
                return f.compareTo(arg0.getFile());
            }
        };
    }
}
