<?php 
    if ( ! defined( 'ABSPATH' ) ) exit;
    // BYOR View Styles
    // wp_enqueue_style('kirb-byor-strip-style', KIRB_PLUGIN_DIR . '/assets/byor-strip-styles.min.css', array(), KIRB_VERSION_CONTROL['byor_strip_styles_css'], 'all');
    // $kirb_strip_css_url = KIRB_PLUGIN_DIR . '/assets/byor-strip-styles-circle-step.min.css?v=' . (isset(KIRB_VERSION_CONTROL['byor_strip_styles_css']) ? KIRB_VERSION_CONTROL['byor_strip_styles_css'] : '');
    // $kirb_strip_js_url  = KIRB_PLUGIN_DIR . '/scripts/byor-view.js?v=' . (isset(KIRB_VERSION_CONTROL['byor_strip_js']) ? KIRB_VERSION_CONTROL['byor_strip_js'] : '');
    // printf('<link rel="stylesheet" href="%s" type="text/css" media="all" class="byor_strip_css_js" />', esc_url($kirb_strip_css_url));
    // printf('<script src="%s" type="text/javascript" class="byor_strip_css_js"></script>', esc_url($kirb_strip_js_url));

    wp_enqueue_style('kirb-byor-strip-style', esc_url(KIRB_PLUGIN_DIR . '/assets/byor-strip-styles-circle-step.min.css?v=' . KIRB_VERSION_CONTROL['byor_strip_styles_css']), array(), KIRB_VERSION_CONTROL['byor_strip_styles_css'], 'all');

    wp_enqueue_script('kirb-byor-view-js', esc_url(KIRB_PLUGIN_DIR . '/scripts/byor-view.js?v=' . KIRB_VERSION_CONTROL['byor_strip_js']), array('jquery'), KIRB_VERSION_CONTROL['byor_strip_js'], true);

    kirb_sideload_css();
    
    global $kirb_filters_data, $kirb_page_id, $kirb_byor_uid, $kirb_filter_values_translation_mapping;
    if(!$kirb_filters_data) {
        $kirb_filters_data = kirb_get_filters_data();
    }

    // if(!$kirb_site_config) {
    //     $kirb_site_config = kirb_get_config_file();
    // }
    
    if(!$kirb_byor_uid && kirb_is_valid_MD5(get_query_var('kirb_param'))) {
        $kirb_byor_uid = get_query_var('kirb_param');
    }

    if(!$kirb_filter_values_translation_mapping) {
        require_once(KIRB_BASE_DIR . '/translations_lookup.php');
    }

    $hide_editable = false;
    if(!empty($kirb_byor_uid) && kirb_is_valid_MD5($kirb_byor_uid) && kirb_get_user_id_by_byor_uid($kirb_byor_uid) != kirb_getCurrentUserId() ) {
        $hide_editable = true;
    }

    global $byor_data;
    $byor_data = kirb_getByorDataForCurrentUser($kirb_byor_uid);

    $isDiamondData = $byor_data && !empty($byor_data['diamond_uid']) && $byor_data['diamond_uid'] != '0';
    $isRingData = $byor_data && $byor_data['ring_post_id'] != 0 && isset($byor_data['ring']) && $byor_data['ring'];
    $step_active = $isDiamondData && $isRingData ? 'review' : ($isDiamondData ? 'Setting' : ($isRingData ? 'Diamond' : ''));

    $kirb_page_ids = json_decode(kirbGetMeta("_page_ids"), true);

    $diamond_label = "Diamond";
    if($byor_data) {
        $diamond_label = $byor_data['diamond_type'] == 0 ? "Diamond" : "Gemstone";
    }else if(isset($is_gemstone) && $is_gemstone) {
        $diamond_label = "Gemstone";
    }

    $kirb_current_page_id = get_the_ID() ? get_the_ID() : $kirb_page_id;

    $current_byor_uid = $byor_data ? $byor_data['uid'] : $kirb_byor_uid;

    $kirb_ring_setting_catg = kirbGetMeta('_setting_ring_setting_catg');
    
    if(!kirbGetMeta('_setting_ring_setting_catg')) {
        return;
    }

    $steps = array($diamond_label, 'Setting');

    // Setting Icon
    $setting_mode = 'ring';

    if(!$byor_data) {
        if(get_post_type($kirb_current_page_id) === 'product') {
            $product = wc_get_product($kirb_current_page_id);
            $product_cat_slugs = kirb_get_parent_slug($product);
            if (in_array($setting_cat_slug, $product_cat_slugs)) {
                $steps = array('Setting', $diamond_label);
            }
        }else if(!in_array($kirb_current_page_id, array_values($kirb_page_ids))) {
            $steps = array('Setting', $diamond_label);
        }
    }

    $is_byor_disabled = true;
    if($byor_data && $byor_data['order'] == '1') {
        $steps = array('Setting', $diamond_label);
    }

    if(!$byor_data || ($byor_data && $byor_data['setting_type'] == -1)) {
        // Check if the current page is a product category page
        $product_cat = get_queried_object();
        if($product_cat && $product_cat->taxonomy == 'product_cat') {
            $slug = $product_cat->slug;
            $page_cat_slugs = kirb_get_page_parent_slug($product_cat->slug);
        }
    }

    if($byor_data) {
        $is_byor_disabled = false;
    }

    $diamond_step = '
        <div class="kirb-item-byor {{step-active}}">
            <div class="kirb-byor-pedding">
                <div class="kirb-byor-icon-circle {{active}}">
                    <a href="{{icon_url}}">
                        <img class="kirb-byor-icon kirb-dynamic-icon {{highlight}}" src="'.esc_url(KIRB_PLUGIN_DIR).'/assets/images/select-diamond.svg" loading="lazy" width="30" height="30" alt="diamond-setting">
                    </a>
                </div>
            </div>    
            <div class="kirb-byor-number {{active}}">
                <span class="kirb-number-head">{{count}}</span>
                <span class="kirb-number-head head-dot kirb-mobile-view-diamond">.</span>
                <span>{{label}}</span>
            </div>
        </div>
    ';
    $diamond_step_active_class = 'kirb-show-dropdown-byor byor-dropdown-diamond';
    
    // Mode based Icon & Text Update
    $setting_icon = "select-setting.svg";
    $review_icon = "complete-ring.svg";
    $review_label = esc_html__("Complete Ring", 'diamond-ring-builder-jewelry');

    $setting_step = '
        <div class="kirb-item-byor {{step-active}}">
            <div class="kirb-byor-pedding">
                <div class="kirb-byor-icon-circle {{active}}">
                    <a href="{{icon_url}}">
                       <img class="kirb-byor-icon kirb-dynamic-icon {{highlight}}" src="'.esc_url(KIRB_PLUGIN_DIR).'/assets/images/'.$setting_icon.'" loading="lazy" width="30" height="30" alt="diamond-setting">
                    </a>
                </div>
            </div>    
            <div class="kirb-byor-number {{active}}" >
                <span class="kirb-number-head">{{count}}</span>
                <span class="kirb-number-head head-dot kirb-mobile-view-diamond">.</span>
                <span>{{label}}</span>
            </div>
        </div>
    ';
    $setting_step_active_class = 'kirb-show-dropdown-byor byor-dropdown-setting';

    $review_step = '
        <div class="kirb-item-byor {{step-active}}">
            <div class="kirb-byor-pedding">
                <div class="kirb-byor-icon-circle {{active}}">
                    <a href="{{icon_url}}">
                        <img class="kirb-byor-icon kirb-dynamic-icon {{highlight}}" src="'.esc_url(KIRB_PLUGIN_DIR).'/assets/images/'.$review_icon.'" loading="lazy" width="30" height="30" alt="diamond-setting">
                    </a>
                </div>
            </div>    
            <div class="kirb-byor-number {{active}}">
                <span class="kirb-number-head">3</span>
                <span class="kirb-number-head head-dot kirb-mobile-view-diamond">.</span>
                <span>'.$review_label.'</span>
            </div>
        </div>
    ';
    $review_step_active_class = 'kirb-show-dropdown-byor byor-dropdown-ring';

    $byor_ring_data = null;
    $byor_diamond = null;
    if($isRingData) {
        $byor_ring_data = $byor_data['ring'];
    }
    if($isDiamondData) {
        $byor_diamond = $byor_data['diamond'];
    }

    $kirb_trans_lookup = array(
        'Diamond' => esc_html__('Diamond', 'diamond-ring-builder-jewelry'),
        'Gemstone' => esc_html__('Gemstone', 'diamond-ring-builder-jewelry'),
        'Setting' => esc_html__('Setting', 'diamond-ring-builder-jewelry'),
        'White Gold 14K' => esc_html__('White Gold 14K', 'diamond-ring-builder-jewelry'),
        'Yellow Gold 14K' => esc_html__('Yellow Gold 14K', 'diamond-ring-builder-jewelry'),
        'Rose Gold 14K' => esc_html__('Rose Gold 14K', 'diamond-ring-builder-jewelry'),
        'White Gold 18K' => esc_html__('White Gold 18K', 'diamond-ring-builder-jewelry'),
        'Yellow Gold 18K' => esc_html__('Yellow Gold 18K', 'diamond-ring-builder-jewelry'),
        'Rose Gold 18K' => esc_html__('Rose Gold 18K', 'diamond-ring-builder-jewelry'),
        'Platinum' => esc_html__('Platinum', 'diamond-ring-builder-jewelry'),
    );
