<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

class EPayNotifyHandler
{
  public function __construct() {
    $this->dbwWes = new DBWorkWebEpaystate();
  }

  protected const STATE_SAME = 0;
  protected const STATE_NEW = 1;
  protected const STATE_CHANGED = 2;

  protected $dbwWes;
  
  public function verarbeiteNotifyRequest($payonline, $requestHandler, $checkState = true) {
    if($requestHandler->validate()) {
      return $this->verarbeiteNotify(
        $payonline,
        $requestHandler->getEPayInfo(),
        $requestHandler->getEmailxml(),
        $requestHandler->getEmailxmlKurse(),
        $checkState);
    } else {
      return false;
    }
  }

  public function verarbeiteNotify($payonline, EPayInfo $epayInfo, $emailxml, $emailxmlKurse, $checkState = true) {
    $this->dbwWes->clearOldEntries();
    $tid = $epayInfo->tid;
    if($checkState) {
      $stateChange = $this->hasPaymentStateChanged($epayInfo);
      if($stateChange == self::STATE_SAME) {
        EpayLogger::logg('E-Pay Status hat sich nicht verändert.', 80);
        return true;
      }
    } else {
      EpayLogger::logg('E-Pay Status hat sich verändert.', 80);
      $stateChange = self::STATE_CHANGED;
    }

    $this->changePaymentStateWebDb($epayInfo, $stateChange); 
    $success = $this->uebermittlePaymentState($payonline, $epayInfo, $emailxml, $emailxmlKurse);

    if(!$success && $stateChange == self::STATE_NEW) {
      EpayLogger::logg("Neues E-Pay-Notify konnte nicht übermittelt werden.", 600);
      return false;
    } elseif(!$success && $stateChange == self::STATE_CHANGED) {
      EpayLogger::logg("E-Pay-Notify konnte nicht übermittelt werden.", 600);
      $this->dbwWes->incrementAttempts($tid);
      if($this->dbwWes->getEpaystateByTransactionId($tid)->attempts == EPAY_NOTIFY_INFORM_ATTEMPT_NUMBER) {
        
        
      }
      return false;
    } 
    return true;
  }

  public function uebermittlePaymentState($payonline, $epayInfo, $emailxml, $emailxmlKurse) {
    try {
      $notifier = EPayNotifySingletonFactory::ermittleNotifier();
      
      
      SessionOperations::setVarValueAnm('epay_schnittstelle', ePaymentApi::getPayonlineProviderByKey($payonline));

      $erfolgreich = $notifier->doNotify($emailxml, $epayInfo, $emailxmlKurse, '.epaynotifyxml', $payonline);
    }
    catch(Exception $e) {
      EpayLogger::logg('Fehler beim Übermitteln des E-Pay Notifys: '.$e->getMessage(), 1100);
      return false;
    }
    return $erfolgreich;
  }

  protected function changePaymentStateWebDb(EPayInfo $epayInfo, $stateChange) {
    if($stateChange == self::STATE_CHANGED) {
      $this->dbwWes->updateState($epayInfo->tid, strtolower($epayInfo->paymentState));
    }
    if($stateChange == self::STATE_NEW) {
      $newEntry = new WebEpaystate($epayInfo->tid, $epayInfo->paymentState);
      $this->dbwWes->insertWebEpaystate($newEntry, SessionOperations::getUserID());
    }
  }

  protected function hasPaymentStateChanged(EPayInfo $epayInfo) {
    $webEpayState = $this->dbwWes->getEpaystateByTransactionId($epayInfo->tid);
    if(empty($webEpayState->tid) && empty($webEpayState->payment_state)) {
      EpayLogger::logg('Neuer E-Pay-Status muss angelegt werden', 50);
      return self::STATE_NEW;
    } 
    
    if(strtolower($webEpayState->payment_state) == strtolower($epayInfo->paymentState)) {
      EpayLogger::logg('E-Pay-Status muss nicht verändert werden', 50);
      return self::STATE_SAME;
    } else {
      EpayLogger::logg('E-Pay-Status muss verändert werden', 50);
      return self::STATE_CHANGED;
    }
  }

  
}