<?php

/* NBDesigner Advanced */
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/* hooks */
add_filter('porto_search_form', 'demo_edit');
add_action('wp_enqueue_scripts', 'nbd_scripts', 10);
add_filter('porto_wishlist', 'change_porto_wishlist');
add_action('wp_enqueue_scripts', 'porto_child_css', 1001);
add_action('nbd_js_config', 'porto_show_design_js_config');
add_action('nbd_footer_list_products', 'footer_list_products');
add_action('woocommerce_single_product_summary', 'nbd_form_start_design');
add_action('nbd_woocommerce_before_single_product_summary', 'product_templates');

require_once 'start-design.php';

// Load CSS
function porto_child_css()
{
    // porto child theme styles
    wp_deregister_style('styles-child');
    wp_register_style('styles-child', esc_url(get_stylesheet_directory_uri()) . '/style.css');
    wp_enqueue_style('styles-child');

    if (is_rtl()) {
        wp_deregister_style('styles-child-rtl');
        wp_register_style('styles-child-rtl', esc_url(get_stylesheet_directory_uri()) . '/style_rtl.css');
        wp_enqueue_style('styles-child-rtl');
    }
}
function nbd_scripts()
{
    wp_enqueue_script('nbd-script', get_stylesheet_directory_uri() . '/js/js.js', array('jquery'));
}

function porto_show_design_js_config()
{
?>
var porto_show_design = true;
<?php
}
function demo_edit()
{
    return;
}

function change_porto_wishlist()
{
    return;
}

function product_templates()
{
?>
<section class="nbd_templates">
    <div id="portoCarousel" class="carousel slide" data-bs-ride="carousel">
        <div class="carousel-indicators">
            <?php
                global $product;
                $product_id         = $product->get_id();
                $api                = new NBD_API();
                $template_folder    = $api->get_gallery_folder($product_id);
                $template_path      = NBDESIGNER_CUSTOMER_DIR . '/' . $template_folder . '/preview';
                $images             = Nbdesigner_IO::get_list_images($template_path, 1);
                ksort($images);
                $nbd_settings       = unserialize(get_post_meta($product_id, '_designer_setting', true));
                $html               = '';
                $thumbnail_size     = $image_size = $full_size = nbdesigner_get_option('nbdesigner_template_width', 500);
                foreach ($images as $index => $value) {
                ?>
            <button type="button" data-bs-target="#portoCarousel" data-bs-slide-to="<?php echo $index ?>"
                <?php if ($index == 0) echo 'aria-current="true" class="active"' ?>
                aria-label="Slide <?php echo $index + 1 ?>"></button>
            <?php
                }
                ?>
        </div>
        <div class="carousel-inner">
            <?php

                foreach ($images as $key => $image_path) {
                    list($width, $height)   = getimagesize($image_path);
                    $full_src               = $thumbnail_src = Nbdesigner_IO::wp_convert_path_to_url($image_path);
                    $image_caption          = isset($nbd_settings[$key]) ? $nbd_settings[$key]['orientation_name'] : '';
                    $attr                   = array(
                        'title'                     => $image_caption,
                        'data-caption'              => $image_caption,
                        'data-src'                  => esc_url($full_src),
                        'data-large_image'          => esc_url($full_src),
                        'data-large_image_width'    => $width,
                        'data-large_image_height'   => $height,
                        'class'                     => '',
                    );
                    $size_class   = $width . 'x' . $height;
                    $default_attr = array(
                        'src'   => $full_src,
                        'class' => "attachment-$size_class size-$size_class",
                        'alt'   => $image_caption
                    );
                    $attr       = wp_parse_args($attr, $default_attr);
                    $attr       = array_map('esc_attr', $attr);
                    $hwstring   = image_hwstring($width, $height);
                ?>
            <div class="carousel-item <?= $key == 0 ? 'active' : ''; ?>">
                <?php
                        $image_html = rtrim("<img $hwstring");
                        foreach ($attr as $name => $value) {
                            $image_html .= " $name=" . '"' . $value . '"';
                        }
                        $image_html .= ' />';
                        $html = '';
                        $html       .= '<div data-thumb="' . esc_url($thumbnail_src) . '" data-thumb-alt="' . esc_attr($image_caption) . '" class="woocommerce-product-gallery__image">
                        <a href="' . esc_url($full_src) . '">' . $image_html . '</a>
                        </div>';
                        echo $html;
                        ?>
            </div>
            <?php
                }
                ?>
        </div>
        <button class="carousel-control-prev" type="button" data-bs-target="#portoCarousel" data-bs-slide="prev">
            <div class="nbd_prev-template">
                <i class='nbd_prev'></i>
            </div>
        </button>
        <button class="carousel-control-next" type="button" data-bs-target="#portoCarousel" data-bs-slide="next">
            <div class="nbd_next-template">
                <i class='nbd_next'></i>
            </div>
        </button>
    </div>
</section>
<?php
}

