#include <iostream>
#include <cstring>
#include <bitset>
#include <unistd.h>
#include <time.h>
#include "HCNetSDK.h"
#include "HkStreamer.h"

void CALLBACK fPlayDataCallBack(LONG lPlayHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void* dwUser)
{
  if (dwBufSize > 0)
  {
    char pb[dwBufSize] = {0};
    memcpy(pb, pBuffer, dwBufSize);
    
    std::cout.write(pb, dwBufSize);
    std::cout.flush();
  }
  int pos = NET_DVR_GetDownloadPos(lPlayHandle);
  if (pos > 98)
  {
    fprintf(stderr, "pyd---Current Time:%d, data size:%d\n", time(NULL), dwBufSize);
  }
}

int login(LPLoginInfo loginInfo)
{
  NET_DVR_Init();
  long lUserID;
  // login
  NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
  NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};
  struLoginInfo.bUseAsynLogin = false;

  struLoginInfo.wPort = 8000;
  memcpy(struLoginInfo.sDeviceAddress, loginInfo->sDeviceAddress, NET_DVR_DEV_ADDRESS_MAX_LEN);
  memcpy(struLoginInfo.sUserName, loginInfo->sUserName, NAME_LEN);
  memcpy(struLoginInfo.sPassword, loginInfo->sPassword, NAME_LEN);
  std::cerr << struLoginInfo.sDeviceAddress << " " << struLoginInfo.sUserName << " " << struLoginInfo.sPassword << std::endl;
  lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
  return lUserID;
}
void cpTime(NET_DVR_TIME *nvrTime, StreamDate date)
{
  nvrTime->dwYear = date.dwYear;
  nvrTime->dwMonth = date.dwMonth;
  nvrTime->dwDay = date.dwDay;
  nvrTime->dwHour = date.dwHour;
  nvrTime->dwMinute = date.dwMinute;
  nvrTime->dwSecond = date.dwSecond;
}
int playback(LPLoginInfo loginInfo, LPStream stream)
{
  long lUserID = login(loginInfo);
  if (lUserID < 0)
  {
    std::cerr << "pyd1---Login error" << NET_DVR_GetLastError() << std::endl;
    return HPR_ERROR;
  }
  NET_DVR_VOD_PARA struVodPara = {0};
  StreamDate start = stream->start;
  struVodPara.struIDInfo.dwChannel = stream->lChannel;
  cpTime(&struVodPara.struBeginTime, start);
  cpTime(&struVodPara.struEndTime, stream->end);
  int hPlayback = NET_DVR_PlayBackByTime_V40(lUserID, &struVodPara);
  if (hPlayback < 0)
  {
    std::cerr << "pyd1---PlayBack error: " << NET_DVR_GetLastError() << std::endl;
    return HPR_ERROR;
  }
  char usrInfo[200];
  if (!NET_DVR_SetPlayDataCallBack_V40(hPlayback, fPlayDataCallBack, usrInfo))
  {
    std::cerr << "NET_DVR_SetPlayDataCallBack fail!\n"
              << std::endl;
  }

  time_t lstart = time(NULL);
  if (!NET_DVR_PlayBackControl_V40(hPlayback, NET_DVR_PLAYSTART,NULL, 0, NULL, NULL))
  {
    std::cerr << "play back control failed : " << NET_DVR_GetLastError() << std::endl;
    return HPR_ERROR;
  }

  int pos = 0;
  for (pos = 0; pos < 100 && pos >= 0; pos = NET_DVR_GetDownloadPos(hPlayback))
  {
    std::cerr<<"Pos:"<<pos<<std::endl;
    sleep(1);
  }
  std::cerr << "have got: " << pos << std::endl;
  if (!NET_DVR_StopGetFile(hPlayback))
  {
    std::cerr << "failed to stop get file: " << NET_DVR_GetLastError() << std::endl;
    return HPR_ERROR;
  }

time_t lend = time(NULL);
  std::cerr << "StopGetFile\n"
            << "Use:"
            << lend - lstart
            << std::endl;
  if (pos < 0 || pos > 100)
  {
    std::cerr << "download err:" << NET_DVR_GetLastError() << std::endl;
    return HPR_ERROR;
  }
  else
  {
    return 0;
  }

  // sleep(180); //˯3����
  NET_DVR_Logout_V30(lUserID);
  // cleanup
  NET_DVR_Cleanup();

  return HPR_OK;
}
int getCfg(LPLoginInfo loginInfo)
{
  long lUserID = login(loginInfo);
  if (lUserID < 0)
  {
    std::cerr << "pyd1---Login error" << NET_DVR_GetLastError() << std::endl;
    return HPR_ERROR;
  }
  NET_DVR_IPPARACFG_V40 ipcfg;
  DWORD bytesReturned = 0;
  ipcfg.dwSize = sizeof(NET_DVR_IPPARACFG_V40);
  int iGroupNO = 0;
  bool resCode = NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_IPPARACFG_V40, iGroupNO, &ipcfg, sizeof(NET_DVR_IPPARACFG_V40), &bytesReturned);
  if (!resCode)
  {
    DWORD code = NET_DVR_GetLastError();
    std::cout << "NET_DVR_GetDVRConfig failed " << NET_DVR_GetErrorMsg((LONG *)(&code)) << std::endl;
    NET_DVR_Logout(lUserID);
    NET_DVR_Cleanup();
    return -1;
  }
  std::cout << "设备组 " << ipcfg.dwGroupNum << " 数字通道个数 " << ipcfg.dwDChanNum << " 起始通道 " << ipcfg.dwStartDChan << std::endl
            << std::endl;
  for (int i = 0; i < ipcfg.dwDChanNum; i++)
  {
    NET_DVR_PICCFG_V30 channelInfo;
    bytesReturned = 0;
    channelInfo.dwSize = sizeof(NET_DVR_PICCFG_V30);
    int channelNum = i + ipcfg.dwStartDChan;
    NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_PICCFG_V30, channelNum, &channelInfo, sizeof(NET_DVR_PICCFG_V30), &bytesReturned);
    std::cout << "通道号 " << channelNum << "\t通道名称 " << channelInfo.sChanName;
    std::cout << "\t用户名 " << ipcfg.struIPDevInfo[i].sUserName << "\t密码 " << ipcfg.struIPDevInfo[i].sPassword;
    std::cout << "\t设备ID " << (long)ipcfg.struIPDevInfo[i].szDeviceID;
    std::cout << "\tip地址 " << ipcfg.struIPDevInfo[i].struIP.sIpV4 << "\t端口 " << ipcfg.struIPDevInfo[i].wDVRPort << std::endl;
  }
  NET_DVR_Logout(lUserID);
  NET_DVR_Cleanup();
  return 0;
}