package top.r3944realms.docchecktoolrefactored.ui.module;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import lombok.Getter;
import lombok.Setter;
import top.r3944realms.docchecktoolrefactored.System;
import top.r3944realms.docchecktoolrefactored.ui.utils.DialogUtil;
import top.r3944realms.docchecktoolrefactored.util.FileUtil;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * The type Project info pane controller.
 */
public class ProjectInfoPaneController implements Initializable {
    @FXML private TextField projectNameTF;
    @FXML private TextField AcceptanceTimeTF;
    @FXML private TextField totalCatalogNumberTF;
    @FXML private TextField fileCategoriesTF;
    @FXML private TextField fileYearTF;
    @FXML
    void onReset(ActionEvent event) {
        if (DialogUtil.showConfirmationDialog("确认","是否清除（该操作不可逆）", "")){// 清空所有文本字段
            projectNameTF.clear();
            fileYearTF.clear();
            fileCategoriesTF.clear();
            totalCatalogNumberTF.clear();
            AcceptanceTimeTF.clear();
        }
    }
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        addAutoSaveListener(projectNameTF, "projectName");
        addAutoSaveListener(AcceptanceTimeTF, "acceptanceTime");
        addAutoSaveListener(totalCatalogNumberTF, "totalCatalogNumber");
        addAutoSaveListener(fileCategoriesTF, "fileCategory");
        addAutoSaveListener(fileYearTF, "fileYear");
    }
    private void addAutoSaveListener(TextField field, String fieldName) {
        field.textProperty().addListener((observable, oldValue, newValue) -> {
            switch (fieldName) {
                case "projectName" -> System.getProjectInfo().setProjectName(newValue);
                case "acceptanceTime" -> System.getProjectInfo().setAcceptanceTime(newValue);
                case "totalCatalogNumber" -> System.getProjectInfo().setTotalCatalogNumber(newValue);
                case "fileCategory" -> System.getProjectInfo().setFileCategory(newValue);
                case "fileYear" -> System.getProjectInfo().setFileYear(newValue);
            }
        });
    }
    @Setter @Getter
    public static class ProjectInfo {
        private String projectName;
        private String acceptanceTime;
        private String totalCatalogNumber;
        private String fileCategory;
        private String fileYear;

        @Override
        public String toString() {
            return "ProjectInfo{" +
                    "projectName='" + projectName + '\'' +
                    ", acceptanceTime='" + acceptanceTime + '\'' +
                    ", totalCatalogNumber='" + totalCatalogNumber + '\'' +
                    ", fileCategory='" + fileCategory + '\'' +
                    ", fileYear='" + fileYear + '\'' +
                    '}';
        }
        public String makeProjectInfoIntoFileNamePrefix() {
            return FileUtil.ensureValidFileName(projectName);
        }
    }
}
