<?php

/**
 * Product Templates and Shortcodes Handler
 * 
 * @package cwpt
 * @since 1.0.0
 */

defined('ABSPATH') or die;

class CWPT_Templates
{
    /**
     * Override single product template
     * 
     * @param string $template
     * @return string
     */
    public static function override_template($template)
    {
        global $post;

        $product = WC()->product_factory->get_product($post->ID);
        if ($product && is_product() && is_array(CWPT_Settings::get('name_ids')) && in_array($product->get_type(), CWPT_Settings::get('name_ids'))) {
        $selected_layout = CWPT_Settings::get('product_layout')[$product->get_type()];
            if (is_singular('product') && $product) {
                if ($selected_layout && $selected_layout !== 'default') {
                    $custom_template = plugin_dir_path(dirname(__FILE__)) . "layouts/single-product-{$selected_layout}.php";
                    if (file_exists($custom_template)) {
                        return $custom_template;
                    }
                }
            }
        }

        return $template;
    }

    /**
     * Custom add to cart for products
     * 
     * @return void
     */
    public static function custom_add_to_cart()
    {
        global $product;

        if (in_array($product->get_type(), CWPT_Settings::get('name_ids'))) {
            wc_get_template('single-product/add-to-cart/simple.php');
        }
    }

    /**
     * Display products shortcode
     * 
     * @param array $cwpt_type
     * @return void
     */
    public static function display_products($cwpt_type)
    {
        if (wp_is_json_request()) {
            return;
        }

        if (!is_admin()) {
            do_action('woocommerce_before_shop_loop');
            woocommerce_product_loop_start();
            ?>
            <style>
                .entry-content ul.products {
                    max-width: 100% !important;
                    max-width: 1600px;
                    padding: 4vw 6vw;
                    margin: 0 auto;
                }
            </style>
            <?php
            $args = array(
                'post_type'      => 'product',
                'posts_per_page' => 12,
                'tax_query'      => array(array(
                    'taxonomy' => 'product_type',
                    'field'    => 'slug',
                    'terms'    => $cwpt_type['product_type'],
                )),
            );
            $loop = new WP_Query($args);

            if ($loop->have_posts()) {
                while ($loop->have_posts()) {
                    $loop->the_post();
                    wc_get_template_part('content', 'product');
                }
                wp_reset_postdata();
                woocommerce_product_loop_end();
                do_action('woocommerce_after_shop_loop');
            } else {
                do_action('woocommerce_no_products_found');
            }

            do_action('woocommerce_after_main_content');
            do_action('woocommerce_sidebar');
            get_footer('shop');
        }
    }

    /**
     * Add WooCommerce class to body on custom pages
     * 
     * @param array $classes
     * @return array
     */
    public static function add_wc_class($classes)
    {
        if (!is_admin()) {
            global $post;

            if (!empty($post) && is_a($post, 'WP_Post')) {
                $current_page = get_post($post->ID);
                if (has_shortcode($current_page->post_content, 'cwpt_products')) {
                    $classes[] = 'woocommerce';
                }
            }

            return $classes;
        }
    }

    /**
     * Display shortcode code hint
     * 
     * @return string
     */
    public static function display_shortcode()
    {
        if (isset($_GET['post'])) {
            $post_id = intval($_GET['post']);
            $cwpt_post_name = get_post_meta($post_id, 'cwpt_product_type_name_singular', true);
            $cwpt_post_id = preg_replace('/\\s+/', '_', trim(preg_replace('/[^\\da-z ]/i', '', strtolower($cwpt_post_name))));

            return '<div class="cwpt-shortcode">
            <h4><strong>Use this shortcode to display this custom product type products</strong></h4>
            <code style="font-size: 1rem;">[cwpt_products product_type="' . esc_attr($cwpt_post_id) . '"]</code></div>';
        }
    }
}
