#include "gatewaytcpclient.h"

#include <QSettings>
#include <QApplication>
#include <QAbstractSocket>

GatewayTcpClient::GatewayTcpClient(QObject *parent) :
    QObject(parent),
    m_pClientSocket(NULL),
    m_pConnectCheckTimer(NULL),
    m_connected(0),
    m_pConnectDetectThread(NULL),
    m_pConnectDetect(NULL),
    m_detect(0),
    m_localip(DEF_LOCAL_IP),
    m_localport(DEF_LOCAL_PORT),
    m_serverip(DEF_SERVER_IP),
    m_serverport(DEF_SERVER_PORT)
{
    m_pClientSocket = new QBindTcpSocket(this);
    m_pClientSocket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
    connect(m_pClientSocket, SIGNAL(readyRead()), this, SLOT(receiveServerData()));
    connect(m_pClientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(displaySocketError(QAbstractSocket::SocketError)));
    //    connect(m_pClientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(netStateChanged(QAbstractSocket::SocketState)));

    m_pConnectCheckTimer = new QTimer(this);
    connect(m_pConnectCheckTimer, SIGNAL(timeout()), this, SLOT(connectCheck()));


    m_pConnectDetectThread = new QThread();
}

GatewayTcpClient::~GatewayTcpClient()
{
    m_pConnectCheckTimer->stop();
    delete m_pConnectCheckTimer;

    m_pClientSocket->disconnectFromHost();
    m_pClientSocket->waitForDisconnected();
    m_pClientSocket->close();
    delete m_pClientSocket;

    if (m_connected != 0)
    {
        m_pConnectDetectThread->quit();
        m_pConnectDetectThread->wait();
    }

    delete m_pConnectDetectThread;
}

void GatewayTcpClient::setConfigFileName(QString configfilename)
{
    m_configFileName = configfilename;
}

void GatewayTcpClient::connectToServer()
{
    if (m_pClientSocket == NULL)
    {
        qDebug() << "m_pClientSocket not alloc";
        return;
    }

    if (m_connected == 0)
    {
        loadIpAndPort();
        if (m_pConnectDetect == NULL)
        {
            m_pConnectDetect = new TcpConnectDetect;
        }
        qDebug() << "ConnectToServer";
        qDebug() << "localip=" << m_localip << "localport=" << m_localport;
        qDebug() << "serverip=" << m_serverip << "serverport=" << m_serverport;

        m_pConnectDetect->setIpAndPort(m_localip, m_localport, m_serverip, m_serverport);

        m_pConnectDetect->moveToThread(m_pConnectDetectThread);

        connect(this, SIGNAL(siDetectHost()), m_pConnectDetect, SLOT(connectDetect()), Qt::QueuedConnection);
        connect(m_pConnectDetect, SIGNAL(siConnectSta(int)), this, SLOT(detectStatus(int)), Qt::QueuedConnection);

        // 20191226由于检测线程会造成网络重连，所以将检测线程注释 lft
/*
        connect(m_pConnectDetectThread, SIGNAL(started()), m_pConnectDetect, SLOT(detectStart()) );
        connect(m_pConnectDetectThread, SIGNAL(finished()), m_pConnectDetect, SLOT(deleteLater()) );  // 退出删除对象

        m_pConnectDetectThread->start();    // 启动线程
*/
        m_pConnectCheckTimer->start(1000);
        m_connected = 1;
        m_detect = 1;
    }
}

void GatewayTcpClient::disConnectFromServer()
{
    if (m_connected != 0)
    {
        m_pConnectCheckTimer->stop();

        if (m_pClientSocket != NULL)
        {
            m_pClientSocket->disconnectFromHost();
            m_pClientSocket->waitForDisconnected();
            m_pClientSocket->close();
        }

        m_pConnectDetectThread->quit();
        m_pConnectDetectThread->wait();

        m_connected = 0;
        m_detect = 0;
    }
}

void GatewayTcpClient::loadIpAndPort()
{
    if (!m_configFileName.isEmpty())
    {
        // 机器的IP和端口 和 连接机器的本地IP和端口
        QSettings configIni(m_configFileName, QSettings::IniFormat);
        QString serverip, localip;
        quint16 serverport, localport;

        //网关IP，不固定，但是不能与主板连接IP相同
        serverip = configIni.value("server/ip", QVariant("192.168.2.95")).toString();
        serverport = configIni.value("server/port", QVariant(8080)).toInt();
        localip = configIni.value("local/ip", QVariant("192.168.2.100")).toString();
        localport = configIni.value("local/port", 5001).toInt();

        // 回写参数
        configIni.setValue("server/ip", serverip);
        configIni.setValue("server/port", serverport);
        configIni.setValue("local/ip", localip);
        configIni.setValue("local/port", localport);

        m_localip = localip;
        m_localport = localport;
        m_serverip = serverip;
        m_serverport = serverport;
    }
}

