
package cz.karry.dop.nbmodule.dbg.breakpoints;

import cz.karry.dop.nbmodule.dbg.breakpoints.ValueBreakpoint;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.netbeans.api.debugger.Properties;
//import org.netbeans.modules.php.dbgp.breakpoints.FunctionBreakpoint.Type;
import org.openide.cookies.LineCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.filesystems.URLMapper;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.text.Line;

/**
 *
 * @author ads
 */
public class BreakpointsReader implements Properties.Reader {

    private static final String LINE_NUMBER    = "lineNumber";         // NOI18N

    private static final String URL             = "url";               // NOI18N
    
    private static final String ENABED          = "enabled";           // NOI18N

    private static final String FUNC_NAME       = "functionName";      // NOI18N

    private static final String TYPE            = "type";              // NOI18N 


    public String [] getSupportedClassNames() {
        return new String[] {
            LineBreakpoint.class.getName(),
            ValueBreakpoint.class.getName()
        };
    }

    public Object read( String typeID, Properties properties ) {
        if (typeID.equals(LineBreakpoint.class.getName())) {
            Line line = getLine(properties.getString(URL, null), properties
                    .getInt(LINE_NUMBER, 1));

            if (line == null) {
                return null;
            }
            LineBreakpoint breakpoint = new LineBreakpoint(line);
            if (!properties.getBoolean(ENABED, true)) {
                breakpoint.disable();
            }
            return new LineBreakpoint(line);
        }
        else if (typeID.equals(ValueBreakpoint.class.getName())){
            // FIXME

            return null;
        }
        else {
            return null;
        }
    }

    public void write(Object object, Properties properties) {
        if (object instanceof LineBreakpoint) {
            LineBreakpoint breakpoint = (LineBreakpoint) object;
            FileObject fileObject = breakpoint.getLine().getLookup().lookup(
                    FileObject.class);

            try {
                properties.setString(URL, fileObject.getURL().toString());
                properties.setInt(LINE_NUMBER, breakpoint.getLine()
                        .getLineNumber());
                properties.setBoolean(ENABED, breakpoint.isEnabled());
            }
            catch (FileStateInvalidException ex) {
                log(ex);
            }
        }
        else if ( object instanceof ValueBreakpoint ) {
            ValueBreakpoint breakpoint = (ValueBreakpoint)object;

            
            // FIXME
        }
    }


    private void log( FileStateInvalidException ex ) {
        Logger.getLogger( BreakpointsReader.class.getName() ).log( Level.SEVERE, 
                null, ex );
    }

    private Line getLine(String url, int lineNumber) {
        FileObject file = getFileObject(url);
        if ( file == null ){
            return null;
        }

        DataObject dataObject = null;
        try {
            dataObject = DataObject.find(file);
        } catch (DataObjectNotFoundException ex) {
            return null;
        }

        if (dataObject == null) {
            return null;
        }

        LineCookie lineCookie = dataObject.getCookie(LineCookie.class);
        if (lineCookie == null) {
            return null;
        }

        Line.Set ls = lineCookie.getLineSet();
        if (ls == null) {
            return null;
        }

        try {
            return ls.getCurrent(lineNumber);
        } catch (IndexOutOfBoundsException e) {
            return null;
        } 
    }

    private FileObject getFileObject( String url ) {
        FileObject file;
        try {
            file = URLMapper.findFileObject(new URL(url));
        } catch (MalformedURLException e) {
            return null;
        }

        return file;
    }
}
