package com.ecep.contract.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ecep.contract.util.ProxyUtils;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;

import com.ecep.contract.SpringApp;
import com.ecep.contract.controller.company.CompanyWindowController;
import com.ecep.contract.controller.table.cell.CompanyTableCell;
import com.ecep.contract.controller.table.cell.LocalDateTimeTableCell;
import com.ecep.contract.model.CloudTyc;
import com.ecep.contract.model.Company;
import com.ecep.contract.service.CloudTycService;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.vm.CloudTycInfoViewModel;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import lombok.Setter;

public class CloudTycManagerSkin
        extends
        AbstEntityManagerSkin<CloudTyc, CloudTycInfoViewModel, CloudTycManagerSkin, CloudTycManagerWindowController> {
    @Setter
    private CloudTycService cloudTycService;

    @Setter
    private CompanyService companyService;

    public CloudTycManagerSkin(CloudTycManagerWindowController controller) {
        super(controller);
    }

    CloudTycService getCloudTycService() {
        if (cloudTycService == null) {
            cloudTycService = getBean(CloudTycService.class);
        }
        return cloudTycService;
    }

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

    @Override
    protected List<CloudTycInfoViewModel> loadTableData() {
        String searchText = controller.searchKeyField.getText();
        Map<String, Object> spec = new HashMap<>();
        if (StringUtils.hasText(searchText)) {
            spec.put("searchText", searchText);
        }
        Page<CloudTyc> page = getCloudTycService().findAll(spec, getPageable());
        updateFooter(page);
        return page.map(CloudTycInfoViewModel::from).toList();
    }

    @Override
    public void initializeTable() {
        controller.idColumn.setCellValueFactory(param -> param.getValue().getId());
        controller.companyColumn.setCellValueFactory(param -> param.getValue().getCompany());
        controller.companyColumn.setCellFactory(param -> new CompanyTableCell<>(getCompanyService()));

        controller.cloudIdColumn.setCellValueFactory(param -> param.getValue().getCloudId());

        controller.latestUpdateColumn.setCellValueFactory(param -> param.getValue().getLatest());
        controller.latestUpdateColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        controller.cloudLatestColumn.setCellValueFactory(param -> param.getValue().getCloudLatest());
        controller.cloudLatestColumn.setCellFactory(param -> new LocalDateTimeTableCell<>());

        controller.scoreColumn.setCellValueFactory(param -> param.getValue().getScore());

    }

    @Override
    protected void createContextMenu(ContextMenu contextMenu) {
        MenuItem item2 = new MenuItem("刷新");
        item2.setOnAction(this::onTableRefreshAction);

        MenuItem item3 = new MenuItem("清空备注");
        item3.setOnAction(this::onTableClearDescriptionAction);

        contextMenu.getItems().addAll(item2, item3);
    }

    /**
     * 请空选择行的注释
     *
     * @param event event
     */
    public void onTableClearDescriptionAction(ActionEvent event) {
        ObservableList<CloudTycInfoViewModel> selectedItems = getTableView().getSelectionModel().getSelectedItems();
        if (selectedItems.isEmpty()) {
            return;
        }
        for (CloudTycInfoViewModel selectedItem : selectedItems) {
            CloudTyc cloudTyc = getCloudTycService().findById(selectedItem.getId().get());
            // selectedItem.getDescription().set("");
            if (selectedItem.copyTo(cloudTyc)) {
                CloudTyc saved = getCloudTycService().save(cloudTyc);
                selectedItem.update(saved);
            }
        }
    }

    @Override
    protected void onTableRowDoubleClickedAction(CloudTycInfoViewModel item) {
        Company company = item.getCompany().get();
        if (!ProxyUtils.isInitialized(company)) {
            company = getCompanyService().findById(company.getId());
        }
        CompanyWindowController.show(company, getTableView().getScene().getWindow());
    }
}
