package com.ecep.contract.controller.vendor.purchase.order;

import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.tab.AbstEntityBasedTabSkin;
import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.controller.vendor.PurchaseOrderWindowController;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.PurchaseOrder;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.ContractFileService;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.PurchaseOrderViewModel;

import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import lombok.Setter;

public class PurchaseOrderTabSkinBase
        extends AbstEntityBasedTabSkin<PurchaseOrderWindowController, PurchaseOrder, PurchaseOrderViewModel>
        implements TabSkin {

    @Setter
    private ContractFileService companyContactService;
    @Setter
    private CompanyService companyService;
    private EmployeeStringConverter employeeStringConverter;

    public PurchaseOrderTabSkinBase(PurchaseOrderWindowController controller) {
        super(controller);
    }

    @Override
    public Tab getTab() {
        return controller.baseInfoTab;
    }

    @Override
    public void initializeTab() {
        controller.codeField.textProperty().bind(viewModel.getCode());
        controller.refIdField.textProperty().bind(viewModel.getRefId().asString());

        initializeEmployeeField(controller.employeeField, viewModel.getEmployee());
        initializeEmployeeField(controller.makerField, viewModel.getMaker());
        // initializeEmployeeField(controller.modifierField, viewModel.getMaker());
        initializeEmployeeField(controller.verifierField, viewModel.getVerifier());
        initializeEmployeeField(controller.closerField, viewModel.getCloser());

        controller.makeDateField.textProperty().bind(viewModel.getMakerDate().map(MyDateTimeUtils::format));
        controller.verifierDateField.textProperty().bind(viewModel.getVerifierDate().map(MyDateTimeUtils::format));
        controller.modifierDateField.textProperty().bind(viewModel.getModifyDate().map(MyDateTimeUtils::format));
        controller.closerDateField.textProperty().bind(viewModel.getCloserDate().map(MyDateTimeUtils::format));

        controller.descriptionField.textProperty().bindBidirectional(viewModel.getDescription());

        controller.saveBtn.disableProperty().bind(viewModel.getChanged().not());
        controller.saveBtn.setOnAction(this::onSaveAction);
    }

    private void initializeEmployeeField(TextField textField, SimpleObjectProperty<Employee> property) {
        EmployeeStringConverter converter = getEmployeeStringConverter();
        UITools.autoCompletion(textField, property, converter::suggest, converter);
    }

    public CompanyService getCompanyService() {
        if (companyService == null) {
            companyService = SpringApp.getBean(CompanyService.class);
        }
        return companyService;
    }


    public EmployeeStringConverter getEmployeeStringConverter() {
        if (employeeStringConverter == null) {
            employeeStringConverter = SpringApp.getBean(EmployeeStringConverter.class);
        }
        return employeeStringConverter;
    }
}
