package org.directcode.ide.core;

import lombok.Getter;
import org.directcode.ide.api.*;
import org.directcode.ide.api.MenuBar;
import org.directcode.ide.plugins.PluginManager;
import org.directcode.ide.util.Logger;
import org.directcode.ide.window.EditorWindow;
import org.directcode.ide.window.laf.LookAndFeelManager;

import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.*;
import java.io.File;

/**
 * Main Entrypoint for the IDE
 * @since 0.0.1
 */
public class Aluminum
{

   /**
    * Primary Instance of our IDE.
    */
   @Getter
   private static Aluminum IDE;

   /**
    * Tab size for the IDE.
    */
   public static int tabSize = 4;

   /**
    * Location of the plugins directory.
    */
   public static File pluginsDir;

   /**
    * The primary window for our application.
    */
   @Getter
   private EditorWindow editor;

   /**
    * Logger for the application.
    */
   private static Logger logger;

   /**
    * PluginManager for our IDE.
    */
   private PluginManager pluginManager;

   /**
    * Start the application.
    */
   private void start()
   {
      // Initialize the look and feel
      initializeLookAndFeel();
      // Start the GUI
      createGui();
      logger = new Logger("Aluminum");
      logger.info("Starting");
      // Initialize the Plugin Manager
      pluginManager = new PluginManager();
      pluginManager.start();
   }

   /**
    * Create the GUI.
    */
   private void createGui()
   {
      editor = new EditorWindow();
   }

   /**
    * Initialize the Look and Feel for our application.
    */
   private void initializeLookAndFeel()
   {
      LookAndFeelManager lookAndFeelManager = new LookAndFeelManager(new NimbusLookAndFeel());
      lookAndFeelManager.setLAF();
   }

   /**
    * Initiate a shutdown of Aluminum.
    */
   public void stop()
   {
      logger.info("Stopping");
      pluginManager.stop();
      System.exit(0);
   }

   /**
    * Main entrypoint.
    *
    * @param args Arguments for application in a String array
    */
   public static void main(String[] args)
   {
      long startTime = System.currentTimeMillis();

      // Ensure the IDE isn't running on a headless system.
      if (GraphicsEnvironment.isHeadless())
      {
         throw new HeadlessException("Unable to detect display");
      }

      // Initialize the plugins directory if it does not exist
      pluginsDir = new File("plugins/");
      pluginsDir.mkdir();

      // Initialize the class, and start
      IDE = new Aluminum();
      IDE.start();

      long endStartTime = System.currentTimeMillis();
      logger.debug("Startup time: " + ((endStartTime - startTime)));

      MenuBar.addMenuItem(0, new JMenu("Test"));
   }

}
