/*
 * Created on 2004-08-15
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.properties;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianPrjNature;
import pl.zalewski.mikolaj.symbdev.SymbianSDKEntry;
import pl.zalewski.mikolaj.symbdev.SymbianSDKsList;
import pl.zalewski.mikolaj.symbdev.Win32ToolEntry;
import pl.zalewski.mikolaj.symbdev.Win32ToolsList;

/**
 * @author mikolaj
 *
 * Properties for a project
 */
public class ProjectProperties {

    static final String BUILD_PATH_DEFAULT = "group\\";
    static final String BUILD_PATH_PROPERTY = "BUILD_PATH";
    static final String SRC_PATH_DEFAULT = "src\\";
    static final String SRC_PATH_PROPERTY = "SRC_PATH";
    static final String INC_PATH_DEFAULT = "inc\\";
    static final String INC_PATH_PROPERTY = "INC_PATH";
    static final String ABLD_BUILD_DEFAULT = "urel";
    static final String ABLD_BUILD_PROPERTY = "ABLD_BUILD";
    static final String TOOL_ID_PROPERTY = "WIN32_TOOL_ID";
    static final String SDK_NAME_PROPERTY = "SDK_NAME";
    static final String CMD_STOP_ON_ERROR = "STOP_ON_ERROR";
    private static final String CMD_PROPERTY = "INTERNAL_CMD";
    
    public static final String BUILD_UREL = "urel"; 
    public static final String BUILD_UDEB = "udeb"; 

    private ProjectProperties() {
        super();
    }

    protected static String getProperty(IProject project, String property, String defaultString) throws CoreException {
        String val=project.getPersistentProperty(
				new QualifiedName(SymbdevPlugin.getUniqueIdentifier(), property));
        return ((val!=null)?val:defaultString);
    }
    
    protected static String getSymbbuildProperty(IProject project, String property, String defaultString)
    	throws CoreException
    {
        SymbianPropertiesFile file=SymbianPropertiesFile.getForProject(project);
        return file.getProperty(SymbdevPlugin.getUniqueIdentifier()+"."+property, defaultString);
    }

    protected static void setProperty(IProject project, String property, String value) throws CoreException {
		project.setPersistentProperty(new QualifiedName(SymbdevPlugin.getUniqueIdentifier(), property), value);
    }    

    protected static void setSymbbuildProperty(IProject project, String property, String value)
		throws CoreException
	{
	    SymbianPropertiesFile file=SymbianPropertiesFile.getForProject(project);
	    file.setProperty(SymbdevPlugin.getUniqueIdentifier()+"."+property, value);
	}    
    
    public static String getBuildPath(IProject project) throws CoreException
    {
        String path=getSymbbuildProperty(project, BUILD_PATH_PROPERTY, null);
        if (path!=null) return path;
            else return getProperty(project, BUILD_PATH_PROPERTY, BUILD_PATH_DEFAULT);
    }

    public static String getIncPath(IProject project) throws CoreException
    {
        return getProperty(project, INC_PATH_PROPERTY, INC_PATH_DEFAULT);
    }

    public static String getSrcPath(IProject project) throws CoreException
    {
        return getProperty(project, SRC_PATH_PROPERTY, SRC_PATH_DEFAULT);
    }

    /**
     * Returns the ABLD build
     * @param project The project
     * @return A string - BUILD_UREL, BUILD_UDEB
     * @see BUILD_UREL, BUILD_UDEB
     * @throws CoreException If something goes wrong
     */
    public static String getABLDBuild(IProject project) throws CoreException
    {
        return getProperty(project, ABLD_BUILD_PROPERTY, ABLD_BUILD_DEFAULT);
    }    
    
    public static void setBuildPath(IProject project, String value) throws CoreException
    {
        setSymbbuildProperty(project, BUILD_PATH_PROPERTY, value);
    }

    public static void setIncPath(IProject project, String value) throws CoreException
    {
        setProperty(project, INC_PATH_PROPERTY, value);
        SymbianPrjNature.applyPaths(project);
    }

    public static void setSrcPath(IProject project, String value) throws CoreException
    {
        setProperty(project, SRC_PATH_PROPERTY, value);
    }

    public static void setABLDBuild(IProject project, String value) throws CoreException
    {
        setProperty(project, ABLD_BUILD_PROPERTY, value);
    }    
    
    public static Win32ToolEntry getWin32Tool(IProject prj) throws CoreException
    {
        String s=getProperty(prj, TOOL_ID_PROPERTY, null);
        if (s==null)
            throw new CoreException(new Status(IStatus.ERROR, SymbdevPlugin.getUniqueIdentifier(),
                    SymbdevPlugin.ERROR_NO_TOOL_CONFIG, "No Win32 compilation tool configured. "+
                    "Go to Project > Properties > Symbian OS Development to configure", null));
        
        int id=-1;
        try
        {
            id=Integer.valueOf(s).intValue();
        }
        catch (NumberFormatException e)
        { }
        
        Win32ToolEntry e=Win32ToolsList.getInstance().getEntry(id);
        if (e==null)
            throw new CoreException(new Status(IStatus.ERROR, SymbdevPlugin.getUniqueIdentifier(),
                    SymbdevPlugin.ERROR_NO_TOOL_CONFIG, "Invalid Win32 compilation tool configuration information in project. "+
                    "Go to Project > Properties > Symbian OS Development to configure", null));
        return e;
    }
    
    public static void setWin32ToolId(IProject project, int id) throws CoreException
    {
        setProperty(project, TOOL_ID_PROPERTY, (new Integer(id)).toString());        
    }

    public static SymbianSDKEntry getSymbianSDK(IProject prj) throws CoreException
    {
        String s=getProperty(prj, SDK_NAME_PROPERTY, null);
        if (s==null)
            throw new CoreException(new Status(IStatus.ERROR, SymbdevPlugin.getUniqueIdentifier(),
                    SymbdevPlugin.ERROR_NO_TOOL_CONFIG, "No Symbian SDK configured. "+
                    "Go to Project > Properties > Symbian OS Development to configure", null));
        
        SymbianSDKEntry e=SymbianSDKsList.getInstance().getEntry(s);
        if (e==null)
            throw new CoreException(new Status(IStatus.ERROR, SymbdevPlugin.getUniqueIdentifier(),
                    SymbdevPlugin.ERROR_NO_TOOL_CONFIG, "Invalid Symbian SDK configuration information in project. "+
                    "Go to Project > Properties > Symbian OS development to configure", null));
        return e;
    }
    
    public static void setSymbianSDKName(IProject project, String name) throws CoreException
    {
        setProperty(project, SDK_NAME_PROPERTY, name);        
        SymbianPrjNature.applyPaths(project);
    }
    
    public static boolean getUseInternalCmd(IProject project) throws CoreException
    {
        String s=getProperty(project, CMD_PROPERTY, Boolean.FALSE.toString());
        return Boolean.valueOf(s).booleanValue();
    }

    public static void setUseInternalCmd(IProject project, boolean val) throws CoreException
    {
        setProperty(project, CMD_PROPERTY, new Boolean(val).toString());
    }

    public static boolean getStopOnError(IProject project) throws CoreException
    {
        String s=getProperty(project, CMD_STOP_ON_ERROR, Boolean.FALSE.toString());
        return Boolean.valueOf(s).booleanValue();
    }

    public static void setStopOnError(IProject project, boolean val) throws CoreException
    {
        setProperty(project, CMD_STOP_ON_ERROR, new Boolean(val).toString());
    }
}
