package com.ecep.contract.controller;

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

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.CloudYu;
import com.ecep.contract.model.Company;
import com.ecep.contract.service.CompanyService;
import com.ecep.contract.service.YongYouU8Service;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.util.ProxyUtils;
import com.ecep.contract.vm.CloudYuInfoViewModel;

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

public class YongYouU8ManagerSkin
        extends
        AbstEntityManagerSkin<CloudYu, CloudYuInfoViewModel, YongYouU8ManagerSkin, YongYouU8ManagerWindowController>
        implements ManagerSkin {

    public YongYouU8ManagerSkin(YongYouU8ManagerWindowController controller) {
        super(controller);
    }

    YongYouU8Service getU8Service() {
        return   getBean(YongYouU8Service.class);
    }

    CompanyService getCompanyService() {
        return getBean(CompanyService.class);
    }

    @Override
    protected List<CloudYuInfoViewModel> loadTableData() {
        String searchText = controller.searchKeyField.getText();
        Map<String, Object> params = ParamUtils.builder().build();
        if (StringUtils.hasText(searchText)) {
            params.put("searchText", searchText);
        }
        Page<CloudYu> page = getU8Service().findAll(params, getPageable());
        updateFooter(page);
        return page.map(CloudYuInfoViewModel::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.descriptionColumn.setCellValueFactory(param -> param.getValue().getVendorCode());
    }

    @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<CloudYuInfoViewModel> selectedItems = getTableView().getSelectionModel().getSelectedItems();
        if (selectedItems.isEmpty()) {
            return;
        }
        for (CloudYuInfoViewModel selectedItem : selectedItems) {
            CloudYu cloudYu = getU8Service().findById(selectedItem.getId().get());
            selectedItem.getCustomerCode().set("");
            if (selectedItem.copyTo(cloudYu)) {
                CloudYu saved = getU8Service().save(cloudYu);
                selectedItem.update(saved);
            }
        }
    }

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