<?php

class Inoby_Product
{
  static function sale_badge_percentage()
  {
    global $product;
    if (!$product->is_on_sale()) {
      return;
    }
    $max_percentage = 0;
    if ($product->is_type("simple")) {
      $max_percentage = (($product->get_regular_price() - $product->get_sale_price()) / $product->get_regular_price()) * 100;
    } elseif ($product->is_type("variable")) {
      foreach ($product->get_children() as $child_id) {
        $variation = wc_get_product($child_id);
        $price = $variation->get_regular_price();
        $sale = $variation->get_sale_price();
        $percentage = 0;
        if ($price != 0 && !empty($sale)) {
          $percentage = (($price - $sale) / $price) * 100;
        }
        if ($percentage > $max_percentage) {
          $max_percentage = $percentage;
        }
      }
    }
    if ($max_percentage > 0) {
      echo "<span class='tag on-sale'>-" . round($max_percentage) . "%</span>";
    }
  }

  static function new_product_badge()
  {
    global $product;
    $newness_days = 30;
    $created = strtotime($product->get_date_created());

    if ($product->is_on_sale() || time() - 60 * 60 * 24 * $newness_days < $created) {
      if (time() - 60 * 60 * 24 * $newness_days < $created) {
        echo '<span class="tag new-product">' . esc_html__("Novinka", "inoby") . "</span>";
      }
    }
  }

  static function product_special_badge()
  {
    global $product;
    $taxonomy = "product_specials";
    $terms = get_the_terms($product->get_id(), $taxonomy);

    if (!$terms) {
      return;
    }

    foreach ($terms as $term) {
      echo '<span class="tag special ' . $term->slug . '">' . esc_html__($term->name, "inoby") . "</span>";
    }
  }

  static function product_color_variations()
  {
    global $product;
    echo '<div class="colors-wrap">';
    $farba_id = wp_get_post_terms($product->get_id(), "pa_farba", true);
    if (!empty($farba_id) && is_array($farba_id)):
      $item_id = array_slice($farba_id, 0, 7);
      foreach ($item_id as $id):
        $farby_img = rwmb_meta("rc_attribute_term_color", ["object_type" => "term", "size" => "thumbnail"], $id->term_id);
        echo '<span class="color-wheel" style="background:' . $farby_img . '"></span>';
      endforeach;
    endif;
    echo "</div>";
  }

  static function get_manufacturer_name()
  {
    global $product;
    $attribute_slug = "vyrobca";
    $manufacturer = $product->get_attribute($attribute_slug);
    if (!empty($attribute_slug)) {
      echo '<div class="manufacturer-wrap">';
      echo '  <p class="manufacturer-name">' . $manufacturer . "</p>";
      echo "</div>";
    }
  }

  /**
   * Get HTML for a gallery image.
   *
   * @param int  $attachment_id Attachment ID.
   * @param string $image_size Image size
   * @param array $attrs Image wrapper html attributes
   * @param boolean $main Is main image
   * @return string
   */
  static function get_gallery_image_html($attachment_id, $image_size = "thumbnail", $attrs = null, $main = false)
  {
    $thumbnail_size = apply_filters("inoby_gallery_thumbnail_size", "thumbnail");
    $full_size = apply_filters("inoby_gallery_full_size", "full");
    $thumbnail_src = wp_get_attachment_image_src($attachment_id, $thumbnail_size);
    $full_src = wp_get_attachment_image_src($attachment_id, $full_size);
    $alt_text = trim(wp_strip_all_tags(get_post_meta($attachment_id, "_wp_attachment_image_alt", true)));
    $image = wp_get_attachment_image(
      $attachment_id,
      $image_size,
      false,
      apply_filters(
        "inoby_gallery_image_html_attachment_image_params",
        [
          "title" => _wp_specialchars(get_post_field("post_title", $attachment_id), ENT_QUOTES, "UTF-8", true),
          "data-caption" => _wp_specialchars(get_post_field("post_excerpt", $attachment_id), ENT_QUOTES, "UTF-8", true),
          "data-src" => esc_url($full_src[0]),
          "data-large_image" => esc_url($full_src[0]),
          "data-large_image_width" => esc_attr($full_src[1]),
          "data-large_image_height" => esc_attr($full_src[2]),
          "class" => esc_attr($main ? "wp-post-image" : ""),
        ],
        $attachment_id,
        $image_size,
        $main
      )
    );

    $default_attrs = [
      "class" => "woocommerce-product-gallery__image",
      "data-thumb" => esc_url($thumbnail_src[0]),
      "data-thumb-alt" => esc_attr($alt_text),
    ];

    $attrs = wp_parse_args($attrs, $default_attrs);

    // prettier-ignore
    return '<div ' . array_to_html_attrs($attrs) . '><a href="' . esc_url($full_src[0]) . '">' . $image . '</a></div>';
  }

  static function wishlist()
  {
    global $product;
    // Získa aktuálneho používateľa
    $current_user = wp_get_current_user();
    // Získa zoznam obľúbených produktov pre aktuálneho používateľa
    $wishlist = get_user_meta($current_user->ID, 'wishlist', true);
    if (!is_array($wishlist)) {
      $wishlist = array(); // Ak nie je pole, inicializujte ho ako prázdne pole
    }
    // Kontrola, či je produkt už v zozname obľúbených
    echo '<div style="position: absolute; right: 0;" class="wishlist">';
    if (in_array($product->get_id(), $wishlist)) {
      echo '<div style="display: block;" data-product-id="' . $product->get_id() . '" class="remove-from-wishlist"><img style="max-width:30px;padding:0;" src="' . get_theme_file_uri("/assets/icons/heart-filled.svg") . '" alt="' . __("Wishlist", "inoby") . '"></div>';
      echo '<div style="display: none;" data-product-id="' . $product->get_id() . '" class="add-to-wishlist"><img style="max-width:30px;padding:0;" src="' . get_theme_file_uri("/assets/icons/heart-not-filled.svg") . '" alt="' . __("Wishlist", "inoby") . '"></div>';
    } else {
      echo '<div style="display: none;" data-product-id="' . $product->get_id() . '" class="remove-from-wishlist"><img style="max-width:30px;padding:0;" src="' . get_theme_file_uri("/assets/icons/heart-filled.svg") . '" alt="' . __("Wishlist", "inoby") . '"></div>';
      echo '<div style="display: block;" data-product-id="' . $product->get_id() . '" class="add-to-wishlist"><img style="max-width:30px;padding:0;" src="' . get_theme_file_uri("/assets/icons/heart-not-filled.svg") . '" alt="' . __("Wishlist", "inoby") . '"></div>';
    }
    echo '</div>';
  }
}
