/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.karry.dop.nbmodule.files;

import cz.karry.dop.nbmodule.dbg.Session;
import java.io.File;
import javax.swing.Action;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.cookies.OpenCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.MultiDataObject;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.*;

/**
 *
 * @author karry
 */
public class DebugAction extends CookieAction {

    @Override
    protected int mode() {
        return MODE_ANY;        
    }

    @Override
    protected Class<?>[] cookieClasses() {
        return new Class[] { OpenCookie.class };
    }

    @Override
    protected void performAction(Node[] activatedNodes) {
        
        
        MultiDataObject d = (MultiDataObject) activatedNodes[0].getCookie(MultiDataObject.class);
        if (d!=null){
            FileObject f = d.getPrimaryFile();
            
            File fileFolder = null;
            if (f.getParent().isFolder()){
                fileFolder = FileUtil.toFile(f.getParent());
            }
            
            File memoryFile = getMemoryFile(fileFolder);
            File file = FileUtil.toFile(f);
            
            Session session = Session.getInstance();
            if (session==null){
                new Session(file,memoryFile,activatedNodes[0]);     
            }else{
                session.changeFiles(file,memoryFile,activatedNodes[0]);
            }
        }else{
            //DopMicroassemblerDataObject d = (DopMicroassemblerDataObject) activatedNodes[0].getCookie(DopMicroassemblerDataObject.class);
            NotifyDescriptor nd = new NotifyDescriptor.Message("Node is not a file!");
            DialogDisplayer.getDefault().notify(nd);
        }
    }

    @Override
    public String getName() {
        return NbBundle.getMessage(DebugAction.class, "Debug");
    }

    @Override
    public HelpCtx getHelpCtx() {
        return new HelpCtx(DebugAction.class);
    }

    @Override
    public boolean asynchronous(){
        return false;
    }

    private File getMemoryFile(File folder) {
        JFileChooser chooser = new JFileChooser(folder);
        chooser.setDialogTitle( NbBundle.getMessage(DebugAction.class, "DA_SelectMemoryFile") );
        
        chooser.addChoosableFileFilter(new FileFilter() {

            @Override
            public boolean accept(File f) {
                int start = f.getName().length() - 4;
                start = start < 0 ? 0 : start;
                return f.isDirectory() || (f.getName().substring(start).equals(".mem"));
            }

            @Override
            public String getDescription() {
                return "*.mem files";
            }
        });
        chooser.showOpenDialog(null);
        if ((chooser.getSelectedFile() != null) && (chooser.getSelectedFile().exists())){
            return chooser.getSelectedFile();
        }
        return null;
    }
}
