package com.ecep.contract.controller.company_old_name;

import java.time.format.DateTimeFormatter;

import com.ecep.contract.controller.tab.AbstEntityBasedTabSkin;
import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.model.CompanyOldName;
import com.ecep.contract.service.CompanyOldNameService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.vm.CompanyOldNameViewModel;

import javafx.scene.control.Tab;
import javafx.util.converter.LocalDateStringConverter;
import lombok.Setter;


public class CompanyOldNameTabSkinBase
        extends AbstEntityBasedTabSkin<CompanyOldNameWindowController, CompanyOldName, CompanyOldNameViewModel>
        implements TabSkin {
    @Setter
    private CompanyService companyService;
    @Setter
    private CompanyOldNameService companyOldNameService;

    public CompanyOldNameTabSkinBase(CompanyOldNameWindowController controller) {
        super(controller);
    }

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

    @Override
    public void initializeTab() {

        controller.nameField.textProperty().bind(viewModel.getName());
        controller.ambiguityField.selectedProperty().bindBidirectional(viewModel.getAmbiguity());
        LocalDateStringConverter converter = new LocalDateStringConverter(DateTimeFormatter.ISO_LOCAL_DATE, null);
        controller.startDateField.setConverter(converter);
        controller.startDateField.valueProperty().bindBidirectional(viewModel.getBeginDate());
        controller.endDateField.setConverter(converter);
        controller.endDateField.valueProperty().bindBidirectional(viewModel.getEndDate());

        controller.pathField.textProperty().bind(viewModel.getPath());
        controller.descriptionField.textProperty().bindBidirectional(viewModel.getMemo());
        controller.versionLabel.textProperty().bind(viewModel.getVersion().asString());

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

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

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