package com.axis.innovators.box.gui;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 启动窗口的任务系统
 * @author tzdwindows 7
 */
public class ProgressBarManager {
    private JFrame loadingFrame;
    private JProgressBar mainProgressBar;
    private JProgressBar subProgressBar;
    private JLabel statusLabel;
    private JLabel timeLabel;
    private long startTime;

    private int totalTasks;
    private int completedTasks;
    private Map<String, Integer> subTasks = new HashMap<>();

    public ProgressBarManager(String title, int totalTasks) {
        this.totalTasks = totalTasks;
        this.completedTasks = 0;
        this.startTime = System.currentTimeMillis();

        loadingFrame = new JFrame(title);
        loadingFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        loadingFrame.setSize(400, 250);
        loadingFrame.setLocationRelativeTo(null);
        loadingFrame.setIconImage(LoadIcon.loadIcon("logo.png", 64).getImage());

        JPanel loadingPanel = new JPanel(new BorderLayout());

        mainProgressBar = new JProgressBar(0, 100);
        mainProgressBar.setStringPainted(true);

        subProgressBar = new JProgressBar(0, 100);
        subProgressBar.setStringPainted(true);
        subProgressBar.setString("Subtask Progress");

        statusLabel = new JLabel("Initializing...", SwingConstants.CENTER);
        timeLabel = new JLabel("Time elapsed: 0s", SwingConstants.CENTER);

        JLabel logoLabel = new JLabel(LoadIcon.loadIcon("logo.png", 64));

        JPanel progressPanel = new JPanel(new GridLayout(2, 1));
        progressPanel.add(mainProgressBar);
        progressPanel.add(subProgressBar);

        loadingPanel.add(logoLabel, BorderLayout.NORTH);
        loadingPanel.add(progressPanel, BorderLayout.CENTER);
        loadingPanel.add(statusLabel, BorderLayout.SOUTH);
        loadingPanel.add(timeLabel, BorderLayout.SOUTH);

        loadingFrame.add(loadingPanel);
        loadingFrame.setVisible(true);
    }

    /**
     * 更新主任务进度
     * @param completedTasks 已完成的主任务数量
     */
    public void updateMainProgress(int completedTasks) {
        this.completedTasks = completedTasks;
        int progress = (int) ((completedTasks / (double) totalTasks) * 100);
        mainProgressBar.setValue(progress);
        statusLabel.setText("Main Progress: " + progress + "%");
        updateTimeLabel();
    }

    /**
     * 更新子任务进度
     * @param subTaskName 子任务名称
     * @param subTaskCompleted 已完成的子任务数量
     * @param subTaskTotal 子任务总数
     */
    public void updateSubProgress(String subTaskName, int subTaskCompleted, int subTaskTotal) {
        subTasks.put(subTaskName, subTaskCompleted);
        int progress = (int) ((subTaskCompleted / (double) subTaskTotal) * 100);
        subProgressBar.setValue(progress);
        subProgressBar.setString(subTaskName + ": " + progress + "%");
        updateTimeLabel();
    }

    /**
     * 更新总任务数
     */
    public void setTotalTasks(int totalTasks) {
        this.totalTasks = totalTasks;
    }

    /**
     * 关闭加载窗口
     */
    public void close() {
        loadingFrame.dispose();
    }

    /**
     * 更新时间标签
     */
    private void updateTimeLabel() {
        long elapsedTime = (System.currentTimeMillis() - startTime) / 1000;
        timeLabel.setText("Time elapsed: " + elapsedTime + "s");
    }
}