package com.ecep.contract.service;

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

import org.controlsfx.control.TaskProgressView;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ecep.contract.Desktop;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.task.CloudRkSyncTask;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.CloudRkViewModel;
import com.ecep.contract.vo.CloudRkVo;
import com.ecep.contract.vo.CompanyVo;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javafx.concurrent.Task;
import lombok.Data;

@Service
public class CloudRkService extends QueryService<CloudRkVo, CloudRkViewModel> {
    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class EntInfo {
        @JsonAlias("entid")
        private String id;
        @JsonAlias("entname")
        private String name;
        private boolean nowName;
    }

    /**
     * 生成定时同步任务
     *
     * @param taskProgressView 任务视图
     */
    public void scheduledTasks(TaskProgressView<Task<?>> taskProgressView) {
        ScheduledExecutorService executorService = Desktop.instance.getExecutorService();
        // 第一次延时1分钟启动
        executorService.schedule(() -> {
            // 定时 30分钟 运行一次
            executorService.scheduleAtFixedRate(() -> {
                Desktop.instance.getTaskMonitorCenter().registerAndStartTask(new CloudRkSyncTask());
            }, 0, 30, TimeUnit.MINUTES);
        }, 1, TimeUnit.MINUTES);
    }

    public CloudRkVo updateCloudRk(CompanyVo company, MessageHolder holder) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'updateCloudRk'");
    }

    public CloudRkVo getOrCreateCloudRk(CompanyVo company) {
        CloudRkVo cloudRk = findByCompany(company);
        if (cloudRk == null) {
            cloudRk = new CloudRkVo();
            cloudRk.setCompanyId(company.getId());
            cloudRk.setCustomerGrade("");
            cloudRk.setCustomerScore(-1);
            cloudRk.setVendorGrade("");
            cloudRk.setVendorScore(-1);
            cloudRk.setRank("");
            cloudRk.setActive(false);
            cloudRk.setVersion(1);
            cloudRk = save(cloudRk);
        }
        return cloudRk;
    }

    public CloudRkVo findByCompany(CompanyVo company) {
        return findAll(ParamUtils.builder().equals("company", company.getId()).build(), Pageable.ofSize(1)).stream()
                .findFirst().orElse(null);
    }

    public boolean checkBlackListUpdateElapse(CompanyVo company, CloudRkVo cloudRk) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'checkBlackListUpdateElapse'");
    }

    public void updateBlackList(CompanyVo company, CloudRkVo cloudRk) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'updateBlackList'");
    }
}
