/*
 * Copyright 2016 kohii
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.smoothcsv.mac;

import com.apple.eawt.AboutHandler;
import com.apple.eawt.AppEvent.AboutEvent;
import com.apple.eawt.AppEvent.OpenFilesEvent;
import com.apple.eawt.AppEvent.PreferencesEvent;
import com.apple.eawt.AppEvent.QuitEvent;
import com.apple.eawt.Application;
import com.apple.eawt.OpenFilesHandler;
import com.apple.eawt.PreferencesHandler;
import com.apple.eawt.QuitHandler;
import com.apple.eawt.QuitResponse;
import com.smoothcsv.commons.exception.CancellationException;
import com.smoothcsv.framework.Env;
import com.smoothcsv.framework.SCApplication;
import com.smoothcsv.framework.command.CommandRegistry;
import com.smoothcsv.framework.modular.ModuleEntryPointBase;
import com.smoothcsv.framework.modular.ModuleManifest;
import com.smoothcsv.swing.utils.SwingUtils;
import command.app.OpenFileCommand;

import java.io.File;

/**
 * @author Kohii
 */
public class MacEntryPoint extends ModuleEntryPointBase {

  @Override
  public ModuleManifest getManifest() {
    return ModuleManifest.builder()
        .name("smoothcsv-mac")
        .author("kohii")
        .dependencies(new String[]{"smoothcsv-core"})
        .build();
  }

  @Override
  protected void activate() {
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    Env.revalidateUsingMacSystemMenuBar();
    System.setProperty("com.apple.macos.smallTabs", "true");

    Application app = Application.getApplication();

    app.setAboutHandler(new AboutHandler() {
      @Override
      public void handleAbout(AboutEvent e) {
        CommandRegistry.instance().runCommand("app:About");
      }
    });

    app.setDockIconImage(SwingUtils.getImage("/img/appicon.png"));
    app.setOpenFileHandler(new OpenFilesHandler() {
      @Override
      public void openFiles(OpenFilesEvent e) {
        OpenFileCommand command = new OpenFileCommand();
        for (File f : e.getFiles()) {
          if (f.isFile()) {
            command.run(f);
          } else if (f.isDirectory()) {
            command.chooseAndOpenFile(f);
          }
        }
      }
    });

    // TODO
    // app.setOpenURIHandler(new OpenURIHandler() {
    // @Override
    // public void openURI(OpenURIEvent e) {
    //
    // }
    // });

    app.setPreferencesHandler(new PreferencesHandler() {
      @Override
      public void handlePreferences(PreferencesEvent e) {
        CommandRegistry.instance().runCommand("app:ShowSettings");
      }
    });

    app.setQuitHandler(new QuitHandler() {
      @Override
      public void handleQuitRequestWith(QuitEvent ev, QuitResponse res) {
        try {
          if (SCApplication.getApplication().exit()) {
            res.performQuit();
          } else {
            res.cancelQuit();
          }
        } catch (CancellationException e) {
          res.cancelQuit();
        } catch (RuntimeException e) {
          res.performQuit();
        }
      }
    });

    // app.setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS);
  }
}

@@ -1423,16 +1423,39 @@
 io.File;
+%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -2661,23 +2661,8 @@
 %0A   
- // TODO%0A    //
  app
@@ -2665,27 +2665,28 @@
  app.setOpen
-URI
+File
 Handler(new 
@@ -2689,19 +2689,21 @@
 new Open
-URI
+Files
 Handler(
@@ -2710,18 +2710,17 @@
 ) %7B%0A    
-//
+ 
  @Overri
@@ -2726,18 +2726,17 @@
 ide%0A    
-//
+ 
  public 
@@ -2748,52 +2748,344 @@
 open
-URI(OpenURIEvent e) %7B%0A    //%0A    // %7D%0A    //
+Files(OpenFilesEvent openFilesEvent) %7B%0A        List%3CFile%3E files = openFilesEvent.getFiles();%0A        OpenFileCommand command = new OpenFileCommand();%0A        for (File f : files) %7B%0A          if (f.isDirectory()) %7B%0A            command.chooseAndOpenFile(f);%0A          %7D else %7B%0A            command.run(f);%0A          %7D%0A        %7D%0A      %7D%0A   
  %7D);

Add a listener for open file events