package com.zhangmeng.online.exam.ui.controller;

import com.zhangmeng.online.exam.ui.module.Permission;
import com.zhangmeng.online.exam.ui.module.SubjectType;
import com.zhangmeng.online.exam.ui.service.SubjectService;
import com.zhangmeng.online.exam.ui.service.UserService;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import com.zhangmeng.online.exam.ui.service.PermissionService;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zm
 * @date 2025/4/8 14:07
 * @version: 1.0
 */
public class PermissionEditController {

    @FXML
    public TextField rolenameField;

    @FXML
    public TextField descriptionField;

    @FXML
    public TextField urlField;

    @FXML
    public ComboBox<Permission> permissionComboBox;

    private Permission editPermission;

    @FXML
    public void initialize() {

        permissionComboBox.getItems().addAll(PermissionService.getALLPermissions());
        //permissionComboBox.getSelectionModel().selectFirst();
        permissionComboBox.setConverter(new StringConverter<Permission>() {
            @Override
            public String toString(Permission object) {
                if (object == null){
                    return null;
                }
                return object.getName();
            }

            @Override
            public Permission fromString(String string) {
                return null;
            }
        });
    }

    public void handleSave(ActionEvent actionEvent) {
        Permission permission = permissionComboBox.getSelectionModel().getSelectedItem();

        Map<String, Object> params = new HashMap<>();
        params.put("id", editPermission == null? "" : editPermission.getId());
        params.put("name", rolenameField.getText());
        params.put("description", descriptionField.getText());
        params.put("url", urlField.getText());
        params.put("parent_id",permission == null? "" : permission.getId());
        params.put("token", UserService.getCurrentUser().getToken());

        boolean flag = false;
        if (editPermission == null){
            flag = true;
        }

        PermissionService.savePermission(params,flag);
        closeDialog();
    }

    public void setEditPermission(Permission editPermission) {
        this.editPermission = editPermission;
        rolenameField.setText(editPermission.getName());
        descriptionField.setText(editPermission.getDescription());
        urlField.setText(editPermission.getUrl());
        ObservableList<Permission> items = permissionComboBox.getItems();
        for (Permission permission : items) {
            if (permission.getId() == editPermission.getParentId()){
                permissionComboBox.getSelectionModel().select(permission);
                break;
            }
        }
    }

    @FXML
    public void handleCancel() {
        closeDialog();
    }

    private void closeDialog() {
        Stage stage = (Stage) permissionComboBox.getScene().getWindow();
        stage.close();
    }
}
