package com.ecep.contract.controller.customer;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.company.CompanyWindowController;
import com.ecep.contract.controller.tab.AbstEntityBasedTabSkin;
import com.ecep.contract.controller.tab.TabSkin;
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.CompanyCustomer;
import com.ecep.contract.service.CompanyContactService;
import com.ecep.contract.service.CompanyCustomerService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.CompanyCustomerViewModel;

import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.ActionEvent;
import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import javafx.util.converter.LocalDateStringConverter;

public class CompanyCustomerTabSkinBase
        extends AbstEntityBasedTabSkin<CompanyCustomerWindowController, CompanyCustomer, CompanyCustomerViewModel>
        implements TabSkin {

    public CompanyCustomerTabSkinBase(CompanyCustomerWindowController controller) {
        super(controller);
    }

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

    @Override
    public void initializeTab() {
        initializeCompanyFieldAutoCompletion(controller.companyField);
        initializeContactFieldAutoCompletion(controller.contactField);

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

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

        controller.pathField.textProperty().bind(viewModel.getPath());
        controller.descriptionField.textProperty().bindBidirectional(viewModel.getDescription());
        controller.createdField.textProperty().bind(
                Bindings.createStringBinding(
                        () -> localDateTimeFormatter(viewModel.getCreated()),
                        viewModel.getCreated()));
        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) {
                CompanyWindowController.show(company, controller.root.getScene().getWindow());
            }
        });

        controller.createPathBtn.setOnAction(this::onCompanyCustomerCreatePathAction);
        controller.changePathBtn.setOnAction(this::onCompanyCustomerChangePathAction);
        controller.pathAsNameBtn.setOnAction(this::onCompanyCustomerPathSameAsNameAction);
    }

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

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

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

    public void onCompanyCustomerCreatePathAction(ActionEvent event) {
        CompanyCustomer companyCustomer = getCompanyCustomerService().findById(viewModel.getId().get());
        if (getCompanyCustomerService().makePathAbsent(companyCustomer)) {
            companyCustomer = getCompanyCustomerService().save(companyCustomer);
            viewModel.update(companyCustomer);
        } else {
            setStatus("目录存在或创建失败");
        }
    }

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

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

    public CompanyCustomerService getCompanyCustomerService() {
        return controller.getCachedBean(CompanyCustomerService.class);
    }

    public CompanyContactService getCompanyContactService() {
        return controller.getCachedBean(CompanyContactService.class);
    }

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