package com.wecloud.im.controller;

import cn.hutool.crypto.digest.MD5;
import com.wecloud.im.param.GetSignParam;
import com.wecloud.im.ws.manager.ChannelManager;
import com.wecloud.im.ws.model.ClientInfo;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.util.RamUsageEstimator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * sign 控制器
 *
 * @author wei
 * @since 2021-04-27
 */
@Slf4j
@RestController
@RequestMapping("/signDemo")
@Api(value = "signAPI", tags = {"获取sign(Demo)"})
public class SignController extends BaseController {

    /**
     * 根据客户方生成签名字符串 验证通过则下发token
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取sign(仅提供测试调试使用)", notes = "生成签名测试,在生产环境中,此步骤需要第三方应用的服务端进行生成")
    public String get(@RequestBody GetSignParam getSignParam) throws Exception {

        return new MD5().digestHex(getSignParam.getTimestamp() + getSignParam.getClientId()
                + getSignParam.getAppKey() + getSignParam.getAppSecret() + getSignParam.getPlatform());
    }

    /**
     * monitor
     */
    @PostMapping("/monitor")
    @ApiOperation(value = "monitor", notes = "monitor")
    public String monitor() {
        int sessionInfoMapSize = ChannelManager.SESSION_INFO_MAP.size();
        Long sizeStr = RamUsageEstimator.sizeOf(ChannelManager.SESSION_INFO_MAP);
        Long shallowSizeStr = RamUsageEstimator.shallowSizeOf(ChannelManager.SESSION_INFO_MAP);
        Long total = 0L;
        for (ClientInfo value : ChannelManager.SESSION_INFO_MAP.values()) {
            Long size = RamUsageEstimator.shallowSizeOf(value);
            log.info("size: {}", size);
            total += size;
        }

        String sizeClientId = RamUsageEstimator.humanSizeOf(ChannelManager.CLIENT_ID);
        String sizeAppId = RamUsageEstimator.humanSizeOf(ChannelManager.APPLICATION_ID);
        String sizeReadIdle = RamUsageEstimator.humanSizeOf(ChannelManager.READ_IDLE_TIMES);
        String sizePlatform = RamUsageEstimator.humanSizeOf(ChannelManager.PLATFORM);

        StringBuilder sb = new StringBuilder()
                .append("sessionInfoMapSize: ").append(sessionInfoMapSize).append("---")
                .append("sizeStr:").append(sizeStr).append("---")
                .append("shallowSizeStr:").append(shallowSizeStr).append("---")
                .append("total:").append(total).append("---")
                .append("sizeClientId:").append(sizeClientId).append("---")
                .append("sizeAppId:").append(sizeAppId).append("---")
                .append("sizeReadIdle:").append(sizeReadIdle).append("---")
                .append("sizePlatform:").append(sizePlatform).append("---")
                ;

        log.info(sb.toString());

        return sb.toString();
    }

}

