package com.muyu.vehicle;

import com.alibaba.fastjson.JSONObject;
import com.muyu.web.common.ScheduledThreadPool;
import com.muyu.web.domain.MqttProperties;
import com.muyu.web.domain.VehicleInfo;
import com.muyu.web.domain.model.PositionModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledFuture;

@Data
@Builder
@Log4j2
@NoArgsConstructor
@AllArgsConstructor
public class VehicleInstance {
    /**
     * 路线轨迹编码
     */
    private String positionCode;

    /**
     * 路径队列
     */
    private LinkedBlockingQueue<PositionModel> positionQueue=new LinkedBlockingQueue<>();

    /**
     * 车辆
     */
    private VehicleInfo vehicleInfo;

    /**
     * 车辆工作线程
     */
    private VehicleThread vehicleThread;

    /**
     * MQTT配置
     */
    private MqttProperties mqttProperties;

    /**
     * 线程提交回调
     */
    private ScheduledFuture<?>scheduledFuture;

    /**
     * 连接上报
     */
    private MqttClient client;


    /**
     * 获取当前车辆VIN
     */
    public String getVin(){
        return this.vehicleInfo.getVin();
    }
    /**
     * 获取车辆租户信息
     */
    public String getTenantId(){
        return this.vehicleInfo.getTenantId();
    }

    /**
     * 发送消息
     */
    public void sengMsg(String msg){

    }


    /**
     * 初始化客户端
     */
    public void initClient(){
        try {
            client = new MqttClient(mqttProperties.getBroker(), mqttProperties.getClientId(), new MemoryPersistence());
            MqttConnectOptions options = new MqttConnectOptions();
            //设置用户名和密码
            if (Objects.nonNull(mqttProperties.getUserName())&&Objects.nonNull(mqttProperties.getPassword())){
             options.setUserName(mqttProperties.getUserName());
             options.setPassword(mqttProperties.getPassword().toCharArray());
            }
            options.setConnectionTimeout(1);
            options.setKeepAliveInterval(20);
            //连接
            client.connect(options);
            log.debug("车辆:[{}]客户端初始化成功连接配置:{}",getVin(),
                    JSONObject.toJSONString(this.mqttProperties)
            );
            VehicleThread vehicleThread = new VehicleThread();
            vehicleThread.setVehicleInstance(this);
            this.setVehicleThread(vehicleThread);
            ScheduledFuture<?> submit = ScheduledThreadPool.submit(vehicleThread);
            this.setScheduledFuture(submit);
            log.info("初始化车辆上报模拟线程开始：[{}]", this.getVin());
        } catch (MqttException e) {
            log.error("车辆：[{}] 客户端初始化异常", getVin(), e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 是否连接在线
     */
    public boolean isOnline(){
        if (this.client==null){
            return false;
        }
        return this.client.isConnected();
    }

    /**
     * 是否建立车辆模拟线程
     */
    public boolean isSend(){
        return this.vehicleThread!=null;
    }
}
