#include "HkStreamer.h"
#include <iostream>
#include <cstring>
#include <map>
#include <getopt.h>
#include <time.h>
#include "cmdlineparser.h"
using namespace std;

void error(string msg)
{
  cerr << msg << endl;
  exit(1);
}
int call_playback(CmdLineParser *parser){
  string user = parser->getOpt("user");
  string password = parser->getOpt("password");
  string device = parser->getOpt("device");
  string channelStr = parser->getOpt("channel");
  unsigned long channel = stol(channelStr);
  string start = parser->getOpt("start");
  string end = parser->getOpt("end");
  LoginInfo loginInfo = {device, user, password};
  StreamCon stream = {channel, 2048, start, end};
  return playback(&loginInfo, &stream);

}
int call_test(CmdLineParser *parser){
  cout<<"Cmd:"<<parser->getOpt("cmd")<<endl;
  cout<<"User:"<<parser->getOpt("user")<<endl;
  cout<<"Password:"<<parser->getOpt("password")<<endl;
  cout<<"Channel:"<<parser->getOpt("channel")<<endl;
  return 0;
}
int main(int argc, char *argv[])
{
  CmdLineParser *cmdParser = new CmdLineParser("hkstreamer", "config and streaming for hk nvr");
  cmdParser->regOpt("cmd",120, required_argument, "sub command, current support: playback");
  cmdParser->regOpt("user",'U', required_argument, "device user");
  cmdParser->regOpt("password", 'P', required_argument, "user password");
  cmdParser->regOpt("device",'D', required_argument, "hk nvr ip");
  cmdParser->regOpt("channel", 'C', required_argument, "channel to retrive video");
  cmdParser->regOpt("start",'S',required_argument, "playback start, iso date str as format YYYY-mm-ddTHH:MM:SSZ");
  cmdParser->regOpt("end",'E', required_argument, "playback end");
  cmdParser->parseArgs(argc, argv);
  string cmd = cmdParser->getOpt("cmd");
  if(cmd == ""){
      error("must give cmd opt");
  }

  if(cmd == "playback"){
      call_playback(cmdParser);
  } else if(cmd == "test"){
      return call_test(cmdParser);
  } else {
      cmdParser->help();
  }

  return 0;
}
