#include "include/library.h"
#include "./src/HIKBallCamera.h"
#include "src/HIKNvr.h"
#include <cstring>

bool DVR_Init(){
    return HK_DVR_Init();
}

void* NewHIKBallCamera() {
    return new HIKBallCamera();
}

bool InitBallCamera(void* PtrHIKBallCamera,const char* ip, const char* port,const char* username,const char* password,const char* BallMachineType){
    auto* HIKBallCameraObj = (HIKBallCamera*)PtrHIKBallCamera;
    return HIKBallCameraObj->InitBallCamera(ip,port,username,password,BallMachineType);
}

bool PtzGotoPut(void* PtrHIKBallCamera,int Action, float P, float T, float Z){
    auto* HIKBallCameraObj = (HIKBallCamera*)PtrHIKBallCamera;
    return HIKBallCameraObj->PtzGotoPut(Action,P,T,Z);
}

bool PtzGet(void* PtrHIKBallCamera,void *P, void *T, void *Z){
    auto* HIKBallCameraObj = (HIKBallCamera*)PtrHIKBallCamera;
    return HIKBallCameraObj->PtzGet((float*)P,(float*)T,(float*)Z);
}

bool StopBus(void* PtrHIKBallCamera,int direction){
    auto* HIKBallCameraObj = (HIKBallCamera*)PtrHIKBallCamera;
    return HIKBallCameraObj->StopBus(direction);
}

bool StartBus(void* PtrHIKBallCamera,int direction,int speed){
    auto* HIKBallCameraObj = (HIKBallCamera*)PtrHIKBallCamera;
    return HIKBallCameraObj->StartBus(direction,speed);
}

void* NewHIKNvr(){
    return new HIKNvr();
}

bool InitNvr(void* PtrHIKNvr,char* ip, char* port, char* username, char* password, int channel){
    auto* HIKNvrObj = (HIKNvr*)PtrHIKNvr;
    return HIKNvrObj->InitNvr(ip,port,username,password,channel);
}

char* CheckTimeRegionWithMonth(void* PtrHIKNvr,int year,int month){
    auto* HIKNvrObj = (HIKNvr*)PtrHIKNvr;

    DateTime select_time;
    select_time.year() = year;
    select_time.month() = month;
    std::string res;

    //check the time region with month
    int nRet = 0;
    std::vector<int> available_date_vec;
    nRet = HIKNvrObj->CheckTimeRegionWithMonth(select_time, available_date_vec);

    if (nRet == 0)
    {
        res = HIKNvrObj->TimeToJson(select_time, &available_date_vec).dump();
    }
    char* cString = new char[res.size() + 1];
    std::strcpy(cString,res.c_str());
    return cString;
}

char* CheckTimeRegionWithDay(void* PtrHIKNvr,int year,int month,int day){
    auto* HIKNvrObj = (HIKNvr*)PtrHIKNvr;
    DateTime select_time;
    select_time.year() = year;
    select_time.month() = month;
    select_time.day() = day;
    std::string res;
    std::vector<TimeRecord> available_time_vec;
    int nRet = HIKNvrObj->CheckTimeRegionWithDay(select_time, available_time_vec);
    if (nRet == 0)
    {
         res = HIKNvrObj->TimeToJsonInDay(select_time, &available_time_vec).dump();
    }
    char* cString = new char[res.size() + 1];
    std::strcpy(cString,res.c_str());
    return cString;
}