package tech.nevets.jaml.gui;

import tech.nevets.jaml.objects.Profile;
import tech.nevets.jaml.objects.Version;
import tech.nevets.jaml.util.LoaderUtils;
import tech.nevets.jaml.util.ProfileUtils;
import tech.nevets.jaml.util.VersionUtils;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class NewProfileGui extends JFrame {

    private JPanel contentPane;

    public NewProfileGui() {
    	setAlwaysOnTop(true);
    	setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                removeFromArray();
            }
        });
        setBounds(100, 100, 360, 330);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        SpringLayout sl_contentPane = new SpringLayout();
        contentPane.setLayout(sl_contentPane);

        JLabel profileNameLabel = new JLabel("Profile Name:");
        sl_contentPane.putConstraint(SpringLayout.NORTH, profileNameLabel, 14, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, profileNameLabel, 10, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, profileNameLabel, 45, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, profileNameLabel, 94, SpringLayout.WEST, contentPane);
        contentPane.add(profileNameLabel);
        JTextField profileNameField = new JTextField(20);
        sl_contentPane.putConstraint(SpringLayout.NORTH, profileNameField, 18, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, profileNameField, 94, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, profileNameField, 41, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, profileNameField, 325, SpringLayout.WEST, contentPane);
        contentPane.add(profileNameField);

        JLabel gamePathLabel = new JLabel("Game Path:");
        sl_contentPane.putConstraint(SpringLayout.NORTH, gamePathLabel, 56, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, gamePathLabel, 10, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, gamePathLabel, 87, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, gamePathLabel, 94, SpringLayout.WEST, contentPane);
        contentPane.add(gamePathLabel);

        JButton gamePathButton = new JButton("...");
        sl_contentPane.putConstraint(SpringLayout.NORTH, gamePathButton, 60, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, gamePathButton, 302, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, gamePathButton, 325, SpringLayout.WEST, contentPane);
        JTextField gamePathField = new JTextField(20);
        sl_contentPane.putConstraint(SpringLayout.NORTH, gamePathField, 60, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, gamePathField, 94, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, gamePathField, 83, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, gamePathField, 303, SpringLayout.WEST, contentPane);
        gamePathButton.addActionListener(ae -> {
            JFileChooser fileChooser = new JFileChooser(System.getenv("APPDATA") + "\\.minecraft\\");
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.showSaveDialog(null);
            if (fileChooser.getSelectedFile() == null) { return; }
            gamePathField.setText(fileChooser.getSelectedFile().toString());
            fileChooser.setVisible(false);
        });
        contentPane.add(gamePathField);
        contentPane.add(gamePathButton);

        Version[] versionsArray = VersionUtils.versionsList;
        ArrayList<String> versions = new ArrayList<>();
        for (Version version : versionsArray) {
            versions.add(version.getId());
        }

        JLabel versionLabel = new JLabel("Version:");
        sl_contentPane.putConstraint(SpringLayout.NORTH, versionLabel, 98, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, versionLabel, 10, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, versionLabel, 129, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, versionLabel, 94, SpringLayout.WEST, contentPane);
        contentPane.add(versionLabel);
        JComboBox versionField = new JComboBox(versions.toArray(new String[versions.size()]));
        sl_contentPane.putConstraint(SpringLayout.NORTH, versionField, 102, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, versionField, 94, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, versionField, 125, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, versionField, 325, SpringLayout.WEST, contentPane);
        contentPane.add(versionField);

        JLabel loaderLabel = new JLabel("Loader:");
        sl_contentPane.putConstraint(SpringLayout.NORTH, loaderLabel, 140, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, loaderLabel, 10, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, loaderLabel, 171, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, loaderLabel, 94, SpringLayout.WEST, contentPane);
        contentPane.add(loaderLabel);

        JComboBox loaderDropdown = new JComboBox(LoaderUtils.getLoaders());
        sl_contentPane.putConstraint(SpringLayout.NORTH, loaderDropdown, 144, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, loaderDropdown, 94, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, loaderDropdown, 167, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, loaderDropdown, 325, SpringLayout.WEST, contentPane);
        contentPane.add(loaderDropdown);

        JLabel offlineModeLabel = new JLabel("Offline Mode:");
        sl_contentPane.putConstraint(SpringLayout.NORTH, offlineModeLabel, 182, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, offlineModeLabel, 10, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, offlineModeLabel, 213, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, offlineModeLabel, 94, SpringLayout.WEST, contentPane);
        contentPane.add(offlineModeLabel);
        JCheckBox offlineModeBox = new JCheckBox("");
        sl_contentPane.putConstraint(SpringLayout.NORTH, offlineModeBox, 185, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, offlineModeBox, 94, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, offlineModeBox, 208, SpringLayout.NORTH, contentPane);
        contentPane.add(offlineModeBox);

        JButton button = new JButton("Create Profile");
        sl_contentPane.putConstraint(SpringLayout.NORTH, button, 243, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, button, 24, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, button, 280, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, button, 241, SpringLayout.WEST, contentPane);
        button.addActionListener(al -> {
            Profile profile = new Profile();
            profile.setProfileName(profileNameField.getText());

            profile.setGamePath(new File(gamePathField.getText()));
            profile.setVersion(versionField.getSelectedItem().toString());
            profile.setLoader(loaderDropdown.getSelectedItem().toString());
            profile.setOfflineMode(offlineModeBox.isSelected());
            try {
                ProfileUtils.createProfile(profile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ProfileUtils.getProfileList();

            this.dispose();
        });
        contentPane.add(button);
        
        JButton cancelButton = new JButton("Cancel");
        sl_contentPane.putConstraint(SpringLayout.NORTH, cancelButton, 243, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.WEST, cancelButton, 242, SpringLayout.WEST, contentPane);
        sl_contentPane.putConstraint(SpringLayout.SOUTH, cancelButton, 280, SpringLayout.NORTH, contentPane);
        sl_contentPane.putConstraint(SpringLayout.EAST, cancelButton, 325, SpringLayout.WEST, contentPane);
        cancelButton.addActionListener(al -> {
            this.dispose();
        });
        contentPane.add(cancelButton);
    }

    private void removeFromArray() {
        GuiHandler.removeFromFramesArray(this);
    }
}


