package tech.nevets.jaml.gui;

import tech.nevets.jaml.init.FirstLaunch;
import tech.nevets.jaml.JAML;
import tech.nevets.jaml.objects.Profile;
import tech.nevets.jaml.util.ProfileUtils;

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

public class FirstLaunchGui implements Runnable, ActionListener {
    //TODO Rework first launch logic
    private JFrame frame;
    private JPanel panel;

    private JTextField gamePathField;
    private JTextField versionField;
    private JTextField loaderField;
    private JTextField offlineModeField;

    public void startGui() {
        frame = new JFrame("Create First Profile");

        JLabel gamePathLabel = new JLabel("Game Path:");
        gamePathLabel.setBounds(10, 80, 80, 25);
        gamePathField = new JTextField(20);

        JButton gamePathButton = new JButton("Choose Path");
        gamePathButton.setBounds(200, 80, 25, 25);
        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);
        });

        JLabel versionLabel = new JLabel("Version:");
        versionLabel.setBounds(100, 80, 165, 25);
        versionField = new JTextField(20);

        JLabel loaderLabel = new JLabel("Loader:");
        loaderLabel.setBounds(10, 110, 80, 25);
        loaderField = new JTextField(20);

        JLabel offlineModeLabel = new JLabel("Offline Mode:");
        offlineModeLabel.setBounds(100, 110, 165, 25);
        offlineModeField = new JTextField(20);

        JButton button = new JButton("Create Profile");
        button.addActionListener(this);

        panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(300, 300, 100, 300));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(gamePathLabel);
        panel.add(gamePathField);
        panel.add(gamePathButton);
        panel.add(versionLabel);
        panel.add(versionField);
        panel.add(loaderLabel);
        panel.add(loaderField);
        panel.add(offlineModeLabel);
        panel.add(offlineModeField);
        panel.add(button);

        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setTitle("JAML");
        frame.setIconImage(new ImageIcon(JAML.path + "\\assets\\icon.png").getImage());
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        Profile profile = new Profile();
        profile.setProfileName("default");

        profile.setGamePath(new File(gamePathField.getText()));
        //profile.setVersion(versionField.getText());
        profile.setLoader(loaderField.getText());
        profile.setOfflineMode(Boolean.parseBoolean(offlineModeField.getText()));
        try {
            ProfileUtils.createProfile(profile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        frame.dispose();
    }

    @Override
    public void run() {
        new FirstLaunch();
        startGui();
    }
}