#include "hkvideocontrol.h"
#include <QDebug>
#include <QMessageBox>
HkVideoControl::HkVideoControl(QObject *parent) : QObject(parent)
{

}

bool HkVideoControl::initSDK()
{
    bool isok = NET_DVR_Init();
    if(isok == false)
    {
        qDebug()<< "NET_DVR_Init error;error number is " <<NET_DVR_GetLastError();
        QMessageBox::warning(NULL,"error","NET_DVR_Init error;error number is "+QString::number(NET_DVR_GetLastError()));
        return isok;
    }
    //设置连接时间与重连时间
    NET_DVR_SetConnectTime(2000, 1);
    NET_DVR_SetReconnect(10000, true);
    return isok;
}

bool HkVideoControl::uninitSDK()
{
    bool isok = NET_DVR_Cleanup();
    if(isok == false)
    {
        qDebug()<< "NET_DVR_Cleanup error;error number is " <<NET_DVR_GetLastError();
        QMessageBox::warning(NULL,"error","NET_DVR_Cleanup error;error number "+QString::number(NET_DVR_GetLastError()));
        return isok;
    }
    return isok;
}

void HkVideoControl::HkVideo_Login(QString ip, int port, QString user, QString pwd)
{
    initSDK();
    NET_DVR_DEVICEINFO_V30 DeviceInfoTmp;
    char * ipc = new char[strlen(ip.toStdString().c_str()) + 1];
    strcpy_s(ipc, strlen(ip.toStdString().c_str()) + 1, ip.toStdString().c_str());

    char * userc = new char[strlen(user.toStdString().c_str()) + 1];
    strcpy_s(userc, strlen(user.toStdString().c_str()) + 1, user.toStdString().c_str());

    char * pwdc = new char[strlen(pwd.toStdString().c_str()) + 1];
    strcpy_s(pwdc, strlen(pwd.toStdString().c_str()) + 1, pwd.toStdString().c_str());
    //NET_DVR_Login_V30(char *sDVRIP, WORD wDVRPort, char *sUserName, char *sPassword, LPNET_DVR_DEVICEINFO_V30 lpDeviceInfo);
    userID=NET_DVR_Login_V30(ipc,port,userc,pwdc,&DeviceInfoTmp);
    if(userID<0)
    {
        qDebug() << "NET_DVR_Login_V30 error;" << "error number is " << NET_DVR_GetLastError();
        QMessageBox::warning(nullptr,"error","NET_DVR_Login_V30 error;error number "+QString::number(NET_DVR_GetLastError()));
        return;
    }
    emit Send_UserID(userID);
}
