<?php
include_once "/var/www/html/Identy-Website/realtime/models/attemptsmaster.php";
include_once "/var/www/html/Identy-Website/realtime/models/logprogressv2.php";
class DBGeneral {

  public static function InsertLogs($resultarray,$tablename) {
      if(!empty($resultarray)){

        foreach($resultarray as $fa){
          if(!empty($fa->accessinfo)){
            $smaster = new statisticsmaster();
            $smaster->logid = $fa->logid;
            $smaster->licenseid = $fa->licenseid;
            $smaster->hash = $fa->hash;
            if(empty($fa->transactionid)){
              print_r("META DATA EMPTY......".PHP_EOL);
              print_r($smaster->transactionid." Old Id......".PHP_EOL);
              $smaster->transactionid = $fa->type.$fa->accesstimestamp.$fa->uniqueid;
              print_r($smaster->transactionid." New Id......".PHP_EOL);
              readline();

              
            }
            else{
              $smaster->transactionid = $fa->transactionid;
            }
            $smaster->accessinfo = $fa->accessinfo;
            $smaster->noofattempts = $fa->noofattempts;
            $smaster->package = $fa->package;
            $smaster->mobilemodel = $fa->mobilemodel;
            $smaster->mobilemanufacturer = $fa->mobilemanufacturer;
            $smaster->uniqueid = $fa->uniqueid;
            $smaster->sdkversion = $fa->sdkversion;
            $smaster->type = $fa->type;
            $smaster->accessinfo = $fa->accessinfo;
            $smaster->accesstimestamp = $fa->accesstimestamp;
            $smaster->meta = $fa->metadata;
            $smaster->metarawdata = $fa->metarawdata;
            $smaster->validity = $fa->validity;
            $smaster->dbreccreatedat = $fa->dbreccreatedat;
            if(isset($fa->description)){
              $smaster->description = $fa->description;
            }
            //print_r($smaster);
            $smaster->save("INSERT",0,$tablename);
          }
          }

        }
  }

  public static function InsertLogsReturnWithId($resultarray,$tablename) {
      if(!empty($resultarray)){

        foreach($resultarray as $fa){
          //print_r(PHP_EOL."**************************************".PHP_EOL);
          //print_r($fa);
          //print_r(PHP_EOL."**************************************".PHP_EOL);
         // readline();
          if(!empty($fa->accessinfo)){

            $smaster = new statisticsmaster();
            $smaster->logid = $fa->logid;
            $smaster->licenseid = $fa->licenseid;
            $smaster->hash = $fa->hash;
            if(strcmp($fa->validity,"PASS")==0){
              $smaster->transactionid = $fa->transactionid;
            }
            if(strcmp($fa->validity,"PASS|SPOOF")==0){
              $smaster->transactionid = $fa->transactionid;
            }
            $smaster->accessinfo = $fa->accessinfo;
            $smaster->noofattempts = $fa->noofattempts;
            $smaster->package = $fa->package;
            $smaster->mobilemodel = $fa->mobilemodel;
            $smaster->mobilemanufacturer = $fa->mobilemanufacturer;
            $smaster->uniqueid = $fa->uniqueid;
            $smaster->sdkversion = $fa->sdkversion;
            $smaster->type = $fa->type;
            $smaster->accessinfo = $fa->accessinfo;
            $smaster->accesstimestamp = $fa->accesstimestamp;
            $smaster->meta = $fa->metadata;
            $smaster->metarawdata = $fa->metarawdata;
            $smaster->validity = $fa->validity;
            $smaster->dbreccreatedat = $fa->dbreccreatedat;
            $processedlogid = $smaster->save("INSERT",0,$tablename);
            $modality = strtoupper($fa->type);
            if($modality=='FACE'){
              if(isset($fa->rf->visualreport)){
                if($fa->rf->visualreport=='Y'){
                  $tablename = "processedlog_".$fa->licenseid."_attempts";
                  $index=0;
                  $lpv2 = new logprogressv2();
                  $lpv2->createProccessedLogAttemptTable($fa->licenseid);
                  foreach($fa->rf->attemptinfo as $ati){
                    $index = $index+1;
                    $am = new attemptsmaster();
                    $am->processed_log_id=$processedlogid;
                    $am->attemptindex = $index;
                    $am->meta= $fa->metadata;
                    if(isset($ati->framemeta)){$am->framemeta = $ati->framemeta;}
                    if(isset($ati->startTime)){$am->starttime = $ati->startTime;}
                    if(isset($ati->endTime)){$am->endtime = $ati->endTime;}
                    if(isset($ati->timeTaken)){$am->timetaken = $ati->timeTaken;}
                    if(isset($ati->totalframes)){$am->total_frames = $ati->totalframes;}
                    if(isset($ati->passedframes)){$am->passed_frames = $ati->passedframes;}
                    if(isset($ati->isSpoof)){$am->is_spoof = $ati->isSpoof;}
                    if(isset($ati->spoof_score)){$am->spoof_score = $ati->spoof_score;}
                    if(isset($ati->layer_one_score)){$am->layer_one_score = $ati->layer_one_score;}
                    if(isset($ati->layer_one_flip_score)){$am->layer_one_flip_score = $ati->layer_one_flip_score;}
                    if(isset($ati->layer_two_score)){$am->layer_two_score = $ati->layer_two_score;}
                    if(isset($ati->layer_two_flip_score)){$am->layer_two_flip_score = $ati->layer_two_flip_score;}
                    if(isset($ati->layer_three_score)){$am->layer_three_score = $ati->layer_three_score;}
                    if(isset($ati->layer_three_flip_score)){$am->layer_three_flip_score = $ati->layer_three_flip_score;}
                    if(isset($ati->layer_four)){$am->layer_four = $ati->layer_four;}
                    if(isset($ati->layer_five)){$am->layer_five = $ati->layer_five;}
                    if(isset($ati->layer_six)){$am->layer_six = $ati->layer_six;}
                    if(isset($ati->layer_zero)){$am->layer_zero = $ati->layer_zero;}
                    if(isset($ati->spoof_processed_frames)){$am->spoof_processed_frames = $ati->spoof_processed_frames;}
                    if(isset($ati->spoof_passed_frames)){$am->spoof_passed_frames = $ati->spoof_passed_frames;}
                    if(isset($ati->matching)){$am->matching = 'N';}
                    if(isset($ati->matching_score)){$am->matching_score = $ati->matching_score;}
                    if(isset($ati->eyeopeningscore)){$am->eyeopeningscore = $ati->eyeopeningscore;}
                    if(isset($ati->error)){$am->error = $ati->error;}
                    if(isset($ati->captured)){$am->captured = $ati->captured;}
                    if(isset($ati->layer_seven_score)){$am->layer_seven_score = $ati->layer_seven_score;}
                    if(isset($ati->layer_eight_score)){$am->layer_eight_score = $ati->layer_eight_score;}
                    if(isset($ati->layer_nine)){$am->layer_nine = $ati->layer_nine;}
                    if(isset($ati->layer_ten)){$am->layer_ten = $ati->layer_ten;}
                    $am->save("INSERT",0,$tablename);
                    print_r($am);
                    //readline();
                  }
                
                }
              }
            }
          }
          }

        }
  }

