package com.bizzan.bitrade.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aqmd.netty.annotation.HawkBean;
import com.aqmd.netty.annotation.HawkMethod;
import com.aqmd.netty.common.NettyCacheUtils;
import com.aqmd.netty.push.HawkPushServiceApi;
import com.bizzan.bitrade.constant.NettyCommand;
import com.bizzan.bitrade.entity.*;
import com.bizzan.bitrade.netty.QuoteMessage;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.SocketAddress;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 处理Netty订阅与取消订阅
 */
@HawkBean
@Slf4j
public class AnnouncementHandler {
    @Autowired
    private HawkPushServiceApi hawkPushService;
    private String topicOfAnnouncement = "CONTRACT_ANNOUNCEMENT";

    public void subscribeTopic(Channel channel, String topic){
        String userKey = channel.id().asLongText();
        if(!NettyCacheUtils.keyChannelCache.containsKey(channel)) {
            NettyCacheUtils.keyChannelCache.put(channel, userKey);
        }

        NettyCacheUtils.storeChannel(topic,channel);
        if(NettyCacheUtils.userKey.containsKey(userKey)){
            NettyCacheUtils.userKey.get(userKey).add(topic);
        }
        else{
            Set<String> userkeys=new HashSet<>();
            userkeys.add(topic);
            NettyCacheUtils.userKey.put(userKey,userkeys);
        }
    }

    public void unsubscribeTopic(Channel channel,String topic){
        String userKey = channel.id().asLongText();
        if(NettyCacheUtils.userKey.containsKey(userKey)) {
            NettyCacheUtils.userKey.get(userKey).remove(topic);
        }
        NettyCacheUtils.keyChannelCache.remove(channel);
    }

    @HawkMethod(cmd = NettyCommand.SUBSCRIBE_ANNOUNCEMENT)
    public QuoteMessage.SimpleResponse subscribeAnnouncement(byte[] body, ChannelHandlerContext ctx){
        QuoteMessage.SimpleResponse.Builder response = QuoteMessage.SimpleResponse.newBuilder();
        log.info("订阅Announcement：");
        subscribeTopic(ctx.channel(),topicOfAnnouncement);
        response.setCode(0).setMessage("订阅成功");
        return response.build();
    }

    @HawkMethod(cmd = NettyCommand.UNSUBSCRIBE_ANNOUNCEMENT)
    public QuoteMessage.SimpleResponse unsubscribeAnnouncement(byte[] body, ChannelHandlerContext ctx){
        QuoteMessage.SimpleResponse.Builder response = QuoteMessage.SimpleResponse.newBuilder();
        log.info("取消订阅Announcement：");
        unsubscribeTopic(ctx.channel(), topicOfAnnouncement);
        response.setCode(0).setMessage("取消订阅成功");
        return response.build();
    }

    /**
     * 推送最新公告
     * @param symbol
     * @param thumb
     */
    public void handleAnnouncement(Announcement announcement) {
    	log.info("netty公告刷新：announcement-{}", announcement);
        byte[] body = JSON.toJSONString(announcement).getBytes();
        hawkPushService.pushMsg(NettyCacheUtils.getChannel(topicOfAnnouncement),NettyCommand.SEND_ANNOUNCEMENT, body);
    }

    
}
