package tech.nevets.jaml.gui;

import tech.nevets.jaml.AutoUpdater;
import tech.nevets.jaml.Config;
import tech.nevets.jaml.JAML;
import tech.nevets.jaml.gui.panels.CustomProgressBarPanel;
import tech.nevets.jaml.util.ImageUtils;
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.nio.file.Path;

public class StartupGui extends JFrame {
    JPanel contentPane;
    private static JLabel currentTaskLabel;
    private static CustomProgressBarPanel progressBar;
    private static final int TASKS = 8;
    private static int task = 0;

    public StartupGui() {
        setUndecorated(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setIconImage(new ImageIcon(getClass().getClassLoader().getResource("assets/icon.png")).getImage());
        setSize(GuiHandler.SCREEN_WIDTH / 3, GuiHandler.SCREEN_HEIGHT / 3);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        setLocationRelativeTo(null);

        currentTaskLabel = new JLabel("");
        currentTaskLabel.setBounds(30, 279, 176, 20);
        contentPane.add(currentTaskLabel);

        int pbWidth = (int) (getWidth() / 1.1);
        int pbHeight = getHeight() / 24;
        int pbX = (getWidth() - pbWidth) / 2;
        int pbY = getHeight() - (pbHeight * 2);

        JLabel progressBarOutline = new JLabel();
        progressBarOutline.setBounds(pbX, pbY - 10, pbWidth, pbHeight + 20);
        ImageIcon pgOutline = new ImageIcon(getClass().getClassLoader().getResource("assets/progressbaroutline.png"));
        pgOutline = ImageUtils.resizeIcon(pgOutline, pbWidth + 20, pbHeight);
        progressBarOutline.setIcon(pgOutline);
        contentPane.add(progressBarOutline);

        progressBar = new CustomProgressBarPanel(pbWidth, pbHeight);
        progressBar.setBounds(pbX, pbY, pbWidth, pbHeight);
        contentPane.add(progressBar);
        
        JLabel splashIcon = new JLabel("");
        splashIcon.setSize(getWidth(), getHeight());
        ImageIcon backgroundImg = new ImageIcon(getClass().getClassLoader().getResource("assets/background.png"));
        backgroundImg = ImageUtils.resizeIcon(backgroundImg, splashIcon.getWidth(), splashIcon.getHeight());
        splashIcon.setIcon(backgroundImg);
        contentPane.add(splashIcon);

        setVisible(true);

        progressBar.setProgress(0);

        currentTaskLabel.setText("Making Directories...");
        task++;
        makeDirs();
        smoothIncrease();

        currentTaskLabel.setText("Loading Config...");
        task++;
        Config.loadConfig();
        smoothIncrease();

        task++;
        if (Config.CONFIG.getBoolean("launcher.auto-update")) {
            currentTaskLabel.setText("Checking for updates...");
            AutoUpdater.checkForUpdate();
        }
        smoothIncrease();

        currentTaskLabel.setText("Downloading Version List...");
        task++;
        VersionUtils.downloadList();
        smoothIncrease();

        currentTaskLabel.setText("Loading Profiles...");
        task++;
        ProfileUtils.getProfileList();
        smoothIncrease();

        currentTaskLabel.setText("Loading Active Profile...");
        task++;
        ProfileUtils.getActiveProfile();
        smoothIncrease();

        currentTaskLabel.setText("Getting Game Loaders...");
        task++;
        LoaderUtils.getLoaders();
        smoothIncrease();

        currentTaskLabel.setText("Getting versions...");
        task++;
        VersionUtils.updateVersionsList();
        smoothIncrease();

        currentTaskLabel.setText("Done!");
        smoothIncrease();

    }

    private static void sleep() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException ignored) {

        }
    }

    public static void setCurrentTask(String currentTask) {
        currentTaskLabel.setText(currentTask);
    }

    public static void makeDirs() {
        if (System.getenv("JAML_HOME") == null) {
            JAML.path = Path.of(System.getenv("APPDATA") + "\\.jaml\\");
        } else {
            JAML.path = Path.of(System.getenv("JAML_HOME"));
        }

        if (!JAML.path.toFile().exists()) {
            JAML.path.toFile().mkdirs();
        }

        Path profilePath = Path.of(JAML.path + "\\profiles\\");
        if (!profilePath.toFile().exists()) {
            profilePath.toFile().mkdirs();
        }

        Path dataPath = Path.of(JAML.path + "\\data\\");
        if (!dataPath.toFile().exists()) {
            dataPath.toFile().mkdirs();
        }

        Path skinsPath = Path.of(JAML.path + "\\data\\skins\\");
        if (!skinsPath.toFile().exists()) {
            skinsPath.toFile().mkdirs();
        }

        Path versionManifestPath = Path.of(JAML.path + "\\data\\version-manifest\\");
        if (!versionManifestPath.toFile().exists()) {
            versionManifestPath.toFile().mkdirs();
        }

        Path cachePath = Path.of(JAML.path + "\\cache");
        if (!cachePath.toFile().exists()) {
            cachePath.toFile().mkdirs();
        }
    }

    private static void smoothIncrease() {
        int i = progressBar.getProgress();
        while (i <= (100 / TASKS) * task) {
            progressBar.setProgress(i);
            i += 1;
        }
    }
}
