package com.ecep.contract.controller.company;

import java.util.List;
import java.util.Optional;

import com.ecep.contract.controller.AbstEntityManagerSkin;
import com.ecep.contract.model.Company;
import com.ecep.contract.service.CompanyOldNameService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.vm.CompanyViewModel;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.scene.control.Alert;
import javafx.scene.control.TextInputDialog;
import lombok.Setter;

public class CompanyManagerSkin
        extends AbstEntityManagerSkin<Company, CompanyViewModel, CompanyManagerSkin, CompanyManagerWindowController> {

    @Setter
    private CompanyOldNameService companyOldNameService;

    public CompanyManagerSkin(CompanyManagerWindowController controller) {
        super(controller);
    }

    public CompanyService getCompanyService() {
        return controller.getViewModelService();
    }

    public CompanyOldNameService getCompanyOldNameService() {
        if (companyOldNameService == null) {
            companyOldNameService = getBean(CompanyOldNameService.class);
        }
        return companyOldNameService;
    }

    @Override
    public void initializeTable() {
        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());
        controller.nameColumn.setCellValueFactory(param -> param.getValue().getName());
        controller.uniscidColumn.setCellValueFactory(param -> param.getValue().getUid());
        controller.entStatusColumn.setCellValueFactory(param -> param.getValue().getEntStatus());
        controller.createdColumn.setCellValueFactory(param -> param.getValue().getCreated());
        controller.memoColumn.setCellValueFactory(param -> param.getValue().getMemo());

        Platform.runLater(() -> {
            getTableView().getSortOrder().add(controller.createdColumn);
        });
    }

    @Override
    protected void onTableRowDoubleClickedAction(CompanyViewModel item) {
        showInOwner(CompanyWindowController.class, item);
    }

    @Override
    protected void onTableCreateNewAction(ActionEvent event) {
        Platform.runLater(() -> {
            TextInputDialog dialog = new TextInputDialog();
            dialog.setTitle("新建公司");
            dialog.setHeaderText("请输入新建的公司名称，请输入公司名字全称");
            Optional<String> optional = dialog.showAndWait();
            if (optional.isPresent()) {
                CompanyService companyService = getCompanyService();
                String newCompanyName = optional.get();
                List<Company> list = companyService.findAllByName(newCompanyName);
                if (list == null || list.isEmpty()) {
                    // 未登记过
                    Company company = companyService.createNewCompany(newCompanyName);
                    Company saved = companyService.save(company);
                    CompanyWindowController.show(saved, getTableView().getScene().getWindow());
                } else {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("新建公司");
                    alert.setHeaderText("公司名称 " + newCompanyName + " 已经存在，是否打开公司页面");
                }
            }
        });
    }
}
