package com.ecep.contract.controller.company.bank_account;

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.service.BankService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.CompanyBankAccountViewModel;
import com.ecep.contract.vo.CompanyBankAccountVo;
import com.ecep.contract.vo.CompanyVo;

import javafx.scene.control.Tab;

public class BankAccountBaseTabSkin
        extends AbstEntityBasedTabSkin<BankAccountWindowController, CompanyBankAccountVo, CompanyBankAccountViewModel>
        implements TabSkin {

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

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

    @Override
    public void initializeTab() {
        UITools.autoCompletion(controller.companyField, viewModel.getCompanyId(), getCompanyService());

        UITools.autoCompletion(controller.bankField, viewModel.getBankId(), getBankService());

        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.getCompanyId().isNull());
        controller.relativeCompanyBtn.setOnAction(event -> {
            Integer companyId = viewModel.getCompanyId().get();
            if (companyId != null) {
                CompanyVo company = getCompanyService().findById(companyId);
                CompanyWindowController.show(company, controller.root.getScene().getWindow());
            }
        });
    }

    public BankService getBankService() {
        return getCachedBean(BankService.class);
    }

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