   public static function InsertLogsFromArray($log,$tablename) {
      $accessinfoarray = explode(",",$log['licenseaccessinfo']);
      $accesstimestampsarray =  explode(",",$log['accesstimestamps']);
      $cnt=0;
      foreach($accessinfoarray as $accessinfo){

          $smaster = new statisticsmaster();

          if(strtoupper(trim($accessinfo))=="ERROR"){
            $smaster->validity = "FAIL";
          }
          else if(strtoupper(trim($accessinfo))=="LVERIFY"){
            $smaster->validity = "LVERIFY";
          }
          else{
            $smaster->validity = "PASS";
          }
          
          $smaster->logid = $log['id'];
          $smaster->licenseid = $log['licenseid'];
          $smaster->hash = $log['hash'];
          if((empty($log['metadata'])) && ($accessinfo<>'LVERIFY')){
              print_r("META DATA EMPTY......".PHP_EOL);
              print_r($smaster->transactionid." Old Id......".PHP_EOL);
              $smaster->transactionid = $log['licensefor'].$accesstimestampsarray[$cnt].$log['uniqueid'];
              print_r($smaster->transactionid." New Id......".PHP_EOL);
              readline();

              
          }
            else{
              $smaster->transactionid = 0;
          }
          $smaster->accessinfo = $accessinfo;
          if($smaster->validity == "PASS"){
            $smaster->noofattempts = 1;
          }
          else{
            $smaster->noofattempts = 0;
          }
          $smaster->package =  $log['package'];
          $smaster->mobilemodel = $log['mobilemodel'];
          $smaster->mobilemanufacturer = $log['mobilemanufacturer'];
          $smaster->uniqueid = $log['uniqueid'];
          $smaster->sdkversion = $log['sdkversion'];
          $smaster->type = "";
          $smaster->accesstimestamp = $accesstimestampsarray[$cnt];
          $smaster->meta = "";
          $smaster->metarawdata = "";
          
          $smaster->dbreccreatedat = $log['created_at'];
         // print_r($smaster);
          $smaster->save("INSERT",0,$tablename);
          $cnt=$cnt+1;
      }
  }

 public static function UpdateValidityToPass($progressid,$tablename) {

      $smaster = new statisticsmaster();
      $smaster->validity="PASS";
      $smaster->save("UPDATE",$progressid,$tablename);

  }

 public static function UpdateProcessedMeta($progressid,$tablename,$processedmeta) {

      $smaster = new statisticsmaster();
      $smaster->processedmeta=$processedmeta;
      $smaster->save("UPDATE",$progressid,$tablename);

  }

 public static function UpdateValidity($progressid,$tablename,$validity) {

      $smaster = new statisticsmaster();
      $smaster->validity=$validity;
      $smaster->save("UPDATE",$progressid,$tablename);

  }
}
?>