package com.ecep.contract.controller.permission;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ecep.contract.controller.AbstEntityController;
import com.ecep.contract.model.Function;
import com.ecep.contract.service.FunctionService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.FunctionViewModel;

import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
import lombok.Getter;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/employee/function.fxml")
public class FunctionWindowController extends AbstEntityController<Function, FunctionViewModel> {
    private static final Logger logger = LoggerFactory.getLogger(FunctionWindowController.class);

    /**
     * 显示界面
     */
    public static void show(FunctionViewModel viewModel, Window window) {
        show(FunctionWindowController.class, viewModel, window);
    }

    @FXML
    public BorderPane root;
    @FXML
    public TabPane tabPane;
    @FXML
    public Tab baseInfoTab;
    @FXML
    public Tab permissionTab;
    @FXML
    public TextField nameField;
    @FXML
    public TextField keyField;
    @FXML
    public TextField controllerField;
    @FXML
    public TextField iconField;
    @FXML
    public TextField descriptionField;
    @FXML
    public CheckBox activeField;
    @FXML
    public Label versionLabel;

    public static void show(Function function, Window owner) {
        FunctionViewModel model = FunctionViewModel.from(function);
        show(model, owner);
    }

    @Autowired
    @Getter
    FunctionService functionService;

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        getTitle().bind(viewModel.getName().map(name -> "[" + viewModel.getId().get() + "] " + name + " 功能详情"));
    }

    @Override
    protected void registerTabSkins() {
        registerTabSkin(baseInfoTab, tab -> new FunctionTabSkinBase(this));
        registerTabSkin(permissionTab, tab -> new FunctionTabSkinPermission(this));
    }

    @Override
    public FunctionService getViewModelService() {
        return functionService;
    }
}