package com.ecep.contract.controller.tab;

import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;

import com.ecep.contract.controller.contract.AbstContractTableTabSkin;
import com.ecep.contract.controller.contract.ContractWindowController;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;
import com.ecep.contract.model.ContractPayPlan;
import com.ecep.contract.service.ContractPayPlanService;
import com.ecep.contract.service.ViewModelService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.ContractPayPlanViewModel;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.converter.CurrencyStringConverter;
import javafx.util.converter.NumberStringConverter;

@FxmlPath("/ui/contract/contract-tab-pay-plan.fxml")
public class ContractTabSkinPayPlan extends AbstContractTableTabSkin<ContractPayPlan, ContractPayPlanViewModel> {

    public TableColumn<ContractPayPlanViewModel, Number> idColumn;
    public TableColumn<ContractPayPlanViewModel, LocalDate> payDateColumn;
    public TableColumn<ContractPayPlanViewModel, Number> payRatioColumn;
    public TableColumn<ContractPayPlanViewModel, Number> payCurrencyColumn;
    public TableColumn<ContractPayPlanViewModel, String> payTermColumn;
    public TableColumn<ContractPayPlanViewModel, Number> refIdColumn;
    public TableColumn<ContractPayPlanViewModel, LocalDateTime> updateDateColumn;

    ContractPayPlanService payPlanService;

    public ContractTabSkinPayPlan(ContractWindowController controller) {
        super(controller);
    }

    ContractPayPlanService getPayPlanService() {
        if (payPlanService == null) {
            payPlanService = getBean(ContractPayPlanService.class);
        }
        return payPlanService;
    }

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

    @Override
    protected ViewModelService<ContractPayPlan, ContractPayPlanViewModel> getViewModelService() {
        return getPayPlanService();
    }

    @Override
    public void initializeTable() {
        super.initializeTable();

        NumberFormat numberInstance = NumberFormat.getNumberInstance(getLocale());
        numberInstance.setMaximumFractionDigits(2);
        numberInstance.setMinimumFractionDigits(2);
        CurrencyStringConverter currencyStringConverter = new CurrencyStringConverter(numberInstance);

        idColumn.setCellValueFactory(param -> param.getValue().getId());
        payDateColumn.setCellValueFactory(param -> param.getValue().getPayDate());

        payRatioColumn.setCellValueFactory(param -> param.getValue().getPayRatio());
        payRatioColumn.setCellFactory(TextFieldTableCell.forTableColumn(new NumberStringConverter()));

        payCurrencyColumn.setCellValueFactory(param -> param.getValue().getPayCurrency());
        payCurrencyColumn.setCellFactory(TextFieldTableCell.forTableColumn(currencyStringConverter));

        payTermColumn.setCellValueFactory(param -> param.getValue().getPayTerm());

        refIdColumn.setCellValueFactory(param -> param.getValue().getRefId());

        updateDateColumn.setCellValueFactory(param -> param.getValue().getUpdateDate());
        updateDateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());
    }

    @Override
    protected void createContextMenu(ContextMenu contextMenu) {
        super.createContextMenu(contextMenu);
    }
}
