package com.ecep.contract.controller.tab;

import java.time.LocalDateTime;

import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.customer.SalesOrderWindowController;
import com.ecep.contract.converter.EmployeeStringConverter;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.SalesOrder;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.ContractFileService;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.SalesOrderViewModel;

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

public class SalesOrderTabSkinBase
        extends AbstEntityBasedTabSkin<SalesOrderWindowController, SalesOrder, SalesOrderViewModel>
        implements TabSkin {

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

    public SalesOrderTabSkinBase(SalesOrderWindowController controller) {
        super(controller);
    }

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

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

        initializeEmployeeField(controller.employeeField, viewModel.getEmployee());
        initializeEmployeeField(controller.makerField, viewModel.getMaker());
        initializeEmployeeField(controller.verifierField, viewModel.getVerifier());


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

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

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

    protected String localDateTimeFormatter(SimpleObjectProperty<LocalDateTime> property) {
        LocalDateTime dateTime = property.get();
        if (dateTime == null) {
            return "";
        }
        return MyDateTimeUtils.format(dateTime);
    }

    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;
    }
}
