<?php

namespace Inoby;

use Inoby_Config;
use WPBS_Booking;

require_once __DIR__ . "/class-inoby-wpbs-outputter.php";

class WPBS_Informations_Outputter extends WPBS_Outputter {
  private WPBS_Booking $booking;
  private array $informations;

  public function __construct(WPBS_Booking $booking) {
    $this->booking = $booking;
    $this->informations = $booking->to_array();
  }

  private function get_config_defaults(): array {
    $defaults = [
      "status" => false,
      "date_created" => false,
      "date_modified" => false,
      "start_date" => true,
      "end_date" => true,
      "form_fields" => [],
    ];

    if (Inoby_Config::wpbs_extend() ?? false) {
      $defaults["form_fields"][] = "capacity";
      $defaults["form_fields"][] = "dates";
    }

    return $defaults;
  }

  public function table_body(): void {
    $config_informations =
      isset(\Inoby_Config::wpbs_order_details()["informations"]) && is_array(\Inoby_Config::wpbs_order_details()["informations"])
        ? \Inoby_Config::wpbs_order_details()["informations"]
        : $this->get_config_defaults();

    foreach ($this->informations as $information => $value) {
      if (is_array($value) || empty($value)) {
        continue;
      }

      if (!isset($config_informations[$information]) || !$config_informations[$information]) {
        continue;
      }

      $td_label = "";
      $td_value = "";

      switch ($information) {
        case "start_date":
          $td_label = __("Začiatok pobytu", "inoby");
          $td_value = $this->format_date($value);
          break;

        case "end_date":
          $td_label = __("Koniec pobytu", "inoby");
          $td_value = $this->format_date($value);
          break;

        case "date_created":
          $td_label = __("Dátum vytvorenia", "inoby");
          $td_value = $this->format_date($value);
          break;

        case "date_modified":
          $td_label = __("Dátum úpravy", "inoby");
          $td_value = $this->format_date($value);
          break;

        case "status":
          $td_label = __("Status", "inoby");
          $td_value = $value;
          break;

        default:
          $td_label = $information;
          $td_value = $value;
          break;
      }

      $this->print_row($td_label, $td_value);
    }

    foreach ($this->informations["fields"] as $field) {
      if (!isset($field["user_value"]) || empty($field["user_value"])) {
        continue;
      }

      if (!isset($field["id"]) || (!in_array($field["id"], $config_informations["form_fields"]) && !in_array($field["type"], $config_informations["form_fields"]))) {
        continue;
      }

      $td_label = $this->get_translated_label($field);
      $td_value = wpbs_get_field_display_user_value($field["user_value"]);

      $this->print_row($td_label, $td_value);
    }
  }
}
