package com.ecep.contract.controller.bank.account;

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.BankStringConverter;
import com.ecep.contract.converter.CompanyStringConverter;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyBankAccount;
import com.ecep.contract.service.BankService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.CompanyBankAccountViewModel;

import javafx.scene.control.Tab;
import javafx.scene.control.TextField;
import lombok.Setter;

public class BankAccountBaseTabSkin
        extends AbstEntityBasedTabSkin<BankAccountWindowController, CompanyBankAccount, CompanyBankAccountViewModel>
        implements TabSkin {
    @Setter
    private CompanyService companyService;
    @Setter
    private BankService bankService;

    public BankAccountBaseTabSkin(BankAccountWindowController controller) {
        super(controller);
        viewModel = controller.getViewModel();
    }

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

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

        controller.openingBankField.textProperty().bindBidirectional(viewModel.getOpeningBank());
        controller.bankAccountField.textProperty().bindBidirectional(viewModel.getAccount());

        // controller.descriptionField.textProperty().bindBidirectional(viewModel.getDescription());
        controller.createdField.textProperty().bind(viewModel.getCreated().asString());
        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());
            }
        });
    }

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

    private void initializeBaseTabBankFieldAutoCompletion(TextField textField) {
        BankStringConverter converter = SpringApp.getBean(BankStringConverter.class);
        UITools.autoCompletion(textField, viewModel.getBank(), converter::suggest, converter);
    }

    public BankService getBankService() {
        if (bankService == null) {
            bankService = getBean(BankService.class);
        }
        return bankService;
    }

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