package com.bocloud.ims.service.task;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.ims.entity.resource.CloudServer;
import com.bocloud.ims.entity.resource.Server;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 资源创建 删除通知cop
 *
 * @author ymh
 * @date 2022/02/28
 */
@Component
public class ResourceTaskNotice {

    private static Logger logger = LoggerFactory.getLogger(ResourceTaskNotice.class);

    @Autowired
    private AmqpTemplate amqpTemplate;

    private static final String TASK_QUEUE = "bocloud.taskhost.operate";

    public void sendLogicalMessage(CloudServer cloudServer, ResourceTaskNotice.Action action) {
        try {
            if(null == cloudServer){
                return;
            }

            JSONObject params = buildParams(cloudServer, action, ResourceTaskNotice.ResourceType.LOGICAL);
            logger.info("发送消息通知cop:" + params);
            amqpTemplate.convertAndSend(TASK_QUEUE,params);
        } catch (AmqpException e) {
            logger.error("发送消息通知cop异常：" + e.getMessage());
        }
    }

    /**
     * 通过平台id通知主机资源删除主机
     * @param vendorId
     */
    public void sendLogicalDeleteMessage(Long vendorId) {
        try {
            if (null == vendorId) {
                return;
            }
            CloudServer cloudServer = new CloudServer();
            cloudServer.setVendorId(vendorId);
            sendLogicalMessage(cloudServer, Action.DELYVID);
        } catch (AmqpException e) {
            logger.error("发送消息通知cop异常：" + e.getMessage());
        }
    }

    public void sendLogicalMessage(List<CloudServer> cloudServers, ResourceTaskNotice.Action action) {
        try {
            if (null == cloudServers) {
                return;
            }
            for (CloudServer cloudServer : cloudServers) {
                sendLogicalMessage(cloudServer, action);
            }
        } catch (AmqpException e) {
            logger.error("发送消息通知cop异常：" + e.getMessage());
        }
    }

    public void sendPhysicalMessage(Server server, ResourceTaskNotice.Action action) {
        try {
            if(null == server){
                return;
            }

            JSONObject params = buildParams(server, action, ResourceTaskNotice.ResourceType.PHYSICAL);
            logger.info("发送消息通知cop:" + params);
            amqpTemplate.convertAndSend(TASK_QUEUE,params);
        } catch (AmqpException e) {
            logger.error("发送消息通知cop异常：" + e.getMessage());
        }
    }

    public void sendPhysicalMessage(List<Server> servers, ResourceTaskNotice.Action action) {
        try {
            if (null == servers) {
                return;
            }
            for (Server server : servers) {
                sendPhysicalMessage(server, action);
            }
        } catch (AmqpException e) {
            logger.error("发送消息通知cop异常：" + e.getMessage());
        }
    }

    /**
     * 通过平台id通知主机资源删除主机
     * @param vendorId
     */
    public void sendPhysicalDeleteMessage(Long vendorId) {
        try {
            if (null == vendorId) {
                return;
            }
            Server server = new Server();
            server.setVendorId(vendorId);
            sendPhysicalMessage(server, Action.DELYVID);
        } catch (AmqpException e) {
            logger.error("发送消息通知cop异常：" + e.getMessage());
        }
    }

    public JSONObject buildParams(Object obj, ResourceTaskNotice.Action action,
                                  ResourceTaskNotice.ResourceType resourceType) {
        JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put("action", action.name());
        jsonObject.put("category", resourceType.name());
        jsonObject.put("body", obj);
        return jsonObject;
    }

    public enum Action {
        ADD, // 添加
        DEL, // 删除
        DELYVID, // 根据平台删除
        UPDATE // 编辑
    }

    public enum ResourceType {
        PHYSICAL, // 宿主机
        LOGICAL // 云主机
    }

}
