<?php

namespace Inoby\WPBS;

// Exit if accessed directly
if (!defined("ABSPATH")) {
  exit();
}

class Form_Outputter extends \WPBS_Form_Outputter {
  protected $form_data = null;

  public function __construct($form, $args = [], $form_fields = [], $calendar_id = null) {
    // call super constructior
    parent::__construct($form, $args, $form_fields, $calendar_id);

    // parse form data from request
    parse_str($_POST["form_data"] ?? null, $this->form_data);
  }

  public function hook_custom_field_outputers($custom_field_types) {
    foreach ($this->form->get("fields") as $field) {
      if (in_array($field["type"], $custom_field_types)) {
        add_filter("wpbs_form_outputter_form_field_" . $this->form->get("id") . "_" . $field["id"] . "_before", [$this, "get_form_field_type__" . $field["type"]], 10, 2);
      }
    }
  }

  /**
   * Constructs and returns the HTML the CAPACITY field
   *
   * @return string
   *
   */
  public function get_form_field_type__capacity() {
    $field = $this->get_current_field();

    // sanitize field
    $this->sanitize_custom_field($field);

    $output = $this->get_form_field_header($field);
    // prettier-ignore
    $output .=
      '<input 
        type="number"
        value="' . $this->get_form_field_value($field) . '"
        name="wpbs-input-' . $this->form->get("id") . "-" . $field["id"] . '"
        id="wpbs-form-field-input-' . $this->form->get("id") . "-" . $field["id"] . "-" . $this->unique . '"
        placeholder="' . esc_attr($this->get_form_field_translation($field["values"], "placeholder")) . '"
      />';

    $output .= $this->get_form_field_footer($field);

    return $output;
  }

  /**
   * Constructs and returns the HTML the date_selection field
   *
   * @return string
   *
   */
  public function get_form_field_type__date_selection() {
    $field = $this->get_current_field();
    $this->sanitize_custom_field($field);

    // turn on display of date selection and backup original value
    $show_date_selection = $this->args["show_date_selection"] ?? 0;
    $this->args["show_date_selection"] = 1;

    $output = $this->get_form_field_header($field);
    $output .= $this->get_selected_dates();
    $output .= $this->get_form_field_footer($field);

    // restore original value
    $this->args["show_date_selection"] = $show_date_selection;
    return $output;
  }

  public function get_form_field_type__meta_price() {
    $field = $this->get_current_field();

    $price = floatval($this->get_form_field_translation($field["values"], "pricing"));

    $addition = $this->get_form_field_translation($field["values"], "pricing_type");

    $output = $this->get_form_field_header($field);
    $output .=
      '<input
        type="hidden"
        name="wpbs-input-' .
      $this->form->get("id") .
      "-" .
      $field["id"] .
      '"
        id="wpbs-form-field-input-' .
      $this->form->get("id") .
      "-" .
      $field["id"] .
      "-" .
      $this->unique .
      '"
        value="' .
      $price .
      "|" .
      $this->get_form_field_translation($field["values"], "label") .
      '"
        data-value="' .
      $price .
      "|" .
      $this->get_form_field_translation($field["values"], "label") .
      '"
        data-price="' .
      $price .
      '"
        data-addition="' .
      $addition .
      '"
        data-display-value="' .
      $this->get_form_field_translation($field["values"], "label") .
      '"
    />';
    $output .= $this->get_form_field_footer($field);

    return $output;
  }

  public function sanitize_custom_field(&$field) {
    if (!$this->form_data) {
      return;
    }
    $value = $this->form_data["wpbs-input-" . $this->form->get("id") . "-" . $field["id"]] ?? "";
    $value = stripslashes($value);
    $value = sanitize_text_field($value);
    $value = esc_attr($value);
    $field["user_value"] = apply_filters("wpbs_form_field_sanitization", $value, $this->form->get("id"), $field["id"]);
  }

  private function get_current_field() {
    $hook_name = current_filter();
    $re = "/.*_([0-9]+)_before.*/m";

    preg_match($re, $hook_name, $matches);

    $id = $matches[1] ?? null;

    foreach ($this->form->get("fields") as $field) {
      if ($field["id"] == $id) {
        return $field;
      }
    }
  }
}
