<?php
namespace nulib\schema;

use nulib\ref\schema\ref_analyze;

class ConsolidatedResult extends Result {
  protected int $highestResult;

  function reset(): void {
    parent::reset();
    $this->highestResult = -1;
  }

  protected function _addMessage(Wrapper $wrapper, $prefix=null): void {
    $result = $wrapper->getResult();
    $message = $this->message;
    if ($message) $message .= "\n";
    if ($prefix !== null) $message .= "$prefix: ";
    $message .= $result->message;
    $this->message = $message;
  }

  function addMissingMessage(Wrapper $wrapper, $prefix=null): void {
    if ($this->highestResult < ref_analyze::MISSING) {
      $this->present = false;
      $this->available = false;
      $this->null = false;
      $this->valid = false;
      $this->messageKey = "missing";
    }
    $this->_addMessage($wrapper, $prefix);
  }

  function addUnavailableMessage(Wrapper $wrapper, $prefix=null): void {
    if ($this->highestResult < ref_analyze::UNAVAILABLE) {
      $this->present = true;
      $this->available = false;
      $this->null = false;
      $this->valid = false;
      $this->messageKey = "unavailable";
    }
    $this->_addMessage($wrapper, $prefix);
  }

  function addNullMessage(Wrapper $wrapper, $prefix=null): void {
    if ($this->highestResult < ref_analyze::NULL) {
      $this->present = true;
      $this->available = true;
      $this->null = true;
      $this->valid = false;
      $this->messageKey = "null";
    }
    $this->_addMessage($wrapper, $prefix);
  }

  function addInvalidMessage(Wrapper $wrapper, $prefix=null): void {
    if ($this->highestResult < ref_analyze::INVALID) {
      $this->present = true;
      $this->available = true;
      $this->null = false;
      $this->valid = false;
      $this->messageKey = "invalid";
    }
    $this->_addMessage($wrapper, $prefix);
  }
}
