/*
 * 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.core;

import com.smoothcsv.core.component.SmoothCsvComponentManager;
import com.smoothcsv.core.handler.SmoothCsvErrorHandler;
import com.smoothcsv.core.util.CoreSettings;
import com.smoothcsv.framework.SCApplication;
import com.smoothcsv.framework.component.support.CommandMapFactory;
import com.smoothcsv.framework.error.ErrorHandlerFactory;
import com.smoothcsv.framework.event.SCListener;
import com.smoothcsv.framework.modular.ModuleManifest.Language;
import command.app.NewFileCommand;
import command.app.OpenFileCommand;

import java.io.File;
import java.util.List;
import java.util.Locale;

/**
 * @author kohii
 */
public class SmoothCsvApp extends SCApplication {

  public SmoothCsvApp(int os, boolean debug) {
    super("SmoothCSV", os, debug);
    ErrorHandlerFactory.setErrorHandler(new SmoothCsvErrorHandler());
  }

  @Override
  protected SmoothCsvComponentManager createComponentManager() {
    return new SmoothCsvComponentManager();
  }

  @Override
  protected CommandMapFactory createCommandMapFactory() {
    return new SmoothCsvCommandMapFactory();
  }

  @Override
  protected void handleArgs(String[] args) {
    listeners().on(BeforeOpenWindowEvent.class, new SCListener<BeforeOpenWindowEvent>() {
      @Override
      public void call(BeforeOpenWindowEvent event) {
        if (args == null || args.length == 0) {
          new NewFileCommand().execute();
        } else {
          OpenFileCommand command = new OpenFileCommand();
          for (String filepath : args) {
            File f = new File(filepath);
            if (f.isFile()) {
              command.run(f);
            } else if (f.isDirectory()) {
              command.chooseAndOpenFile(f);
            }
          }
        }
      }
    });
  }

  @Override
  protected void setupLanguageSetting() {
    List<Language> langs = getModuleManager().getAvailableLanguages();
    String langSetting = CoreSettings.getInstance().get(CoreSettings.LANGUAGE);
    String languageToUse = null;
    for (Language lang : langs) {
      if (lang.getId().equals(langSetting)) {
        languageToUse = langSetting;
      }
    }
    if (languageToUse == null) {
      languageToUse = Language.EN.getId();
      CoreSettings.getInstance().save(CoreSettings.LANGUAGE, languageToUse);
    }
    Locale.setDefault(new Locale(languageToUse));
  }
}

@@ -942,57 +942,8 @@
 ry;%0A
-import com.smoothcsv.framework.event.SCListener;%0A
 impo
@@ -1699,32 +1699,27 @@
 rs().on(
-BeforeOpen
 Window
+Opend
 Event.cl
@@ -1727,122 +1727,19 @@
 ss, 
-new SCListener%3CBeforeOpenWindowEvent%3E() %7B%0A      @Override%0A      public void call(BeforeOpenWindowEvent event)
+event -%3E
  %7B%0A
-  
     
@@ -1788,18 +1788,16 @@
         
-  
 new NewF
@@ -1816,26 +1816,24 @@
 .execute();%0A
-  
       %7D else
@@ -1843,18 +1843,16 @@
         
-  
 OpenFile
@@ -1900,18 +1900,16 @@
         
-  
 for (Str
@@ -1941,18 +1941,16 @@
         
- 
- 
 File f =
@@ -1976,26 +1976,24 @@
 ;%0A          
-  
 if (f.isFile
@@ -2002,34 +2002,32 @@
 ) %7B%0A            
- 
- 
 command.run(f);%0A
@@ -2022,26 +2022,24 @@
 and.run(f);%0A
-  
           %7D 
@@ -2062,26 +2062,24 @@
 ectory()) %7B%0A
-  
             
@@ -2112,22 +2112,8 @@
 f);%0A
-            %7D%0A
     

Make app be able to handle arguments