package com.cisdi.data.IDCardReader.gateway.impl;

import com.cisdi.data.IDCardReader.gateway.vo.ZKIDROnlineConfigVo;
import com.cisdi.data.sdk.gateway.base.SocketGatewayBase;
import com.cisdi.data.sdk.service.ServiceProvider;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class ZKIDROnlineClient implements Runnable{

    private String deviceId;
    ServiceProvider serviceProvider;
    SocketGatewayBase socketGateway;
    private ZKIDROnlineConfigVo zkidrOnlineConfigVo;
    private ZKIDROnlineListener zkidrOnlineListener=null;
    private Logger logger= LoggerFactory.getLogger(ZKIDROnlineClient.class);
    private AtomicBoolean shouldRun=new AtomicBoolean(true);

    private Boolean isOpen=false;

    public Boolean getOpen() {
        return isOpen;
    }

    public Boolean close()
    {
        zkidrOnlineListener.setShouldRun(false);
        shouldRun.set(false);
        return zkidrOnlineListener.endListen();
    }

    public ZKIDROnlineClient(String deviceId, ServiceProvider serviceProvider, SocketGatewayBase socketGateway, ZKIDROnlineConfigVo zkidrOnlineConfigVo) {
        this.deviceId = deviceId;
        this.serviceProvider = serviceProvider;
        this.socketGateway = socketGateway;
        this.zkidrOnlineConfigVo = zkidrOnlineConfigVo;
    }

    @Override
    public void run() {
        while (shouldRun.get())
        {
            try {
                zkidrOnlineListener=new ZKIDROnlineListener(new URI(zkidrOnlineConfigVo.getUri()),deviceId,socketGateway,serviceProvider,zkidrOnlineConfigVo);
                logger.info("ZKIDROnline网关:{}启动成功 参数:{}", socketGateway.getInstanceVo().getRunId(), socketGateway.getInstanceVo().getParameter());
                break;
            } catch (URISyntaxException e) {
                logger.error("连接ID:"+socketGateway.getInstanceVo().getRunId()+"，设备ID:"+deviceId+"，自动重连设置:"+zkidrOnlineConfigVo.getAutoReconnect()+"启动失败，错误原因："+e.getMessage());
                try {
                    Thread.sleep(zkidrOnlineConfigVo.getReconnectDelay());
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                if(zkidrOnlineConfigVo.getAutoReconnect())
                {
                    logger.info("连接ID:"+socketGateway.getInstanceVo().getRunId()+"，设备ID:"+deviceId+"，自动重连设置:"+zkidrOnlineConfigVo.getAutoReconnect()+"启动失败，正在重连");
                }
                else
                {
                    logger.info("ZKIDROnline网关:{}启动失败 参数:{}", socketGateway.getInstanceVo().getRunId(), socketGateway.getInstanceVo().getParameter());
                    break;
                }
            }
        }

        if(zkidrOnlineListener!=null) {
            zkidrOnlineListener.start(false);
            isOpen=true;
            return ;
        }
    }
}
