package com.ecep.contract.service;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.controlsfx.control.TaskProgressView;
import org.springframework.stereotype.Service;

import com.ecep.contract.Desktop;
import com.ecep.contract.model.CloudYu;
import com.ecep.contract.model.Company;
import com.ecep.contract.task.ContractSyncTask;
import com.ecep.contract.task.CustomerSyncTask;
import com.ecep.contract.task.MonitoredTask;
import com.ecep.contract.task.VendorSyncTask;
import com.ecep.contract.vm.CloudYuInfoViewModel;

import javafx.concurrent.Task;

@Service
public class YongYouU8Service extends QueryService<CloudYu, CloudYuInfoViewModel> {

    /**
     * 生成定时同步任务
     *
     * @param taskProgressView 任务视图
     */
    public void scheduledTasks(TaskProgressView<Task<?>> taskProgressView) {
        ScheduledExecutorService executorService = Desktop.instance.getExecutorService();
        executorService.scheduleAtFixedRate(() -> {
            ContractSyncTask task = new ContractSyncTask();
            MonitoredTask<Object> registerTask = Desktop.instance.getTaskMonitorCenter().registerTask(task);
            registerTask.schedule(5, TimeUnit.SECONDS);
        }, 3, TimeUnit.MINUTES.toSeconds(15), TimeUnit.SECONDS);

        executorService.scheduleAtFixedRate(() -> {
            // 1小时运行一次同步供应商任务
            VendorSyncTask vendorTask = new VendorSyncTask();
            MonitoredTask<Object> registerVendorTask = Desktop.instance.getTaskMonitorCenter().registerTask(vendorTask);
            registerVendorTask.schedule(60, TimeUnit.SECONDS);

            // 1小时运行一次同步客户任务
            CustomerSyncTask customerTask = new CustomerSyncTask();
            MonitoredTask<Object> registerCustomerTask = Desktop.instance.getTaskMonitorCenter()
                    .registerTask(customerTask);
            registerCustomerTask.schedule(60, TimeUnit.SECONDS);

        }, 3, TimeUnit.HOURS.toSeconds(1), TimeUnit.SECONDS);

    }

    public CloudYu getOrCreateCloudYu(Company company) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getOrCreateCloudYu'");
    }

}