?>
<div class="kirb-main-byor-section kirb-byor-strip-container">
    <div class="kirb-new-byor-section" >
        <?php 
            $i = 1;
            foreach($steps as $step) {
                $is_current_step_inactive = ($step == 'Setting' && !$isRingData) || (($step == 'Diamond' || $step == 'Gemstone') && !$isDiamondData);

                $replace_arr = array();
                if($step == 'Diamond' || $step == 'Gemstone') {
                    $diamond_title = $byor_data['diamond']['title'] ?? '';
                    if($is_current_step_inactive) {
                        // Inactive
                        $replace_arr = array(
                            '{{active}}'      => esc_attr(''), 
                            '{{count}}'       => (int) $i, 
                            // translators: %s will be replaced by Diamond or Gemstone
                            '{{label}}'       => esc_html(sprintf(__('Select %s','diamond-ring-builder-jewelry'), ($kirb_trans_lookup[$step] ?? $step))),
                            '{{step-active}}' => esc_attr(''),
                            '{{highlight}}'   => esc_attr(''),
                            '{{icon_url}}'    => esc_url(kirb_getPageUrlById($kirb_page_ids['loose-diamonds']))
                        );
                    }else{
                        // Active
                        $replace_arr = array(
                            '{{active}}'      => esc_attr($diamond_step_active_class), 
                            '{{count}}'       => (int) $i, 
                            '{{label}}'       => esc_html($kirb_trans_lookup[$step] ?? $step),
                            '{{step-active}}' => esc_attr('active'),
                            '{{highlight}}'   => esc_attr('kirb_highlight_img_btn'),
                            '{{icon_url}}'    => esc_url(kirb_getPageUrlById($kirb_page_ids[str_replace("kirb-","",kirb_getDiamondCategory($byor_diamond['category_id'], $kirb_filters_data))]))
                        );
                    }
                    $diamond_step = str_replace(array_keys($replace_arr), array_values($replace_arr), $diamond_step);
                    echo wp_kses_post($diamond_step);
                }else if($step == 'Setting') {
                    if($is_current_step_inactive) {
                        // Inactive
                        $replace_arr = array(
                            '{{active}}'      => esc_attr(''), 
                            '{{count}}'       => (int) $i, 
                            '{{step-active}}' => esc_attr(''),
                            '{{highlight}}'   => esc_attr(''),
                            // translators: %s will be replaced by Setting
                            '{{label}}'       => esc_html(sprintf(__('Select %s', 'diamond-ring-builder-jewelry'), ($kirb_trans_lookup[$step] ?? $step))),
                        );
                    }else{
                        // Active
                        $replace_arr = array(
                            '{{active}}'      => esc_attr($setting_step_active_class), 
                            '{{count}}'       => (int) $i, 
                            '{{step-active}}' => esc_attr('active'),
                            '{{highlight}}'   => esc_attr('kirb_highlight_img_btn'),
                            '{{label}}'       => esc_html($kirb_trans_lookup[$step] ?? $step),
                        );
                    }

                    $replace_arr['{{icon_url}}'] = esc_url(get_term_link(get_term_by('slug', $kirb_ring_setting_catg, 'product_cat')));

                    $setting_step = str_replace(array_keys($replace_arr), array_values($replace_arr), $setting_step);
                    echo wp_kses_post($setting_step);
                }
                $i++;
            }
            if($step_active == 'review') {
                $review_step = str_replace(array('{{active}}', '{{step-active}}', '{{highlight}}', '{{icon_url}}'), array($review_step_active_class, 'active', 'kirb_highlight_img_btn', esc_url(kirb_getPageUrlById($kirb_page_ids['byor-review']) . '/' . $current_byor_uid)), $review_step);
            }else{
                $review_step = str_replace(array('{{active}}', '{{step-active}}', '{{highlight}}', '{{icon_url}}'), '', $review_step);
            }
            echo wp_kses_post($review_step);
        ?>
    </div>

    <div class="kirb-byor-detail-dropdown tooltip-content-byor-dropdown-diamond" style="display: none;">
        <?php 
            if($isDiamondData) {
                $diamond_title = $byor_data['diamond']['title'];
                $byor_price = kirb_format_price($byor_diamond['saleamt']);
                $byor_url_change = kirb_getPageUrlById($kirb_page_ids[str_replace("kirb-","",kirb_getDiamondCategory($byor_diamond['category_id'], $kirb_filters_data))]);
                $byor_diamond_url = kirb_getPageUrlById($kirb_page_ids['diamond-detail']) . kirb_prepareDiamondSlug($byor_diamond) . '/';
                $byor_diamond_src = isset($byor_diamond['imageurl']) ? esc_url($byor_diamond['imageurl']) : '';
                $byor_diamond_fallback = esc_url(kirb_getShapeErrorImage($byor_diamond['shape']));
        ?>
            <div class="kirb-image-section">
                <img src="<?php echo esc_url($byor_diamond_src); ?>" onerror="this.src='<?php echo esc_js($byor_diamond_fallback); ?>';this.onerror=null;this.style.width='100%';this.style.height='100%';" alt="">
            </div>
            <div class="kirb-byor-content-section">
                <div class="kirb-inner-content">
                    <div class="kirb-heading-byor"><?php echo esc_html(kirb_string_split_translate($diamond_title, $kirb_filter_values_translation_mapping)) ?></div>
                    <p>
                        <span><?php echo esc_html__('SKU', 'diamond-ring-builder-jewelry');?>: <?php echo esc_html($byor_data['diamond']['stone_no']); ?></span>
                        <span><?php echo esc_html($byor_data['diamond']['lab']); ?>: <?php echo esc_html($byor_data['diamond']['lab_report_no']); ?></span>
                    </p>
                </div>
                <div class="kirb-price-view-section">
                    <div class="kirb-byor-price"><?php echo esc_html($byor_price); ?></div>
                    <div class="kirb-byor-button-section">
                        <button class="kirb-btn-byor-view" data-url="<?php echo esc_url($byor_diamond_url);?>"><?php echo esc_html__('View', 'diamond-ring-builder-jewelry');?></button>
                        <?php if(!$hide_editable) { ?>
                            <button class="kirb-btn-byor-change" data-url="<?php echo esc_url($byor_url_change);?>"><?php echo esc_html__('Change', 'diamond-ring-builder-jewelry');?></button>
                        <?php } ?>
                    </div>
                </div>
            </div>
        <?php 
            }
        ?>
    </div>

    <div class="kirb-byor-detail-dropdown tooltip-byor-dropdown-setting" style="display: none;">
        <?php 
            if($isRingData) {
                $ring_title = $byor_ring_data->get_title();
                $byor_price = kirb_format_price($byor_ring_data->get_price());
                $metal_name = kirb_get_metal_name_by_variation_id($byor_data['ring_variation_id']);
                $subtitle = "";
                if(!empty($metal_name)) {
                    $subtitle = esc_html($kirb_trans_lookup[$metal_name] ?? $metal_name);
                }
                
                $byor_ring_url = "";
                $byor_ring_id = $byor_data['ring_post_id'];
                
                if($byor_data['ring_is_variable']) {
                    $byor_ring_url = get_permalink($byor_data['ring_variation_id']);
                }else{  
                    $byor_ring_url = get_permalink($byor_ring_id);
                }

                $byor_url_change = get_term_link(get_term_by('slug',kirbGetMeta('_setting_ring_setting_catg'), 'product_cat'));

                $byor_ring_thumbnail = "";
                $byor_ring_error_image = KIRB_PLUGIN_DIR . '/assets/images/no-image.png';
                if($byor_data['ring_is_variable']) {
                    $byor_ring_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($byor_data['ring_variation_id']), 'single-post-thumbnail')[0] ?? wp_get_attachment_image_src(get_post_thumbnail_id($byor_ring_id), 'single-post-thumbnail')[0] ?? $byor_ring_error_image;
                }else{
                    $byor_ring_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($byor_ring_id),'single-post-thumbnail')[0] ?? $byor_ring_error_image;
                }
                $byor_ring_image_src = $byor_ring_thumbnail ? esc_url($byor_ring_thumbnail) : '';
        ?>
            <div class="kirb-image-section">
                <img src="<?php echo esc_url($byor_ring_image_src); ?>" alt="">
            </div>

            <div class="kirb-byor-content-section">
                <div class="kirb-inner-content">
                    <div class="kirb-heading-byor"><?php echo esc_html($ring_title);?></div>
                    <?php if(!empty($subtitle)) { ?>
                        <p>
                            <span>Metal Type: <?php echo esc_html($subtitle); ?></span>
                        </p>
                    <?php } ?>
                </div>
                <div class="kirb-price-view-section">
                    <div class="kirb-byor-price"><?php echo esc_html($byor_price); ?></div>
                    <div class="kirb-byor-button-section">
                        <button class="kirb-btn-byor-view" data-url="<?php echo esc_url($byor_ring_url); ?>"><?php echo esc_html__('View', 'diamond-ring-builder-jewelry');?></button>
                        <?php if(!$hide_editable) { ?>
                            <button class="kirb-btn-byor-change" data-url="<?php echo esc_url($byor_url_change); ?>"><?php echo esc_html__('Change', 'diamond-ring-builder-jewelry');?></button>
                        <?php } ?>
                    </div>
                </div>
            </div>
        <?php 
            }
        ?>
    </div>
    <?php 
        if($step_active == 'review') {
    ?>
        <div class="kirb-byor-detail-dropdown tooltip-byor-dropdown-ring" style="display: none;">
            <div class="kirb-inner-byor-detail-dropdown">
            <div class="kirb-image-section">
                    <img src="<?php echo esc_url($byor_ring_image_src); ?>" alt="">
            </div>
            <div class="kirb-image-section kirb-byor-laptop-view">
                    <img src="<?php echo esc_url($byor_diamond_src); ?>" onerror="this.src='<?php echo esc_js($byor_diamond_fallback); ?>';this.onerror=null;this.style.width='100%';this.style.height='100%';" alt="">
            </div>
            <div class="kirb-byor-content-section">
                <div class="kirb-inner-content">
                    <div class="">
                        <div class="kirb-heading-byor"><?php echo esc_html($ring_title);?></div>
                        <?php if(!empty($subtitle)) { ?>
                            <p>
                                <span>Metal Type: <?php echo esc_html($subtitle); ?></span>
                            </p>
                        <?php } ?>
                    </div>
                    <div class ="kirb-byor-laptop-view">
                        <div class="kirb-heading-byor">
                            <?php echo esc_html(kirb_string_split_translate($diamond_title,$kirb_filter_values_translation_mapping)) ?>
                        </div>
                        <p>
                            <span><?php echo esc_html__('SKU', 'diamond-ring-builder-jewelry');?>: <?php echo esc_html($byor_data['diamond']['stone_no']); ?></span>
                            <span><?php echo esc_html($byor_data['diamond']['lab']); ?>: <?php echo esc_html($byor_data['diamond']['lab_report_no']); ?></span>
                        </p>
                    </div>
                </div>
                <div class="kirb-price-view-section kirb-byor-laptop-view">
                    <div class="kirb-byor-price"><?php echo esc_html(kirb_format_price(kirb_price_roundoff($byor_diamond['saleamt'], null)+intval($byor_ring_data->get_price())));?></div>
                    <div class="kirb-byor-button-section">
                        <button class="kirb-btn-byor-view" data-url="<?php echo esc_url(kirb_getPageUrlById($kirb_page_ids['byor-review']) . '/' . $current_byor_uid);?>"><?php echo esc_html__('View', 'diamond-ring-builder-jewelry');?></button>
                        <?php if(!$hide_editable) { ?>
                            <button class="kirb-btn-byor-change kirb-byor-reset-btn"><?php echo esc_html__('Reset', 'diamond-ring-builder-jewelry');?></button>
                        <?php } ?>
                    </div>
                </div>
            </div>
            </div>

            <div class="kirb-inner-byor-detail-dropdown kirb-mobile-view-diamond">
            <div class="kirb-image-section">
                <img src="<?php echo esc_url($byor_diamond_src); ?>" onerror="this.src='<?php echo esc_js($byor_diamond_fallback); ?>';this.onerror=null;this.style.width='100%';this.style.height='100%';" alt="">
            </div>

            <div class="kirb-byor-content-section">
                <div class="kirb-inner-content">
                    <div class="kirb-heading-byor"><?php echo esc_html(kirb_string_split_translate($diamond_title, $kirb_filter_values_translation_mapping)) ?></div>
                    <p>
                        <span><?php echo esc_html__('SKU', 'diamond-ring-builder-jewelry');?>: <?php echo esc_html($byor_data['diamond']['stone_no']); ?></span>
                        <span><?php echo esc_html($byor_data['diamond']['lab']); ?>: <?php echo esc_html($byor_data['diamond']['lab_report_no']); ?></span>
                    </p>
                </div>
            </div>
            </div>
            <div class="kirb-price-competed-section kirb-mobile-view-diamond">
                <div class="kirb-byor-button-section">
                    <button class="kirb-btn-byor-view" data-url="<?php echo esc_url(kirb_getPageUrlById($kirb_page_ids['byor-review']) . $current_byor_uid);?>"><?php echo esc_html__('View', 'diamond-ring-builder-jewelry');?></button>
                    <?php if(!$hide_editable) { ?>
                        <button class="kirb-btn-byor-change kirb-byor-reset-btn"><?php echo esc_html__('Reset', 'diamond-ring-builder-jewelry');?></button>
                    <?php } ?>
                </div>
                <div class="kirb-byor-price"><?php echo esc_html(kirb_format_price(kirb_price_roundoff($byor_diamond['saleamt'], null)+intval($byor_ring_data->get_price())));?></div>
            </div>
        </div>
    <?php } ?>
    
</div>
