package com.yihu.hos.remotemanage.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.interceptor.LocalContext;
import com.yihu.hos.services.ServiceShellEventService;
import com.yihu.hos.web.framework.constant.SSHConstant;
import com.yihu.hos.web.framework.model.bo.ServiceShell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zbus.broker.ZbusBroker;
import org.zbus.mq.Consumer;
import org.zbus.net.http.Message;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2017/1/5.
 */
@Service("RemoteShellService")
public class RemoteShellService {
    public static final String BEAN_ID = "RemoteShellService";

    @Autowired
    private ServiceShellEventService serviceShellEventService;
    private ZbusBroker zbusBroker;
    private Consumer consumer;
    private static Map<String,String> cunsumerNap = new HashMap<>();

    @Autowired
    private ObjectMapper objectMapper;
    /**
     * 发送shell命令消息
     *
     * @param command
     * @param disConnection
     * @return
     */
    public boolean sendShell(String command, boolean disConnection) {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
            String ip = addr.getHostAddress();
            ServiceShell serviceShell = new ServiceShell();
            serviceShell.setCommand(command);
            serviceShell.setFromHost(ip);
            serviceShell.setUpdated(new Date());
            serviceShell.setDisconnect(disConnection);
            serviceShellEventService.serviceShellSend(serviceShell);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public void handle(Message message, Consumer consumer) throws IOException {
        String bodyString = message.getBodyString();
        String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
        LocalContext.getContext().setAttachment(ContextAttributes.SHELL_RESPONSE + attachment, bodyString);
        System.out.println("回调返回：" + bodyString);
    }

    public void start() {
        if (zbusBroker == null) {
            System.out.println("zbusBroker is null");
            return;
        }

        String attachment = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
        if (attachment == null) {
            return;
        }

        try {
            if (!cunsumerNap.containsKey(SSHConstant.SHELL_RESPONSE + "@" + attachment)){
                consumer = new Consumer(zbusBroker, SSHConstant.SHELL_RESPONSE + "@" + attachment);
                consumer.start(this::handle);
                cunsumerNap.put(SSHConstant.SHELL_RESPONSE + "@" + attachment,attachment);
            }
            System.out.println("start success~");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Autowired
    public void setZbusBroker(ZbusBroker zbusBroker) {
        this.zbusBroker = zbusBroker;
    }

    @Override
    protected void finalize() throws Throwable {
        consumer.close();
        super.finalize();
    }

}