// 发送数据的槽
void GatewayTcpClient::slotSendData(QByteArray dat)
{
    if (m_pClientSocket != NULL)
    {
        m_pClientSocket->write(dat);
    }
}

// 自动检测连接
void GatewayTcpClient::connectCheck()
{
    if (m_pClientSocket == NULL)
    {
        qDebug() << "Socket is not alloced";
        return;
    }

//  qDebug() << "check client connect";
    if (m_connected == 1)
    {
        if (m_localip.isEmpty() == false)
        {
            m_pClientSocket->bindAddrAndPort(m_localip, m_localport);
        }
        m_pClientSocket->abort();
        m_pClientSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
        m_pClientSocket->connectToHost(m_serverip, m_serverport);
        int rslt = m_pClientSocket->waitForConnected(2000);
        if (rslt == 0)
        {

        }
        /*
        qDebug("bind connect and waitForConnected 2000, rslt=%d", rslt);
        qDebug() << "localip=" << m_localip << "localport=" << m_localport;
        qDebug() << "serverip=" << m_serverip << "serverport=" << m_serverport;
        */
        m_connected = 2;
    }
    else if (m_connected != 0)
    {
        switch(m_pClientSocket->state())
        {
            case QAbstractSocket::ConnectedState:
            {
                if (m_connected != 3)
                {
                    /*
                    qDebug() << "net State is Connected";
                    qDebug() << "localip=" << m_localip << "localport=" << m_localport;
                    */
                    m_connected = 3;       // 检测连接状态
                }

                break;
            }
            case  QAbstractSocket::ConnectingState:
            {
                if (m_connected != 2)
                {
                    qDebug() << "net State is Connecting";
                 //   qDebug() << "localip=" << m_localip << "localport=" << m_localport;
                }
                m_connected = 2;
                break;
            }
            case QAbstractSocket::UnconnectedState:
            {
                if (m_connected != 1)
                {
                //    qDebug() << "net State is unconnected";
                //    qDebug() << "localip=" << m_localip << "localport=" << m_localport;
                }
                m_connected = 1;
                break;
            }
            default:
            {
            /*
                QAbstractSocket::HostLookupState
                QAbstractSocket::BoundState
                QAbstractSocket::ListeningState
                QAbstractSocket::ClosingState
            */
                {
                    qDebug("net State is %d\r\n", m_pClientSocket->state());
                    qDebug() << "localip=" << m_localip << "localport=" << m_localport;
                    qDebug() << "serverip=" << m_serverip << "serverport=" << m_serverport;
                }
                m_connected = 1;
            }
        }
    }
    else
    {

    }

    if (m_connected == 3)   // 已连接
    {
        if (m_detect == 1)
        {
         //   qDebug() << "Machine " << QThread::currentThread();
            m_detect = 0;
            emit(siDetectHost());
         //   qDebug() << "after send SiDetectHost ";
        }
    }

    emit (siConnectSta(m_connected));     // 网络连接信号
}

void GatewayTcpClient::detectStatus(int sta)
{
    qDebug("gateway host detect sta=%d", sta);

    m_detect = 1;
    if (sta == 1)  // 网络已断开
    {
        qDebug("disconnect ClientSocket by detect");
        m_pClientSocket->disconnectFromHost();
//        m_pClientSocket->waitForDisconnected();
        m_pClientSocket->close();
        m_connected = 1;
    }
    else if (sta == 2)     // 网络连接还在
    {

    }
    else
    {

    }
}

void GatewayTcpClient::receiveServerData()
{
    if (m_pClientSocket != NULL)
    {
        QByteArray tmpDat;
        tmpDat = m_pClientSocket->readAll(); // 读取
//      qDebug("ReceiveServerData, size = %d", tmpDat.size());
//      PrintAsHex(&tmpDat);

        emit(siReceiveData(tmpDat));          // 发送收到数据信号
    }
}

void GatewayTcpClient::displaySocketError(QAbstractSocket::SocketError err)
{
    QString errinfo;

    errinfo.sprintf("Err: code=%d, errorString=", err);
    errinfo += m_pClientSocket->errorString();

//    qDebug() << errinfo;

    emit(siConnectErr(errinfo));    // 网络错误信息
}

void GatewayTcpClient::netStateChanged(QAbstractSocket::SocketState sta)
{
    switch (sta)
    {
    /*
    case UnconnectedState:
    case HostLookupState:
    case ConnectingState:
    case ConnectedState:
    case BoundState:
    case ListeningState:
    case ClosingStat:
    */
    default:
        break;
    }

}
void GatewayTcpClient::hostFound()
{
    qDebug("found host");
}







