package com.ecep.contract.controller.vendor.approved_list;

import java.io.File;
import java.time.LocalDate;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.springframework.util.StringUtils;

import com.ecep.contract.DesktopUtils;
import com.ecep.contract.controller.tab.TabSkin;
import com.ecep.contract.controller.table.AbstEntityTableTabSkin;
import com.ecep.contract.model.CompanyVendorApprovedFile;
import com.ecep.contract.model.CompanyVendorApprovedList;
import com.ecep.contract.service.CompanyVendorApprovedFileService;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.vm.CompanyVendorApprovedFileViewModel;
import com.ecep.contract.vm.CompanyVendorApprovedListViewModel;

import javafx.event.ActionEvent;
import javafx.scene.control.Tab;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;

@FxmlPath("/ui/company/vendor/vendor_approved_list-tab-file.fxml")
public class CompanyVendorApprovedListTabSkinFiles
        extends
        AbstEntityTableTabSkin<CompanyVendorApprovedListWindowController, CompanyVendorApprovedList, CompanyVendorApprovedListViewModel, CompanyVendorApprovedFile, CompanyVendorApprovedFileViewModel>
        implements TabSkin {

    private CompanyVendorApprovedFileService fileService;

    public TableColumn<CompanyVendorApprovedFileViewModel, Number> fileTable_idColumn;
    public TableColumn<CompanyVendorApprovedFileViewModel, String> fileTable_filePathColumn;
    public TableColumn<CompanyVendorApprovedFileViewModel, LocalDate> fileTable_signDateColumn;
    public TableColumn<CompanyVendorApprovedFileViewModel, String> fileTable_descriptionColumn;

    public CompanyVendorApprovedListTabSkinFiles(CompanyVendorApprovedListWindowController controller) {
        super(controller);
    }

    CompanyVendorApprovedFileService getFileService() {
        if (fileService == null) {
            fileService = getBean(CompanyVendorApprovedFileService.class);
        }
        return fileService;
    }

    @Override
    protected CompanyVendorApprovedFileService getViewModelService() {
        return getFileService();
    }

    @Override
    public Map<String, Object> getSpecification(CompanyVendorApprovedList parent) {
        Map<String, Object> params = getSpecification();
        params.put("list", parent);
        return params;
    }

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

    @Override
    public void initializeTable() {
        super.initializeTable();
        fileTable_idColumn.setCellValueFactory(param -> param.getValue().getId());
        fileTable_filePathColumn.setCellValueFactory(param -> param.getValue().getFileName());
        fileTable_filePathColumn.setCellFactory(param -> new FileTableFilePathTableCell());
        fileTable_signDateColumn.setCellValueFactory(param -> param.getValue().getSignDate());
        fileTable_descriptionColumn.setCellValueFactory(param -> param.getValue().getDescription());
    }

    public void onFileReBuildingAction(ActionEvent event) {
        CompletableFuture.runAsync(() -> {
            int id = viewModel.getId().get();
            CompanyVendorApprovedList list = controller.getViewModelService().findById(id);
            try {
                if (getViewModelService().reBuildingFiles(list, (level, msg) -> setStatus(msg))) {
                    loadTableDataSet();
                }
            } catch (Exception e) {
                setStatus("重置 " + list.getTitle() + " 文件时发生错误 " + e.getMessage());
            }
        });
    }

    @Override
    protected void onTableRowDoubleClickedAction(CompanyVendorApprovedFileViewModel item) {
        String parent = viewModel.getPath().get();
        if (!controller.getViewModelService().existPath(getEntity())) {
            setStatus("合同的目录未设置或者无效，请检查!");
            return;
        }

        String fileName = item.getFileName().get();
        if (StringUtils.hasText(fileName)) {
            File file = new File(parent, fileName);
            if (!file.exists()) {
                setStatus("文件不存在 " + file.getName());
                return;
            }
            DesktopUtils.showInExplorer(file);
        }
    }

    class FileTableFilePathTableCell extends TableCell<CompanyVendorApprovedFileViewModel, String> {
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText("");
                return;
            }
            String path = viewModel.getPath().get();
            if (StringUtils.hasText(path)) {
                if (item.startsWith(path)) {
                    item = "~" + item.substring(path.length());
                }
            }
            setText(item);
        }
    }
}
