package com.ecep.contract.controller.tab;

import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.controller.contract.sale_order.SalesOrderWindowController;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.ContractService;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.SalesOrderViewModel;
import com.ecep.contract.vo.SalesOrderVo;

import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.util.converter.NumberStringConverter;

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

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

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

    @Override
    public void initializeTab() {
        controller.codeField.textProperty().bind(viewModel.getCode());
        controller.refIdField.textProperty().bindBidirectional(viewModel.getRefId(), new NumberStringConverter(getLocale()));

        controller.taxRateField.textProperty().bindBidirectional(viewModel.getTaxRate(), new NumberStringConverter(getLocale()));

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

        initializeCompanyField(controller.customerField, viewModel.getCustomer());

        controller.customerAddressField.textProperty().bindBidirectional(viewModel.getCustomerAddress());
        
        initializeContractField(controller.contractField, viewModel.getContract());

        controller.makeDateField.textProperty().bind(viewModel.getMakerDate().map(MyDateTimeUtils::format));
        controller.verifierDateField.textProperty().bind(viewModel.getVerifierDate().map(MyDateTimeUtils::format));
        controller.modifierDateField.textProperty().bind(viewModel.getModifierDate().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<Integer> property) {
        UITools.autoCompletion(textField, property, controller.getEmployeeService());
    }

    private void initializeCompanyField(TextField textField, SimpleObjectProperty<Integer> property) {
        UITools.autoCompletion(textField, property, getCompanyService());
    }
    
    private void initializeContractField(TextField textField, SimpleObjectProperty<Integer> property) {
        UITools.autoCompletion(textField, property, getContractService());
    }

    public CompanyService getCompanyService() {
        return getCachedBean(CompanyService.class);
    }
    
    public ContractService getContractService() {
        return getCachedBean(ContractService.class);
    }
}