function nbd_logo_porto($sticky_logo = false)
{
    global $porto_settings;
    if ($porto_settings['logo'] && $porto_settings['logo']['url']) {
        $logo_width  = '';
        $logo_height = '';
        $logo        = $porto_settings['logo']['url'];
        if ($sticky_logo && $porto_settings['sticky-logo'] && $porto_settings['sticky-logo']['url']) {
            $logo = $porto_settings['sticky-logo']['url'];
        }
        if (isset($porto_settings['logo-retina-width']) && isset($porto_settings['logo-retina-height']) && $porto_settings['logo-retina-width'] && $porto_settings['logo-retina-height']) {
            $logo_width  = (int) $porto_settings['logo-retina-width'];
            $logo_height = (int) $porto_settings['logo-retina-height'];
        }

        // sticky logo
        if (!$sticky_logo && isset($porto_settings['sticky-logo-retina']) && $porto_settings['sticky-logo-retina'] && $porto_settings['sticky-logo-retina']['url']) {
            $sticky_retina_logo_src = $porto_settings['sticky-logo-retina']['url'];
        }
        if (!$sticky_logo && $porto_settings['sticky-logo'] && $porto_settings['sticky-logo']['url']) {
            $sticky_logo_src = $porto_settings['sticky-logo']['url'];
            echo '<img class="img-responsive sticky-logo' . (!isset($sticky_retina_logo_src) || !$sticky_retina_logo_src || $sticky_retina_logo_src == $sticky_logo_src ? ' sticky-retina-logo' : '') . '"' . ($logo_width ? ' width="' . $logo_width . '"' : '') . ($logo_height ? ' height="' . $logo_height . '"' : '') . ' src="' . esc_url(str_replace(array('http:', 'https:'), '', $sticky_logo_src)) . '" alt="' . esc_attr(get_bloginfo('name', 'display')) . '" />';
        }
        if (isset($sticky_retina_logo_src) && $sticky_retina_logo_src && $sticky_retina_logo_src != $sticky_logo_src) {
            echo '<img class="img-responsive sticky-retina-logo"' . ($logo_width ? ' width="' . $logo_width . '"' : '') . ($logo_height ? ' height="' . $logo_height . '"' : '') . ' src="' . esc_url(str_replace(array('http:', 'https:'), '', $sticky_retina_logo_src)) . '" alt="' . esc_attr(get_bloginfo('name', 'display')) . '" style="max-height:' . $logo_height . 'px;" />';
        }

        // default logo
        $retina_logo = '';
        if (isset($porto_settings['logo-retina']) && $porto_settings['logo-retina'] && $porto_settings['logo-retina']['url']) {
            $retina_logo = $porto_settings['logo-retina']['url'];
        }
        if ($sticky_logo && isset($porto_settings['sticky-logo-retina']) && $porto_settings['sticky-logo-retina'] && $porto_settings['sticky-logo-retina']['url']) {
            $retina_logo = $porto_settings['sticky-logo-retina']['url'];
        }

        echo '<img class="img-responsive standard-logo' . (!$retina_logo || $retina_logo == $logo ? ' retina-logo' : '') . '"' . ($logo_width ? ' width="' . $logo_width . '"' : '') . ($logo_height ? ' height="' . $logo_height . '"' : '') . ' src="' . esc_url(str_replace(array('http:', 'https:'), '', $logo)) . '" alt="' . esc_attr(get_bloginfo('name', 'display')) . '" />';

        if ($retina_logo && $retina_logo != $logo) {
            echo '<img class="img-responsive retina-logo"' . ($logo_width ? ' width="' . $logo_width . '"' : '') . ($logo_height ? ' height="' . $logo_height . '"' : '') . ' src="' . esc_url(str_replace(array('http:', 'https:'), '', $retina_logo)) . '" alt="' . esc_attr(get_bloginfo('name', 'display')) . '"' . ($logo_height ? ' style="max-height:' . $logo_height . 'px;"' : '') . ' />';
        }
    }
}

function footer_list_products()
{
    require_once 'porto-footer.php';
}

function nbd_form_start_design()
{
?>
<section class="nbd_main-form-start-design">
    <div class="nbd_header-form">
        <div class="nbd_header-product">
            <div class="nbd_product-name">
                <span class="nbd-name">
                    <?php
                        $product = wc_get_product(get_the_ID());
                        echo $product->get_title();
                        ?>
                </span>
            </div>
            <div class="nbd_product-price">
               <?php
                    $_percent = get_user_meta(nbd_getuid(), 'percent');
                    $product = wc_get_product(get_the_ID());
                    $p_price = $product->get_price();
                    $cost = '';
                    $percent_css = '';
                    if (empty($_percent)) {
                        echo '<span class="nbd-price ' . $percent_css . '">$' . $p_price . '</span>';
                    }
                    if ($_percent != '' && !empty($_percent)) {
                        $percent = $_percent[0];
                        $sale_price = ($p_price * $percent) / 100;
                        $price = $p_price - $sale_price;
                        $cost = 'nbd_cost';
                        $percent_css = 'sale_price';
                        echo '<span class="nbd-price ' . $cost . '">$' . $p_price . '</span>';
                        echo '<span class="nbd-price ' . $percent_css . '">$' . $price . '</span>';
                    }
                    ?>
            </div>
        </div>
    </div>
    <div class="nbd_content-form">
        <div class="nbd_form-select">
            <div class="nbd_select">
                <!-- code here -->
            </div>
            <div class="nbd_footer-form">
                <div class="nbd_text">
                    <p>
                        Custom tissue paper custom tissue paper custom tissue paper custom tissue paper custom tissue
                        paper custom tissue paper custom custom tissue paper custom custom tissue paper custom custom
                        tissue paper custom.
                    </p>
                </div>

                <div class="nbd_button-show-design">
                    <script>
                    window.onload = function() {
                        page_loaded();
                    }
                    </script>
                    <script>
                    function page_loaded() {
                        jQuery('#triggerDesign').text('Siguiente >');
                    }
                    </script>
                </div>
            </div>
        </div>
    </div>
</section>
<?php
}



function after_user_login_pt()
{
    if (nbd_getuid()) {
        $role_id = get_user_meta(nbd_getuid(), 'user_in_web');
        if (!isset($role_id)) {
            update_user_meta(nbd_getuid(), 'user_in_web', 1);
        }
    }
}
add_action('woocommerce_after_customer_login_form', 'after_user_login_pt', 10, 1);