package com.ecep.contract.controller;

import java.time.LocalDateTime;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ecep.contract.Message;
import com.ecep.contract.SpringApp;
import com.ecep.contract.model.CloudRk;
import com.ecep.contract.model.Company;
import com.ecep.contract.service.CloudRkService;
import com.ecep.contract.task.CloudRkSyncTask;
import com.ecep.contract.util.FxmlPath;
import com.ecep.contract.util.UITools;
import com.ecep.contract.vm.CloudRkViewModel;

import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.scene.control.TableColumn;
import javafx.stage.WindowEvent;

@Lazy
@Scope("prototype")
@Component
@FxmlPath("/ui/cloud/rk_manager.fxml")
public class CloudRkManagerWindowController
        extends AbstManagerWindowController<CloudRk, CloudRkViewModel, CloudRkManagerSkin> {
    private static final Logger logger = LoggerFactory.getLogger(CloudRkManagerWindowController.class);

    public static void show() {
        show(CloudRkManagerWindowController.class, null);
    }

    @Autowired
    private CloudRkService cloudRkService;

    public TableColumn<CloudRkViewModel, Number> idColumn;
    public TableColumn<CloudRkViewModel, LocalDateTime> latestUpdateColumn;

    public TableColumn<CloudRkViewModel, Company> companyColumn;
    public TableColumn<CloudRkViewModel, String> cloudIdColumn;
    public TableColumn<CloudRkViewModel, LocalDateTime> cloudLatestColumn;
    public TableColumn<CloudRkViewModel, LocalDateTime> cloudBlackListUpdatedColumn;
    public TableColumn<CloudRkViewModel, LocalDateTime> cloudEntUpdateColumn;
    public TableColumn<CloudRkViewModel, Boolean> autoUpdateColumn;
    public TableColumn<CloudRkViewModel, String> descriptionColumn;

    @Override
    protected CloudRkManagerSkin createDefaultSkin() {
        return new CloudRkManagerSkin(this);
    }

    @Override
    public void onShown(WindowEvent windowEvent) {
        super.onShown(windowEvent);
        getTitle().set("数据源：集团相关方");
    }

    private void initializeTask(Task<Object> task, String prefix, Consumer<String> consumer) {
        task.setOnScheduled(e -> {
            consumer.accept("正在从相关方平台同步" + prefix + "，请稍后...");
        });
        task.setOnRunning(e -> {
            consumer.accept("开始" + prefix + "...");
        });
        task.setOnSucceeded(e -> {
            consumer.accept(prefix + "完成...");
        });
        task.exceptionProperty().addListener((observable, oldValue, newValue) -> {
            consumer.accept(newValue.getMessage());
            logger.error("{} 发生异常", prefix, newValue);
        });
        SpringApp.getBean(ScheduledExecutorService.class).submit(task);
        consumer.accept("任务已创建...");
    }

    public void onDataRepairAction(ActionEvent event) {
    }

    public void onSyncAction(ActionEvent event) {
        CloudRkSyncTask task = new CloudRkSyncTask();
        UITools.showTaskDialogAndWait("同步数据", task, consumer -> {
            initializeTask(task, "同步数据", msg -> consumer.accept(Message.info(msg)));
        });
    }

    @Override
    public CloudRkService getViewModelService() {
        return cloudRkService;
    }
}
