<?php

class CodeGenHelper
{
  public static $cg;

  public function __construct()
  {
  }

  public static function getInstance()
  {
    if (!empty(self::$cg)) return self::$cg;
    self::$cg = new CodeGenHelper();
    return self::$cg;
  }

  public static function generatePassword($hlNum, $lan, $type, $version = 64)
  {
    $country = substr($hlNum, 0, 2);
    $number = substr($hlNum, 2, 4);
    $type = (int)substr($type, 0, 1);

    $command = PATH_CODEGEN . " -Password " . $country . $number . $version . $lan . $type;
    $ph = popen($command, "r") or die("A 'codegen' paranccsal nem veheto fel kapcsolat");
    $password = fgets($ph, 1024);
    pclose($ph);
    return ($password);
  }

  public static function generateTimeCode($pass, $isid, $numOfDays)
  {
    $command = PATH_CODEGEN . " -GenerateTimeCode " . $pass . " " . $isid . " " . $numOfDays;
    $ph = popen($command, "r") or die("A 'codegen' paranccsal nem veheto fel kapcsolat");
    $aktkod = fgets($ph, 1024);
    pclose($ph);
    return ($aktkod);
  }

  public static function generateTimeCodeForSoftwareKey($pass, $isid, $numOfDays)
  {
    $ret = generateTimeCode($pass, $isid, $numOfDays);
    return ($ret);
  }

  public static function generateTimeCodeForHardwareKey($pass, $numOfDays)
  {
    $isid = substr($pass, 0, 2) . substr($pass, 7, 2);
    $ret = generateTimeCode($pass, $isid, $numOfDays);
    return ($ret);
  }

  public static function isSoftwareKey($pass)
  {
    $command = PATH_CODEGEN . " -IsSoftwareKey " . $pass;
    $ph = popen($command, "r") or die("A 'codegen' paranccsal nem veheto fel kapcsolat");
    $ret = fgets($ph, 1024);
    pclose($ph);
    return ($ret == "true" ? TRUE : FALSE);
  }

  public static function parseTimeCode($idokod)
  {
    $command = PATH_CODEGEN . " -ParseTimeCode " . $idokod;
    $ph = popen($command, "r");
    $elteltnap = (int)fgets($ph, 1024);
    pclose($ph);
    return ($elteltnap);
  }

  public static function diffDay($dateFrom, $dateTo)
  {
    $command = PATH_CODEGEN . " -DiffDay " . $dateFrom . " " . $dateTo;
    $ph = popen($command, "r") or die("A 'codegen' paranccsal nem veheto fel kapcsolat");
    $napok = fgets($ph, 1024);
    pclose($ph);
    return ($napok);
  }
}
