<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */


abstract class Condition
{
  
  public $value1;
  
  public $value2;
  
  public $operator;

  public function __construct()
  {
    $this->value1 = '';
    $this->value2 = '';
  }

  
  abstract public function evaluate(\Kufer\Web\Classes\scripting\ExpressionEvaluator $exportAdapter);
}



class SimpleCondition extends Condition
{
  public function evaluate(\Kufer\Web\Classes\scripting\ExpressionEvaluator $exportAdapter)
  {
    
    $value1 = $exportAdapter->fillVarsByVariableList(
      $this->value1,
      $exportAdapter->returnVarNames($this->value1)
    );
    $value2 = $exportAdapter->fillVarsByVariableList(
      $this->value2,
      $exportAdapter->returnVarNames($this->value2)
    );

    $numeric = false;
    $matches = array();
    
    if (preg_match('/\\d+(,\\d*)?/', trim($value1), $matches) > 0 && $matches[0] == trim($value1)
       && preg_match('/\\d+(,\\d*)?/', trim($value2), $matches) > 0 && $matches[0] == trim($value2)) {
      $numeric = true;
      
      $value1 = str_replace(',', '.', trim($value1));
      $value2 = str_replace(',', '.', trim($value2));
    }
    $condition = false;
    switch ($this->operator) {
      case '==':
        
        if ($numeric) {
          $condition = $value1 == $value2;
        } else {
          $condition = strcmp(StringOperations::html_entity_decode($value1), StringOperations::html_entity_decode($value2)) == 0;
        }
        break;
      case '!=':
        
        if ($numeric) {
          $condition = $value1 != $value2;
        } else {
          $condition = (strcmp($value1, $value2) != 0);
        }
        break;
      case '<':
        $condition = ($value1 < $value2);
        break;
      case '<=':
        $condition = ($value1 <= $value2);
        break;
      case '>':
        $condition = ($value1 > $value2);
        break;
      case '>=':
        $condition = ($value1 >= $value2);
        break;
      case 'vor':

        $condition = DateTimeOperations::isDateBefore($value1, $value2);
        break;
      case 'contains':
        $condition = StringOperations::strstr(StringOperations::html_entity_decode($value1), StringOperations::html_entity_decode($value2)) != false;
        break;
      case 'matches':
        $matches = array();
        $value2ToCompare = StringOperations::html_entity_decode($value2);
        $match = preg_match("$value1", $value2ToCompare, $matches);
        if ($match === false) {
          Logger::logg('Fehler beim Pattern Matching: ' . $value1 . ' matches ' . $value2 . ' / ' . $value2ToCompare, PARSER_ERROR_SEVERITY_LOG);
          die(PARSER_ERROR_MESSAGE);
        }
        $condition = $match > 0 && $matches[0] === $value2ToCompare;
    }

    return $condition;
  }
}



class ANDORCondition extends Condition
{
  public function __construct()
  {
    parent::__construct();
  }

  public function evaluate(\Kufer\Web\Classes\scripting\ExpressionEvaluator $exportAdapter)
  {
    $condition = false;
    switch ($this->operator) {
      case 'AND':
        $condition = ($this->value1->evaluate($exportAdapter)) && ($this->value2->evaluate($exportAdapter));
        break;
      case 'OR':
        $condition = ($this->value1->evaluate($exportAdapter)) || ($this->value2->evaluate($exportAdapter));
        break;
    }

    return $condition;
  }
}