package com.ruoyi.common.server;


import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.ModBusUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;


/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@ChannelHandler.Sharable
@Component
public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
    private static final Logger log = LoggerFactory.getLogger(EnvInputServerHandler.class);
    private StringBuilder askTextSb = null;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    private Map<String, String> channelMap = new ConcurrentHashMap<>();

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // 当客户端连接时，打印出客户端的 IP 地址
        System.out.println("连接成功");
        ByteBuf bufff = Unpooled.buffer();
        bufff.writeBytes(hexString2Bytes("5A000101000000DCE5"));
        ctx.writeAndFlush(bufff);
        super.channelActive(ctx);
    }

    // 设备数据的前缀
    private static final String DEVICE_PREFIX = "device:";
    private  static Integer INDEX = 1;
    //
    public void appendClientAsk(String text) {
        if (this.askTextSb == null) {
            askTextSb = new StringBuilder();
        }
        askTextSb.append(text);
    }
    @Override
    public  void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        System.out.println("收到的数据："+msg);

        super.channelRead(ctx, msg);
    }

    public   static boolean isStartOfToday(Date date) {
        // 创建一个示例的LocalDateTime对象
        LocalDateTime exampleTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        // 获取今天的开始时间
        LocalDateTime todayStart = LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.MIDNIGHT);

        // 比较给定的时间和今天的开始时间
        if (exampleTime.isAfter(todayStart)) {
            System.out.println("给定的时间大于今天的开始时间");
            return true;
        } else {
            System.out.println("给定的时间不大于今天的开始时间");
            return false;
        }
    }
    public  boolean isTimeBetween(LocalTime startTime, LocalTime endTime) {
        LocalDateTime now = LocalDateTime.now();

        // 获取当前日期的00:00和23:59:59
        LocalDateTime startOfDay = now.withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfDay = startOfDay.plusDays(1).minusSeconds(1);

        // 创建表示时间段的开始和结束时间的LocalDateTime对象
        LocalDateTime startDateTime = LocalDateTime.of(now.toLocalDate(), startTime);
        LocalDateTime endDateTime = LocalDateTime.of(now.toLocalDate(), endTime);

        // 判断当前时间是否在两个时间段之一内
        if (endDateTime.isBefore(startDateTime)) {
            // 时间段跨越午夜，分两部分判断
            return (now.isAfter(startDateTime) && now.isBefore(endOfDay)) ||
                    (now.isAfter(startOfDay) && now.isBefore(endDateTime));
        } else {
            // 时间段在同一天内，直接判断
            return now.isAfter(startDateTime) && now.isBefore(endDateTime);
        }
    }
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        String key = channel.id().asLongText(); // 或者使用其他唯一标识符
        System.out.println("退出-->"+key);
        channelMap.remove(key);
        super.channelInactive(ctx);
    }
    public static byte[] hexString2Bytes(String src) {

        int l = src.length() / 2;

        byte[] ret = new byte[l];

        for (int i = 0; i < l; i++) {

            ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();

        }

        return ret;

    }





    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

//        if (true){
//            ctx.writeAndFlush(Unpooled.copiedBuffer(new byte[]{1, 2, 3}));
//        }

//        if (askTextSb.toString().indexOf("end") != -1) {
//            // {处理客户端消息}


//            handleClientAskCmd(askTextSb.toString(), ctx);
//            //清空重置;
//            askTextSb.delete(0, askTextSb.length());
//        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        if (cause.getMessage().indexOf("Connection reset") != -1) {
            log.info("相关采集器设备正在重启:" + cause.toString());
        }
        ctx.close();
    }

    //应答
    public void answerCmd(String answerText, ChannelHandlerContext ctx) {
        ctx.writeAndFlush(Unpooled.copiedBuffer(answerText.getBytes()));
    }


}
