package com.ecep.contract.controller.vendor;

import java.time.format.DateTimeFormatter;
import java.util.List;

import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.ComboBoxUtils;
import com.ecep.contract.controller.company.CompanyWindowController;
import com.ecep.contract.controller.tab.AbstEntityBasedTabSkin;
import com.ecep.contract.converter.CompanyStringConverter;
import com.ecep.contract.converter.EntityStringConverter;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyContact;
import com.ecep.contract.model.CompanyVendor;
import com.ecep.contract.model.VendorTypeLocal;
import com.ecep.contract.service.CompanyContactService;
import com.ecep.contract.service.CompanyVendorService;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.CompanyVendorViewModel;
import com.ecep.contract.vm.CompanyViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.util.converter.LocalDateStringConverter;
import javafx.util.converter.LocalDateTimeStringConverter;

public class CompanyVendorTabSkinBase
        extends AbstEntityBasedTabSkin<CompanyVendorWindowController, CompanyVendor, CompanyVendorViewModel> {
    public CompanyVendorTabSkinBase(CompanyVendorWindowController controller) {
        super(controller);
    }

    CompanyContactService companyContactService;

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

    CompanyContactService getCompanyContactService() {
        if (companyContactService == null) {
            companyContactService = SpringApp.getBean(CompanyContactService.class);
        }
        return companyContactService;
    }

    @Override
    public void initializeTab() {
        // initializeBaseTabCompanyFieldAutoCompletion(controller.companyField);

        LocalDateStringConverter converter = new LocalDateStringConverter(DateTimeFormatter.ISO_LOCAL_DATE, null);

        controller.developDateField.setConverter(converter);
        controller.developDateField.valueProperty().bindBidirectional(viewModel.getDevelopDate());

        List<VendorTypeLocal> vendorTypeLocals = getCompanyVendorService()
                .findAllTypes(controller.getLocale().toLanguageTag());
        ComboBoxUtils.initialComboBox(controller.catalogField, vendorTypeLocals, true);
        ComboBoxUtils.bindComboBox(controller.catalogField, viewModel.getType(), vendorTypeLocals);

        controller.protocolProviderField.selectedProperty().bindBidirectional(viewModel.getProtocolProvider());
        initializeContactField(controller.contactField);
        controller.purchaseField.textProperty().bindBidirectional(viewModel.getPurchase());

        controller.pathField.textProperty().bind(viewModel.getPath());
        controller.descriptionField.textProperty().bindBidirectional(viewModel.getDescription());
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter
                .ofPattern(MyDateTimeUtils.DEFAULT_DATETIME_FORMAT_PATTERN);
        controller.createdField.textProperty().bindBidirectional(viewModel.getCreated(),
                new LocalDateTimeStringConverter(dateTimeFormatter, null));
        controller.versionLabel.textProperty().bind(viewModel.getVersion().asString());

        controller.relativeCompanyBtn.disableProperty().bind(viewModel.getCompany().isNull());
        controller.relativeCompanyBtn.setOnAction(event -> {
            Company company = viewModel.getCompany().get();
            if (company != null) {
                showInOwner(CompanyWindowController.class, CompanyViewModel.from(company));
            }
        });

        controller.createPathBtn.setOnAction(this::onCreatePathAction);
        controller.changePathBtn.setOnAction(this::onChangePathAction);
        controller.pathAsNameBtn.setOnAction(this::onPathSameAsNameAction);
    }

    private void initializeContactField(TextField textField) {
        EntityStringConverter<CompanyContact> stringConverter = new EntityStringConverter<>();
        stringConverter.setInitialized(cc -> getCompanyContactService().findById(cc.getId()));
        stringConverter.setSuggestion(p -> getCompanyContactService().searchByCompany(viewModel.getCompany().get(), p));
        UITools.autoCompletion(textField, viewModel.getContact(), stringConverter);
    }

    private void initializeCompanyField(TextField textField) {
        CompanyStringConverter converter = SpringApp.getBean(CompanyStringConverter.class);
        UITools.autoCompletion(textField, viewModel.getCompany(), converter);
    }

    public void onCreatePathAction(ActionEvent event) {
        CompanyVendor companyVendor = getEntity();
        if (getCompanyVendorService().makePathAbsent(companyVendor)) {
            save(companyVendor);
        } else {
            setStatus("目录存在或创建失败");
        }
    }

    public void onChangePathAction(ActionEvent event) {
        setStatus("未实现");
    }

    public void onPathSameAsNameAction(ActionEvent event) {
        setStatus("未实现");
    }

    public CompanyVendorService getCompanyVendorService() {
        return controller.getViewModelService();
    }
}
