package com.mortals.xhx.daemon.netty.server.controlserver.handler;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.busiz.rsp.ServerInfo;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.utils.EncryptUtil;
import com.mortals.xhx.module.device.service.DeviceService;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;

import java.net.InetSocketAddress;

import static com.mortals.xhx.common.key.Constant.*;


/**
 * 业务处理
 *
 * @author: zxfei
 * @date: 2022/4/8 10:20
 */
@Slf4j
public class NettyUDPServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {


    //@Value("${application.registerApiPath:/api/register}")
    private String registerApiPath="/api/register";
    //@Value("${application.deviceInitApiPath:/api/deviceInit}")
    private String deviceInitApiPath="/api/deviceInit";
    //@Value("${application.deviceUpdateApiPath:/api/deviceUpdate}")
    private String deviceUpdateApiPath="/api/deviceUpdate";
    //@Value("${server.servlet.context-path:/m}")
    private String servletPath="/m";


    private DeviceService deviceService;

    public NettyUDPServerHandler(DeviceService deviceService) {
        this.deviceService = deviceService;


    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {

        ByteBuf byteBuf = packet.copy().content();
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        String content = new String(bytes);
        ApiResp<ServerInfo> resp = new ApiResp<>();
        resp.setCode(YesNoEnum.YES.getValue());
        log.info("receive->UDP entbefore:{}", packet.sender().toString() + "," + content);
        try {
            content = EncryptUtil.myReEnscrt(content, 9, DES_STR, ENCRYPT_STR);
            //EncryptUtil.decrypt(content, ENCRYPT_STR);
            log.info("receive->UDPafter:{}", packet.sender().toString() + "," + content);
            JSONObject jsonObject = JSON.parseObject(content);

            String action = jsonObject.getString("action");

            resp.setMsg("获取服务端地址成功！");
            if (!ObjectUtils.isEmpty(action) && "findserver".equals(action)) {
                String domain = GlobalSysInfo.getParamValue(PARAM_SERVER_HTTP_URL, "http://192.168.0.98:11091");
                String deviceRegisterApi = domain + servletPath + registerApiPath;
                String deviceInitApi = domain + servletPath + deviceInitApiPath;
                String deviceUpdateApi = domain + servletPath + deviceUpdateApiPath;

                ServerInfo serverInfo = new ServerInfo();
                serverInfo.setDeviceRegisterApi(deviceRegisterApi);
                serverInfo.setDeviceInitApi(deviceInitApi);
                serverInfo.setDeviceUpdateApi(deviceUpdateApi);
                serverInfo.setHomeUrl(RandomUtil.randomString(500));

                resp.setData(serverInfo);
            } else {
                resp.setCode(YesNoEnum.NO.getValue());
                resp.setMsg("请求参数异常！");
            }

            InetSocketAddress remoteAddress = new InetSocketAddress(packet.sender().getHostName(), CLIENT_PORT);
            String sa = EncryptUtil.myEnscrt(JSON.toJSONString(resp), 9, DES_STR, ENCRYPT_STR);

            log.info("enStr:{}",sa);

            log.info("deEncrypt:{}", EncryptUtil.myReEnscrt(sa, 9, DES_STR, ENCRYPT_STR));


            DatagramPacket sendpacket = new DatagramPacket(Unpooled.copiedBuffer(sa, CharsetUtil.UTF_8), remoteAddress);
            //ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(JSON.toJSONString(resp), CharsetUtil.UTF_8), packet.sender()));
            ctx.writeAndFlush(sendpacket);
        } catch (Exception e) {
            log.error("异常", e);
            resp.setCode(YesNoEnum.NO.getValue());
            resp.setMsg("加密解密异常！");
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // TODO Auto-generated method stub
        ctx.close();
        cause.printStackTrace();
    }
}
