package com.muyu.cloud.vehicle.gateway.service.impl;

import com.muyu.cloud.vehicle.gateway.domain.VehicleConnection;
import com.muyu.cloud.vehicle.gateway.domain.VinIp;
import com.muyu.cloud.vehicle.gateway.domain.model.MqttServerModel;
import com.muyu.cloud.vehicle.gateway.domain.properties.MqttProperties;
import com.muyu.cloud.vehicle.gateway.domain.req.VehicleConnectionReq;
import com.muyu.cloud.vehicle.gateway.mapper.VehicleConnectionMapper;
import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.redis.service.RedisService;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.muyu.cloud.vehicle.gateway.config.RabbitmqConfig.*;

@Log4j2
@Service
public class VehicleConnectionServiceImpl implements VehicleConnectionService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private VehicleConnectionMapper vehicleConnectionMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RedisService redisService;

    /**
     * 获取连接信息
     * @param vehicleConnectionReq 车辆连接请求参数
     * @return
     */
    @Override
    public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
        log.info("车辆连接请求:{}",vehicleConnectionReq.toString());

        // 使用交换机发送消息
//        rabbitTemplate.convertAndSend("exchange_topics_inform","inform.#.email.#",vehicleConnectionReq.getVehicleVin());
//        log.info("发送消息成功:{}",vehicleConnectionReq.getVehicleVin());


        VehicleConnection vehicleConnection = new VehicleConnection();
        //车辆vin
        vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin());
        //用户名
        vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
        //密码(vin+时间戳+随机数)
        vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
        //查询有没有这辆车的vin码
        List<String> selectVehicle = vehicleConnectionMapper.selectByVehicleVin(vehicleConnectionReq.getVehicleVin());

        if(selectVehicle.isEmpty()){
            //添加连接信息
            vehicleConnectionMapper.addConnect(vehicleConnection);
            log.info("车辆上线成功");
        }else {
            throw new RuntimeException("车辆无法重复预上线");
        }

        //先判断vin码
        if(redisService.hasKey(vehicleConnection.getVehicleVin())){
            log.error("============车辆：{}已经绑定过了",vehicleConnectionReq.getVehicleVin());
            throw new RuntimeException("=============车辆已经绑定过了");
        }

        MqttProperties mqttProperties = new MqttProperties();
        List<VehicleConnection> vehicleVin = selectByVehicleVin(vehicleConnectionReq.getVehicleVin());
        for (VehicleConnection connection : vehicleVin) {
            mqttProperties.setClientId(connection.getVehicleVin());
            mqttProperties.setUserName(connection.getUsername());
            mqttProperties.setPassword(connection.getPassword());
        }
        mqttProperties.setTopic("vehicle");
        mqttProperties.setQos(0);

//        //使用交换机发送消息
//        rabbitTemplate.convertAndSend(EXCHANGE_TOPICS_INFORM,QUEUE_INFORM_EMAIL,mqttProperties);
//        log.info("==============发送消息成功：{}",mqttProperties);

        //判断redis有没有count键
        if(redisTemplate.hasKey("oneCount")){
            //取出count
            Integer count = Integer.valueOf(redisTemplate.opsForValue().get("oneCount"));
            if(count == 1){
                redisTemplate.opsForValue().set("oneCount",String.valueOf(0));
            }else {
                redisTemplate.opsForValue().set("oneCount",String.valueOf(count+1));
            }
            //根据游标count获取服务IP
            Object ipList = redisService.redisTemplate.opsForList().index("oneIpList", count);
            log.info("=========================oneIpList:"+ipList);
            //关联车辆和服务
            this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ipList.toString()));
            //响应信息
            log.info("车辆:{}",vehicleConnectionReq.getVehicleVin()+"绑定成功:{}",ipList);
            mqttProperties.setBroker("tcp://"+ipList+":1883");

            //使用交换机发送消息
            rabbitTemplate.convertAndSend(EXCHANGE_TOPICS_INFORM,ROUTINGKEY_SMS,mqttProperties);
            log.info("==================发送消息成功：{}",mqttProperties);
            return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle"));

        }else {
            redisTemplate.opsForValue().set("oneCount",String.valueOf(0));
            //根据游标count获取服务器Ip
//            String ip = redisTemplate.opsForList().index("ipList", 0);
            Object ipList = redisService.redisTemplate.opsForList().index("oneIpList", 0);
            //关联车辆和服务
            this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ipList.toString()));
            //响应信息
            log.info("车辆:{}",vehicleConnectionReq.getVehicleVin(),"与:{}绑定成功",ipList);
            mqttProperties.setBroker("tcp://"+ipList+":1883");

            //使用交换机发送消息
            rabbitTemplate.convertAndSend(EXCHANGE_TOPICS_INFORM,ROUTINGKEY_SMS,mqttProperties);
            log.info("================发送消息成功：{}",mqttProperties);
            return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle"));
        }
    }


    /**
     * 查询车辆绑定的服务器信息
     * @param vehicleVin 车辆vin码集合
     * @return
     */
    private List<VehicleConnection> selectByVehicleVin(String vehicleVin) {
        return vehicleConnectionMapper.getMqttServerModel(vehicleVin);
    }

    /**
     * 添加车辆绑定IP地址存入redis中
     */
    public void addIpAddress(VinIp vinIp) {
        if (vinIp == null || vinIp.getVin() == null || vinIp.getVin().isEmpty() || vinIp.getIp() == null || vinIp.getIp().isEmpty()) {
            throw new IllegalArgumentException("vin 或 ip 不能为空或无效");
        }
        redisService.setCacheObject(vinIp.getVin(), vinIp.getIp());
    }
}
