package com.cm.central.control.service.socket.clientinfo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cm.central.control.manager.SocketServerManager;
import com.cm.central.control.pojo.bos.SocketClientBO;
import com.cm.central.control.service.socket.BaseSocketService;
import com.cm.common.exception.SearchException;
import com.cm.socket.pojo.Message;
import com.google.common.collect.EvictingQueue;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: ClientInfoService
 * @Description: 客户端信息
 * @Author: WangGeng
 * @Date: 2020/7/28 3:48 下午
 * @Version: 1.0
 **/
@Service("socketClientInfoServiceImpl")
public class SocketClientInfoServiceImpl extends BaseSocketService {

    @Override
    protected void readMessageService(ChannelHandlerContext channelHandlerContext, Message readMessage) throws Exception {
        SocketClientBO socketClientBO = SocketServerManager.getInstance().getOnlineClientByToken(readMessage.getToken());
        if (socketClientBO == null) {
            writeError(channelHandlerContext, "客户端不在线，请重新登录");
            return;
        }
        JSONObject clientInfo = JSONObject.parseObject(getDecodeContent(readMessage.getContent()));
        // 系统信息
        socketClientBO.setSystemInfo(getSystem(clientInfo));
        socketClientBO.setDiskList(listDisk(clientInfo));
        socketClientBO.addCpuQueue(getCPU(clientInfo));
        socketClientBO.addJvmQueue(getJvm(clientInfo));
        socketClientBO.addMemoryQueue(getMemory(clientInfo));
    }


    @Override
    public void autoReply(ChannelHandlerContext channelHandlerContext) {

    }

    /**
     * 系统信息
     *
     * @param clientInfo
     * @return
     */
    private Map<String, Object> getSystem(JSONObject clientInfo) {
        JSONObject systemJsonObject = clientInfo.getJSONObject("system");
        Map<String, Object> systemMap = getHashMap(10);
        systemMap.put("userHome", systemJsonObject.getString("userHome"));
        systemMap.put("javaVersion", systemJsonObject.getString("javaVersion"));
        systemMap.put("osName", systemJsonObject.getString("osName"));
        systemMap.put("userName", systemJsonObject.getString("userName"));
        systemMap.put("javaHome", systemJsonObject.getString("javaHome"));
        return systemMap;
    }

    /**
     * 磁盘列表信息
     *
     * @param clientInfo
     * @return
     */
    private List<Map<String, Object>> listDisk(JSONObject clientInfo) {
        JSONArray diskJsonArray = clientInfo.getJSONArray("disk");
        if (diskJsonArray == null) {
            return new ArrayList<>();
        }
        List<Map<String, Object>> diskList = new ArrayList<>();
        for (int i = 0; i < diskJsonArray.size(); i++) {
            JSONObject diskJsonObject = diskJsonArray.getJSONObject(i);
            Map<String, Object> diskMap = getHashMap(10);
            diskMap.put("path", diskJsonObject.getString("path"));
            diskMap.put("usable", diskJsonObject.getDoubleValue("usable"));
            diskMap.put("total", diskJsonObject.getDoubleValue("total"));
            diskMap.put("free", diskJsonObject.getDoubleValue("free"));
            diskList.add(diskMap);
        }
        return diskList;
    }

    /**
     * 内存信息
     *
     * @param clientInfo
     * @return
     */
    public Map<String, Object> getMemory(JSONObject clientInfo) {
        JSONObject memoryJsonObject = clientInfo.getJSONObject("memory");
        Map<String, Object> memoryMap = getHashMap(10);
        memoryMap.put("usedMemory", memoryJsonObject.getDoubleValue("usedMemory"));
        memoryMap.put("totalMemory", memoryJsonObject.getDoubleValue("totalMemory"));
        memoryMap.put("freePhysicalMemory", memoryJsonObject.getDoubleValue("freePhysicalMemory"));
        memoryMap.put("time", memoryJsonObject.getString("time"));
        return memoryMap;
    }

    /**
     * cpu信息
     *
     * @param clientInfo
     * @return
     */
    public Map<String, Object> getCPU(JSONObject clientInfo) {
        JSONObject cpuJsonObject = clientInfo.getJSONObject("cpu");
        Map<String, Object> cpuMap = getHashMap(10);
        cpuMap.put("freeRate", cpuJsonObject.getDoubleValue("freeRate"));
        cpuMap.put("userUseRate", cpuJsonObject.getDoubleValue("userUseRate"));
        cpuMap.put("systemUseRate", cpuJsonObject.getDoubleValue("systemUseRate"));
        cpuMap.put("waitRate", cpuJsonObject.getDoubleValue("waitRate"));
        cpuMap.put("coreCount", cpuJsonObject.getDoubleValue("coreCount"));
        cpuMap.put("time", cpuJsonObject.getString("time"));
        return cpuMap;
    }

    /**
     * jvm信息
     *
     * @param clientInfo
     * @return
     */
    public Map<String, Object> getJvm(JSONObject clientInfo) {
        JSONObject jvmJsonObject = clientInfo.getJSONObject("jvmMemory");
        Map<String, Object> jvmMap = getHashMap(10);
        jvmMap.put("maxMemorySize", jvmJsonObject.getDoubleValue("maxMemorySize"));
        jvmMap.put("usedMemorySize", jvmJsonObject.getDoubleValue("usedMemorySize"));
        jvmMap.put("initTotalMemorySize", jvmJsonObject.getDoubleValue("initTotalMemorySize"));
        jvmMap.put("time", jvmJsonObject.getString("time"));
        return jvmMap;
    }

}
