/*
 * Created on 2005-03-24
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2005
 */
package pl.zalewski.mikolaj.symbdev.utils;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

/**
 * Get's a filename string as in the MMP file and returns a string
 * we show to the user and the path type
 * 
 * @author mikolaj
 */
public class FileStringType {

    /**
     * this is a path relative to the workspace root
     */
    static public final int PATH_WORKSPACE=1;
    
    /**
     * this is an absolute FS path
     */
    static public final int PATH_FILESYSTEM=2;
    
    /**
     * this ia a path relative to the provided root (usually EPOCROOT)
     */
    static public final int PATH_ROOT=3;
    
    /**
     * this is an undefined path
     */
    static public final int PATH_NOT_FOUND=4;
    
    /**
     * The path type - one of the PATH_* constants
     */
    public int fType;
    /**
     * The string to display to the user
     */
    public String fString;
    /**
     * If type is PATH_NOT_FOUND we store here the addition
     */
    public String fAddition;
    
    public static final int TYPE_FILE=1;
    public static final int TYPE_DIR=2;
    
    /**
     * Checks the path for the specified parameters
     * @param path The path string, as found in the MMP file
     * @param rootPath Where the path beginning with '\' are relative to
     * @param current The current directory
     * @param relativeAddition A change of the current directory or
     * <code>null</code> if none
     */
    public FileStringType(String path, String rootPath, 
            String current, String relativeAddition, int type)
    {
        if (!Path.ROOT.isValidPath(path))
        {
            fType=PATH_NOT_FOUND;
            fString=path;
            return;
        }
        
        if (path.charAt(0)=='\\')
        {
            fType=PATH_ROOT;
            fString=path;
        } else
        {
            IPath p=new Path(current);
            if (relativeAddition!=null)
            {
                if (relativeAddition.charAt(0)=='\\')
                {
                    fType=PATH_ROOT;
                    fString=new Path(relativeAddition).append(path).toOSString();
                    return;
                } else p=p.append(relativeAddition);
            }
            p=p.append(path);
            if (!p.toFile().exists() ||
                    (p.toFile().isDirectory() && (type&TYPE_DIR)==0) ||
                    (p.toFile().isFile() && (type&TYPE_FILE)==0))
            {
                fType=PATH_NOT_FOUND;
                fAddition=relativeAddition;
                fString=path;
                return;
            }
            
            IContainer c=ResourcesPlugin.getWorkspace().getRoot()
            	.getContainerForLocation(p);
            if (c==null)
            {
                fType=PATH_FILESYSTEM;
                fString=p.toOSString();
            } else
            {
                fType=PATH_WORKSPACE;
                fString=c.getFullPath().makeRelative().toString();
            }
        }
    }
    
    /**
     * Checks the path for the specified parameters
     * @param path The path string, as found in the MMP file
     * @param rootPath Where the path beginning with '\' are relative to
     * @param current The current directory
     */
    public FileStringType(String path, String rootPath, 
            String current)
    {
        this(path, rootPath, current, null, TYPE_DIR);
    }
    
    public String getStringName(String rootName)
    {
        switch (fType)
        {
        	case PATH_NOT_FOUND: 
        	    if (fAddition==null) return fString;
        	    	else return "'"+fString+"' in '"+fAddition;
        	case PATH_FILESYSTEM: return "'"+fString+"' in filesystem";
        	case PATH_WORKSPACE: return "'"+fString+"' in workspace";
        	case PATH_ROOT: return "'"+fString+"' in "+rootName;
        	default: throw new IllegalStateException();
        }
    }
    
    public String getStringName()
    {
        return getStringName("SDK root");
    }
}
