<?php
namespace Inoby\WPBS;

class FormExtendDatepickerAjax {
  public static function init() {
    add_action("wp_ajax_inoby_wpbs_get_selectable_dates", [__CLASS__, "get_selectable_dates"]);
    add_action("wp_ajax_nopriv_inoby_wpbs_get_selectable_dates", [__CLASS__, "get_selectable_dates"]);
  }

  public static function get_selectable_dates() {
    $response = [
      "success" => false,
    ];

    if (empty($_POST["calendar_id"])) {
      self::send_response($response);
    }

    $calendar_id = absint($_POST["calendar_id"]);
    $calendar = wpbs_get_calendar($calendar_id);

    if (empty($_POST["year"] ?? "") || empty($_POST["month"] ?? "")) {
      self::send_response($response);
    }

    $data_source = self::DataSource($calendar);
    $data = $data_source->get_data($_POST["year"], $_POST["month"]);

    if (!empty($data)) {
      $response["success"] = true;
      $response["data"] = $data;
    }

    self::send_response($response);
  }

  private static function send_response($data) {
    echo json_encode($data);
    wp_die();
  }

  private static function DataSource($calendar) {
    return new class ($calendar) extends \WPBS_Calendar_Outputter {
      public function __construct($calendar) {
        parent::__construct($calendar, []);
      }

      public function get_data($year, $month) {
        $start_day = 1;

        $total_days = (int) date("t", mktime(0, 0, 0, $month, 1, $year));

        $data = [];

        for ($i = $start_day; $i <= $total_days; $i++) {
          $data[$year][$month][$i] = $this->is_bookable($year, $month, $i);
        }

        return $data;
      }

      protected function is_bookable($year, $month, $day) {
        /**
         * Get the custom event data for the current day
         *
         */
        $data = $this->get_data_by_date($year, $month, $day);

        /**
         * Get the event for the current day
         *
         */
        $event = $this->get_event_by_date($year, $month, $day);

        /**
         * Get the event for the current day from the iCal feeds
         *
         */
        $ical_event = $this->get_ical_event_by_date($year, $month, $day);

        if (!is_null($ical_event)) {
          $event = $ical_event;
        }

        /**
         * Get the legend item for the current day
         *
         */
        $legend_item = null;

        if (!is_null($data) && isset($data["legend_item_id"])) {
          foreach ($this->legend_items as $li) {
            if ($data["legend_item_id"] == $li->get("id")) {
              $legend_item = $li;
            }
          }
        } elseif (!is_null($event)) {
          foreach ($this->legend_items as $li) {
            if ($event->get("legend_item_id") == $li->get("id")) {
              $legend_item = $li;
            }
          }
        }

        if (is_null($legend_item)) {
          $legend_item = $this->default_legend_item;
        }

        // Determine if the current day is bookable
        return $this->is_date_bookable($year, $month, $day, $legend_item);
      }
    };
  }
}
