package org.beatbot;

import org.beatbot.injection.generic.RSClient;
import org.beatbot.script.ScriptDef;
import org.beatbot.script.ScriptLoader;
import org.beatbot.ui.BotLogger;
import org.beatbot.utils.Utilities;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * Created by Snorflake on 8/11/2015.
 */
public class BeatBot extends JFrame{

    public static BeatBot instance = null;

   // public static BotMenuBar botMenuBar;
    private MenuBar menuBar;
    private Menu fileMenu;

    public MenuItem getStartScriptOption() {
        return startScriptOption;
    }

    public MenuItem getPauseScriptOption() {
        return pauseScriptOption;
    }

    public MenuItem startScriptOption;
    public MenuItem pauseScriptOption;
    private Menu viewMenu;

    public static CheckboxMenuItem players, npc;
    public BeatBot()
    {

        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
        ImageIcon icon = new ImageIcon(getClass().getResource("/resources/boot_pulse.gif"));
        JLabel label = new JLabel(icon,JLabel.CENTER);
        add(label, BorderLayout.CENTER);

        //botMenuBar = new BotMenuBar();

        //add(botMenuBar,BorderLayout.NORTH);





        setLocationRelativeTo(null);

        BotLogger botLogger = new BotLogger();
        add(botLogger, BorderLayout.SOUTH);
        //botLogger.redirectSystemStreams();
        setVisible(true);
        setTitle("BeatBot V0 by Aria");
        getContentPane().setBackground(Color.BLACK);
        setSize(780, 662);
        System.out.println("BeatBot remake has been started!\n");
        RSClient client = new RSClient();
        remove(label);
        add(client, BorderLayout.NORTH);
        menuBar = new MenuBar();
        fileMenu = new Menu("File");

        startScriptOption = new MenuItem(RSClient.getClient().currentScript == null ? "Start Script" : "Stop script");
        startScriptOption.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startScriptOptionActionPerformed(e);
            }


        });
        fileMenu.add(startScriptOption);
        pauseScriptOption = new MenuItem((RSClient.getClient().currentScript != null && RSClient.getClient().currentScript.isPaused) ? "Resume Script" : "Pause Script");
        pauseScriptOption.setEnabled(RSClient.getClient().currentScript != null);
        pauseScriptOption.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pauseScriptOptionActionPerformed(e);
            }
        });
        fileMenu.add(pauseScriptOption);
        menuBar.add(fileMenu);
        viewMenu = new Menu("View");


        setLocationRelativeTo(null);
        players = new CheckboxMenuItem("Players");
        npc = new CheckboxMenuItem("NPCs");
        viewMenu.add(players);
        viewMenu.add(npc);
        menuBar.add(viewMenu);
        setMenuBar(menuBar);
        validate();
        Utilities.sleep(1000);
        setSize(780, 663);

        System.out.println("Loaded successfully!");

        setLocationRelativeTo(null);
    }
    public String getDefaultDirectory() {
        try {
            return new File(".").getCanonicalPath();
        } catch (IOException e) {
            return System.getProperty("user.dir");
        }
    }
    private void startScriptOptionActionPerformed(ActionEvent e) {
        if(RSClient.getClient().currentScript == null)
        {
            JFileChooser c = new JFileChooser();
            c.setCurrentDirectory(new File(getDefaultDirectory()));
            c.setDialogTitle("Select script...");
            c.setFileSelectionMode(JFileChooser.FILES_ONLY);
            c.setFileFilter(new FileFilter() {
                @Override
                public boolean accept(File f) {
                    return f.getName().toLowerCase().endsWith(".class") || f.getName().toLowerCase().endsWith(".jar") || f.isDirectory();
                }

                @Override
                public String getDescription() {
                    return "BeatBot scripts";
                }
            });
            c.setAcceptAllFileFilterUsed(false);
            if(c.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
            {
                ScriptLoader loader = null;
                loader = new ScriptLoader(c.getSelectedFile().toString());
                if(loader != null)
                {
                    try {
                        Class<?> scriptClass = loader.loadClass(c.getSelectedFile().getName().substring(0,c.getSelectedFile().getName().indexOf(".")));

                        Object scriptObject = scriptClass.newInstance();
                        if(scriptObject instanceof ScriptDef)
                        {
                            RSClient.getClient().currentScript = (ScriptDef) scriptObject;
                            RSClient.getClient().currentScript.start();
                            startScriptOption.setLabel("Stop Script");
                            pauseScriptOption.setLabel("Pause Script");
                            pauseScriptOption.setEnabled(true);
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }else {
            startScriptOption.setLabel("Start Script");
            pauseScriptOption.setLabel("Pause Script");
            pauseScriptOption.setEnabled(false);
            RSClient.getClient().currentScript.stop();
            RSClient.getClient().currentScript = null;
            Runtime.getRuntime().gc();
        }
    }

    public void pauseScriptOptionActionPerformed(java.awt.event.ActionEvent evt) {
        if (pauseScriptOption.getLabel().contains("Pause")) {
            pauseScriptOption.setLabel("Resume Script");
            RSClient.getClient().currentScript.pause();
        } else {
            pauseScriptOption.setLabel("Pause Script");
            RSClient.getClient().currentScript.unpause();
        }
    }
    public static void main(String... args)
    {

        instance = new BeatBot();
    }
}
