<?php

namespace Inoby\WPBS;

class Wpbs_Inoby_Booking_Details {
  public function __construct() {
    add_filter("wpbs_booking_modal_tabs", [$this, "add_booking_woo_details_tab"], 10, 2);
    add_filter("wpbs_booking_modal_tab_woo_details", [$this, "woo_details_tab"], 10, 2);
    add_action("add_meta_boxes", [$this, "add_admin_order_booking_detail"], 10);
    add_filter("manage_edit-shop_order_columns", [$this, "wc_order_column"]);
    add_filter("manage_shop_order_posts_custom_column", [$this, "wc_order_column_data"]);

    add_action("woocommerce_email_order_details", [$this, "add_email_booking_details"], 10, 4);
    add_action("woocommerce_order_status_changed", [$this, "update_booking_by_order_state"], 10, 4);
    add_action("wpbs_insert_booking", [$this, "auto_accept_reservation"], 10, 2);
  }

  public function add_booking_woo_details_tab($tabs, $booking) {
    $tabs["woo_details"] =
      '<svg aria-hidden="true" focusable="false" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path fill="currentColor" d="M336 64h-88.6c.4-2.6.6-5.3.6-8 0-30.9-25.1-56-56-56s-56 25.1-56 56c0 2.7.2 5.4.6 8H48C21.5 64 0 85.5 0 112v352c0 26.5 21.5 48 48 48h288c26.5 0 48-21.5 48-48V112c0-26.5-21.5-48-48-48zM192 32c13.3 0 24 10.7 24 24s-10.7 24-24 24-24-10.7-24-24 10.7-24 24-24zm160 432c0 8.8-7.2 16-16 16H48c-8.8 0-16-7.2-16-16V112c0-8.8 7.2-16 16-16h48v20c0 6.6 5.4 12 12 12h168c6.6 0 12-5.4 12-12V96h48c8.8 0 16 7.2 16 16z"></path></svg>' .
      __("Detaily objednávky", "wp-booking-system");

    return $tabs;
  }

  public function woo_details_tab($booking, $calendar) {
    $order = $this->get_order_by_booking($booking);
    if ($order) {
      $link = $order->get_edit_order_url(); ?>
        <h1>Objednávka #<?= $order->get_id() ?></h1>
        <a href="<?= $link ?>" target="blank">Upraviť objednávku</a>
      <?php
      wc_get_template("order/order-details-customer.php", ["order" => $order]);
      woocommerce_order_details_table($order->get_id());

      $payout_link = $order->get_meta("payout_redirect_url");
      if ($payout_link) {
        echo "<h2>Payout link</h2>
            <a href=\"$payout_link\" target=\"blank\">$payout_link</a>";
      }

    }
  }

  private function get_order_by_booking($booking) {
    $payment = wpbs_get_payment_by_booking_id($booking->get("id"));
    if ($payment) {
      $order_id = $payment->get("order_id") ?? false;
      if ($order_id) {
        return wc_get_order($order_id);
      }
    }

    return false;
  }

  function add_admin_order_booking_detail() {
    foreach (wc_get_order_types("order-meta-boxes") as $type) {
      $order_type_object = get_post_type_object($type);
      add_meta_box("wpbs-booking-details", "Rezervácia", "Inoby\WPBS\Wpbs_Inoby_Booking_Details::order_admin_output", $type, "normal", "high");
    }
  }

  function wc_order_column($columns) {
    $columns["wpbs_booking_dates"] = "Dátum rezervácie";
    $columns["wpbs_booking_details"] = "Podrobnosti rezervácie";
    return $columns;
  }

  function wc_order_column_data($column) {
    global $post;

    switch ($column) {
      case "wpbs_booking_dates":
        $booking = wpbs_get_booking_from_order($post->ID);
        if ($booking) {
          echo "od: " . wpbs_booking_start_date_output($booking) . "<br>do: " . wpbs_booking_end_date_output($booking);
        }
        break;
      case "wpbs_booking_details":
        $booking = wpbs_get_booking_from_order($post->ID);
        if ($booking) {
          $details = wpbs_get_booking_details($booking);
          foreach ($details as $item) {
            echo "<div>";
            echo $item["label"];
            echo trim($item["value"]) ? " : " . $item["value"] : "";
            echo "</div>";
          }
        }
        break;
    }
  }

  function update_booking_by_order_state($id, $old_status, $new_status, $order) {
    $booking = wpbs_get_booking_from_order($order);
    if ($booking) {
      $booking_status = "";
      switch ($new_status) {
        case "completed":
          $booking_status = "accepted";
          break;
        case "canceled":
        case "refunded":
        case "failed":
          $booking_status = "trash";
          break;
        default:
          $booking_status = "pending";
          break;
      }
      $old_booking_status = $booking->get("status");

      if ($old_booking_status != $booking_status) {
        $booking_id = $booking->get("id");

        wpbs_add_booking_meta($booking_id, "before_trash_status", $old_booking_status);
        wpbs_update_booking($booking_id, [
          "status" => $booking_status,
        ]);
      }
    }
  }

  function auto_accept_reservation($payment_id, $data) {
    if (isset($data["order_status"]) && isset($data["booking_id"])) {
      $booking_id = $data["booking_id"] ?? false;
      wpbs_update_booking($booking_id, [
        "status" => "accepted",
      ]);
    }
  }

  static function order_admin_output($post) {
    self::output_booking_details($post->ID);
  }

  function add_email_booking_details($order, $sent_to_admin, $plain_text, $email) {
    self::output_booking_details($order);
  }

  static function output_booking_details($order) {
    $booking = wpbs_get_booking_from_order($order);
    if ($booking) {

      $details = [];
      $details[] = ["label" => "Začiatok pobytu", "value" => wpbs_booking_start_date_output($booking)];
      $details[] = ["label" => "Koniec pobytu", "value" => wpbs_booking_end_date_output($booking)];
      $details = array_merge($details, wpbs_get_booking_details($booking));
      ?>
<table class="booking-details">
  <?php foreach ($details as $row): ?>
    <tr><th class="row-heading" style="text-align: left; padding-right: 3rem;"><?= $row["label"] ?></th><td><?= $row["value"] ?></td><tr>
  <?php endforeach; ?>
</table>
<?php
    }
  }
}
