package com.tax.token.service.websocket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tax.token.service.api.TkTokenService;
import com.tax.token.service.websocket.bean.SocketSendBean;
import com.tax.token.service.websocket.bean.SocketSendMsg;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.constants.RedisContants;
import com.yxcheng.token.utils.SpringUtil;
import com.yxcheng.token.utils.StringUtil;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import io.netty.channel.Channel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class WebSocketClientService {


    public static void executeMsg(String msg, Channel channel){
        if(StringUtil.isBlank(msg))return;

        SocketSendMsg socketSendMsg = getSocketSendMsg(msg);
        if(socketSendMsg.getCode()!=0)return;

        SocketSendBean bean =  getBean(msg);
        switch (bean.getOperationType()){
            case WebSocketConstants.GetToken:
                //从服务端获取到token
                    getToken(bean);
                    break;
            case WebSocketConstants.sendToken:
                sendTaxNoForToken(channel,bean.getCertno());
                break;
            case WebSocketConstants.GetSign:
                getSign(bean);
                break;
        }
    }

    private static void getSign(SocketSendBean bean) {
        if(StringUtils.isNotBlank(bean.getSigndata())){
            RedisService redisService = SpringUtil.getBean(RedisService.class);
            redisService.set(RedisContants.TOKEN_WEB_SOCKET_GETSIGN + bean.getCertno(),bean.getSigndata(),60*5);
        }
    }

    public static void sendClientHelloForToken(Channel channel, String taxno,String zoneCode) {
        SocketSendMsg socketSendMsg = new SocketSendMsg();
        SocketSendBean bean = new SocketSendBean();
        bean.setOperationType(WebSocketConstants.sendClientHello);
        bean.setCertno(taxno);
        bean.setAreacode(zoneCode);
        socketSendMsg.setData(bean);
        System.out.println("sendClientHelloForToken: " + socketSendMsg);
        TextWebSocketFrame frame = new TextWebSocketFrame(JSON.toJSONString(socketSendMsg));
        channel.writeAndFlush(frame);
    }



    public static void sendDataForSign(Channel channel,  String taxno, String strData) {
        SocketSendMsg socketSendMsg = new SocketSendMsg();
        SocketSendBean bean = new SocketSendBean();
        bean.setOperationType(WebSocketConstants.sendSign);
        bean.setSigndata(strData);
        bean.setCertno(taxno);
        socketSendMsg.setData(bean);
        System.out.println("sendDataForSign: " + socketSendMsg);
        TextWebSocketFrame frame = new TextWebSocketFrame(JSON.toJSONString(socketSendMsg));
        channel.writeAndFlush(frame);
    }



    private static void getHearBeat(SocketSendBean bean) {
        System.out.println("getHeartBean: " + bean);
        RedisService redisService = SpringUtil.getBean(RedisService.class);
        Map<String, String> onlineMap = redisService.hGetAll(RedisContants.TOKEN_WEB_SOCKET_ISONLINE);
        if(onlineMap == null){
            onlineMap = new HashMap<>();
        }
        onlineMap.put(bean.getCertno(),bean.getOnline());
        redisService.hMSet(RedisContants.TOKEN_WEB_SOCKET_ISONLINE,onlineMap,-1);
        System.out.println("Socket-onlineMap:" + onlineMap);
    }

    public static void connectClient(Channel channel){
        SocketSendMsg socketSendMsg = new SocketSendMsg();
        SocketSendBean bean = new SocketSendBean();
        bean.setOperationType(WebSocketConstants.connectClient);
        socketSendMsg.setData(bean);
        System.out.println("connectClient: " + socketSendMsg);
        TextWebSocketFrame frame = new TextWebSocketFrame(JSON.toJSONString(socketSendMsg));
        channel.writeAndFlush(frame);
    }

    public static void sendTaxNoForToken(Channel channel,String taxNo){
        SocketSendMsg socketSendMsg = new SocketSendMsg();
        SocketSendBean bean = new SocketSendBean();
        bean.setOperationType(WebSocketConstants.sendToken);
        bean.setCertno(taxNo);
        socketSendMsg.setData(bean);
        System.out.println("sendTaxNoForToken: " + socketSendMsg);
        TextWebSocketFrame frame = new TextWebSocketFrame(JSON.toJSONString(socketSendMsg));
        channel.writeAndFlush(frame);
    }


    public static void getToken(SocketSendBean bean){
        System.out.println("WebSocketClientService GetToken:" + bean);
        if(StringUtils.isNotBlank(bean.getToken())) {
            RedisService redisService = SpringUtil.getBean(RedisService.class);
            redisService.set(RedisContants.TOKEN_WEB_SOCKET + bean.getCertno(),bean.getToken(),60);
            TkTokenService tkTokenService = SpringUtil.getBean(TkTokenService.class);
            tkTokenService.saveToken(bean.getCertno(), bean.getToken());
        }
    }

    public static SocketSendMsg getSocketSendMsg(String msg){
        SocketSendMsg bean =  JSON.parseObject(msg,SocketSendMsg.class);
        return bean;
    }

    public static SocketSendBean getBean(String msg){
        JSONObject jsonObject = JSON.parseObject(msg);
        SocketSendBean bean =  JSON.parseObject(jsonObject.getString("data"),SocketSendBean.class);
        return bean;
    }


}
