<?php
defined('ABSPATH') || exit('No Access!');

global $product, $options, $labels;
$product_attributes = $product->get_attributes();

$group_attributes = get_posts(
    [
        'post_type' => 'group_attribute',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby' => 'meta_value_num',
        'meta_key' => 'irk_group_attribute_position',
        'order' => 'ASC',
    ]
);

$temp = [];

if (is_array($group_attributes) && count($group_attributes)) {
    foreach ($group_attributes as $group_attribute) {
        $attribute_group_name = $group_attribute->post_title;
        $attributes_in_group = get_post_meta($group_attribute->ID, 'irk_group_attributes', true);
        $icon = get_post_meta($group_attribute->ID, 'irk_group_attribute_icon', true);
        $icon_html = irk_get_icon($icon);

        if (is_array($attributes_in_group) && count($attributes_in_group)) {
            foreach ($attributes_in_group as $attribute_in_group) {
                if (is_array($product_attributes) && count($product_attributes)) {
                    foreach ($product_attributes as $product_attribute) {
                        $attribute_data = $product_attribute->get_data();
                        if ($attribute_data['is_visible'] == 0) {
                            continue;
                        }

                        if ($attribute_data['id'] == $attribute_in_group) {
                            $temp[$attribute_group_name]['name'] = $attribute_group_name;
                            $temp[$attribute_group_name]['icon'] = $icon_html;
                            $temp[$attribute_group_name]['attributes'][] = $product_attribute;
                        }
                    }
                }
            }
        }
    }
}

if (count($temp)) {
    echo '<table class="woocommerce-product-attributes shop_attributes group-attributes">';
    foreach ($temp as $item) {
        echo '<tr class="attribute-group-row">';
        echo '<th colspan="2">' . $item['icon'] . '<span class="group-title">' . $item['name'] . '</span>' . '</th>';
        echo '</tr>';

        $_product_attributes = [];
        $attributes = $item['attributes'];

        if (is_array($attributes) && count($attributes)) {
            foreach ($attributes as $attribute) {
                $values = [];

                if ($attribute->is_taxonomy()) {
                    $attribute_taxonomy = $attribute->get_taxonomy_object();
                    $attribute_values = wc_get_product_terms($product->get_id(), $attribute->get_name(), array('fields' => 'all'));

                    foreach ($attribute_values as $attribute_value) {
                        $value_name = esc_html($attribute_value->name);

                        if ($attribute_taxonomy->attribute_public) {
                            $values[] = '<a href="' . esc_url(get_term_link($attribute_value->term_id, $attribute->get_name())) . '" rel="tag">' . $value_name . '</a>';
                        } else {
                            $values[] = $value_name;
                        }
                    }
                } else {
                    $values = $attribute->get_options();

                    foreach ($values as &$value) {
                        $value = make_clickable(esc_html($value));
                    }
                }

                $_product_attributes['attribute_' . sanitize_title_with_dashes($attribute->get_name())] = [
                    'label' => wc_attribute_label($attribute->get_name()),
                    'value' => apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values),
                ];
            }

            foreach ($_product_attributes as $product_attribute_key => $product_attribute) {
                echo '<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--' . esc_attr($product_attribute_key) . '">';
                echo '<th class="woocommerce-product-attributes-item__label">' . wp_kses_post($product_attribute['label']) . '</th>';
                echo '<td class="woocommerce-product-attributes-item__value">' . wp_kses_post($product_attribute['value']) . '</td>';
                echo '</tr>';
            }
        }
    }

    if (!$options->get_option('hide-dimensions')) {
        $dimensions_icon = true ? 'package-variant-closed' : IRK_IMG_URI . 'package.png';
        echo '<tr class="attribute-group-row attribute-group-row-dimensions">';
        echo '<th colspan="2">' . irk_get_icon($dimensions_icon) . '<span class="group-title">' . $labels->get_label('more-info') . '</span>' . '</th>';
        echo '</tr>';

        echo '<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--weight">';
        echo '<th class="woocommerce-product-attributes-item__label">' . __('Weight', 'woocommerce') . '</th>';
        echo '<td class="woocommerce-product-attributes-item__value">' . wc_format_weight($product->get_weight()) . '</td>';
        echo '</tr>';

        echo '<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--dimensions">';
        echo '<th class="woocommerce-product-attributes-item__label">' . __('Dimensions', 'woocommerce') . '</th>';
        echo '<td class="woocommerce-product-attributes-item__value">' . wc_format_dimensions($product->get_dimensions(false)) . '</td>';
        echo '</tr>';
    }

    echo '</table>';
}