package tech.nevets.jaml.gui;

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

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;

public class FirstLoginGui extends JFrame {
    private JPanel contentPane;

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

        JLabel profileNameLabel = new JLabel("Profile Name:");
        profileNameLabel.setBounds(10, 14, 84, 31);
        contentPane.add(profileNameLabel);
        JTextField profileNameField = new JTextField(20);
        profileNameField.setBounds(94, 18, 231, 23);
        contentPane.add(profileNameField);

        JLabel gamePathLabel = new JLabel("Game Path:");
        gamePathLabel.setBounds(10, 56, 84, 31);
        contentPane.add(gamePathLabel);

        JButton gamePathButton = new JButton("...");
        gamePathButton.setBounds(302, 60, 23, 23);
        JTextField gamePathField = new JTextField(20);
        gamePathField.setBounds(94, 60, 209, 23);
        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);

        JLabel versionLabel = new JLabel("Version:");
        versionLabel.setBounds(10, 98, 84, 31);
        contentPane.add(versionLabel);
        JTextField versionField = new JTextField(20);
        versionField.setBounds(94, 102, 231, 23);
        contentPane.add(versionField);

        JLabel loaderLabel = new JLabel("Loader:");
        loaderLabel.setBounds(10, 140, 84, 31);
        contentPane.add(loaderLabel);

        JComboBox<String> loaderDropdown = new JComboBox<>(LoaderUtils.getLoaders());
        loaderDropdown.setBounds(94, 144, 231, 23);
        contentPane.add(loaderDropdown);

        JLabel offlineModeLabel = new JLabel("Offline Mode:");
        offlineModeLabel.setBounds(10, 182, 84, 31);
        contentPane.add(offlineModeLabel);
        JCheckBox offlineModeBox = new JCheckBox("");
        offlineModeBox.setBounds(94, 185, 21, 23);
        contentPane.add(offlineModeBox);

        JButton button = new JButton("Create Profile");
        button.setBounds(64, 234, 217, 37);
        button.addActionListener(al -> {
            Profile profile = new Profile();
            profile.setProfileName(profileNameField.getText());

            profile.setGamePath(new File(gamePathField.getText()));
            //profile.setVersion(versionField.getText());
            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);
    }

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