<?php

if (!defined('ABSPATH')) {
    exit;
}
use HvnlyNab\Frontend\LayoutManager;
// ==============================================
// GLOBAL WRAPPER FUNCTIONS (KEEP AS IS)
// ==============================================

/**
 * Output start page wrapper.
 */
if (!function_exists('hvnly_output_content_wrapper_start')) {
    function hvnly_output_content_wrapper_start()
    {
        hvnly_get_template_part('global/wrapper-start');
    }
}

/**
 * Output end page wrapper.
 */
if (!function_exists('hvnly_output_content_wrapper_end')) {
    function hvnly_output_content_wrapper_end()
    {
        hvnly_get_template_part('global/wrapper-end');
    }
}

// ==============================================
// SINGLE PROPERTY FUNCTIONS (NEW DESIGN)
// ==============================================

/* ==== Scroll Progress Functions ==== */

if (!function_exists('hvnly_single_property_scroll_progress')) {
    function hvnly_single_property_scroll_progress()
    {
        // Only show on single property pages
        if (!is_singular('hvnly_property')) {
            return;
        }

        echo '<!-- Scroll Progress Indicator -->
        <div class="hvnly-property-single__scroll-progress">
            <div class="hvnly-property-single__scroll-progress-bar" id="hvnlyPropertySingleScrollProgressBar"></div>
        </div>';
    }
}

/* ==== SVG Icons Functions ==== */

if (!function_exists('hvnly_single_property_svg_icons')) {
    function hvnly_single_property_svg_icons()
    {
        // Only show on single property pages
        if (!is_singular('hvnly_property')) {
            return;
        }

        hvnly_get_template_part('single-property/svg-icons');
    }
}

/* ==== Modal Popup Functions ==== */

if (!function_exists('hvnly_single_property_gallery_popup')) {
    function hvnly_single_property_gallery_popup()
    {
        // Only show on single property pages
        if (!is_singular('hvnly_property')) {
            return;
        }
        hvnly_get_template_part('single-property/modal-gallery');
    }
}

if (!function_exists('hvnly_single_property_video_popup')) {
    function hvnly_single_property_video_popup()
    {
        // Only show on single property pages
        if (!is_singular('hvnly_property')) {
            return;
        }

        hvnly_get_template_part('single-property/modal-video');
    }
}

if (!function_exists('hvnly_single_property_scroll_top')) {
    function hvnly_single_property_scroll_top()
    {
        // Check if back to top button is enabled in settings
        if (!hvnly_is_back_to_top_enabled()) {
            return;
        }

        // Only show on single property pages
        if (!is_singular('hvnly_property')) {
            return;
        }
        echo '<!-- Scroll to top button -->
        <div class="hvnly-property-single__gallery-scroll-top">
            <svg class="hvnly-icon hvnly-icon-thin"><use xlink:href="#hvnly-chevron-up"></use></svg>
        </div>';
    }
}

/* ==== Header Functions ==== */

if (!function_exists('hvnly_single_property_header')) {
    function hvnly_single_property_header()
    {
        echo '<div class="hvnly-property-single__header">';
        echo '<div class="hvnly-property-single__container">';
        
        do_action('hvnly_single_property_header');
        
        echo '</div>';
        echo '</div>';
    }
}

if (!function_exists('hvnly_single_property_breadcrumb')) {
    function hvnly_single_property_breadcrumb()
    {
        // Check if breadcrumbs are enabled in settings
        if (!hvnly_is_breadcrumb_enabled()) {
            return; // Exit early if disabled
        }
        
        hvnly_get_template_part('single-property/breadcrumb');
    }
}

if (!function_exists('hvnly_single_property_title_section')) {
    function hvnly_single_property_title_section()
    {
        hvnly_get_template_part('single-property/title-section');
    }
}

if (!function_exists('hvnly_single_property_meta')) {
    function hvnly_single_property_meta()
    {
        hvnly_get_template_part('single-property/meta');
    }
}

if (!function_exists('hvnly_single_property_actions')) {
    function hvnly_single_property_actions()
    {
        // Check all button settings dynamically
        $save_enabled = hvnly_is_save_property_enabled();
        $share_enabled = hvnly_is_share_button_enabled();
        $print_enabled = hvnly_is_print_button_enabled();
        
        // Only render if at least one action is enabled
        if (!$save_enabled && !$share_enabled && !$print_enabled) {
            return;
        }
        
        hvnly_get_template_part('single-property/actions', '', array(
            'save_enabled' => $save_enabled,
            'share_enabled' => $share_enabled,
            'print_enabled' => $print_enabled
        ));
    }
}

/* ==== Main Container Functions ==== */

if (!function_exists('hvnly_single_property_main_container_start')) {
    function hvnly_single_property_main_container_start()
    {
        echo '<div class="hvnly-property-single__container">';
    }
}

if (!function_exists('hvnly_single_property_main_container_end')) {
    function hvnly_single_property_main_container_end()
    {
        echo '</div>';
    }
}

/* ==== Layout Grid Functions ==== */

if (!function_exists('hvnly_single_property_layout_grid')) {
    function hvnly_single_property_layout_grid()
    {
        $layout_manager = LayoutManager::instance();
        $grid_classes = apply_filters('hvnly_layout_grid_classes', [], 'single');
        
        echo '<div class="' . esc_attr(implode(' ', $grid_classes)) . '">';
        do_action('hvnly_single_property_layout_grid');
        echo '</div>';
    }
}

/* ==== Main Content Area Functions ==== */

if (!function_exists('hvnly_single_property_main_content_area')) {
    function hvnly_single_property_main_content_area()
    {
        $layout_manager = LayoutManager::instance();
        $content_classes = apply_filters('hvnly_main_content_classes', [], 'single');
        
        echo '<div class="' . esc_attr(implode(' ', $content_classes)) . '">';
        do_action('hvnly_single_property_main_content');
        echo '</div>';
    }
}

/* ==== Gallery Carousel Functions ==== */

// if (!function_exists('hvnly_single_property_gallery_carousel')) {
//     function hvnly_single_property_gallery_carousel()
//     {
//         //hvnly_get_template_part('single-property/gallery-carousel');
//     }
// }


/* ==== Sidebar Area Functions ==== */

if (!function_exists('hvnly_single_property_sidebar_area')) {
    function hvnly_single_property_sidebar_area()
    {
        $layout_manager = LayoutManager::instance();
        $should_display = apply_filters('hvnly_should_display_sidebar', true, 'single');
        
        if (!$should_display) {
            return;
        }
        
        $sidebar_classes = apply_filters('hvnly_sidebar_classes', [], 'single');
        
        echo '<div class="' . esc_attr(implode(' ', $sidebar_classes)) . '">';
        do_action('hvnly_single_property_sidebar');
        echo '</div>';
    }
}

/* ==== Widget Functions ==== */

if (!function_exists('hvnly_display_sidebar_widgets')) {
    function hvnly_display_sidebar_widgets()
    {
       hvnly_get_template_part('single-property/sidebar');
    }
}






/* ==== Similar Properties Functions ==== */

if (!function_exists('hvnly_single_property_similar_properties')) {
    function hvnly_single_property_similar_properties()
    {
        echo '<div class="hvnly-property-single__similar-properties">';
        do_action('hvnly_single_property_similar_properties');
        echo '</div>';
    }
}

if (!function_exists('hvnly_single_property_similar_header')) {
    function hvnly_single_property_similar_header()
    {
        hvnly_get_template_part('single-property/similar-carousel-header');
    }
}

if (!function_exists('hvnly_single_property_similar_carousel')) {
    function hvnly_single_property_similar_carousel()
    {
        hvnly_get_template_part('single-property/similar-carousel-properties');
    }
}

if (!function_exists('hvnly_single_property_similar_carousel_dots')) {
    function hvnly_single_property_similar_carousel_dots()
    {
        hvnly_get_template_part('single-property/similar-carousel-dots');
    }
}

// ==============================================
// SEARCH FUNCTIONS (KEEP AS IS)
// ==============================================

/**
 * Output search loop start.
 */
if (!function_exists('hvnly_search_loop_start')) {
    function hvnly_search_loop_start()
    {
        hvnly_get_template_part('search/loop-start');
    }
}

/**
 * Output search loop end.
 */
if (!function_exists('hvnly_search_loop_end')) {
    function hvnly_search_loop_end()
    {
        hvnly_get_template_part('search/loop-end');
    }
}



// ==============================================
// ARCHIVE LAYOUT FUNCTIONS
// ==============================================

/**
 * Add dynamic archive layout styles based on sidebar visibility
 * This function adds CSS to adjust grid columns when sidebar is hidden
 * 
 * @since 2.0.3
 */
if (!function_exists('hvnly_add_archive_layout_styles')) {
    function hvnly_add_archive_layout_styles() {
        // Only apply on property archive pages
        if (!is_post_type_archive('hvnly_property') && !is_tax(get_object_taxonomies('hvnly_property'))) {
            return;
        }
        
        // Check if sidebar should be shown
        $show_sidebar = function_exists('hvnly_should_show_filter_sidebar') ? hvnly_should_show_filter_sidebar() : true;
        
        // If sidebar is shown, no need to add custom styles
        if ($show_sidebar) {
            return;
        }
        
        // Add custom CSS when sidebar is hidden
        ?>
<style id="hvnly-no-sidebar-styles">
/* When sidebar is hidden, make main container full width */
.hvnly-with-sidebar__contents__grids {
    grid-template-columns: 1fr !important;
    gap: 0;
}

/* Hide sidebar element if it exists */
.hvnly-property-filter-sidebar {
    display: none !important;
}

/* Adjust grid columns for full width */
.hvnly-property-grid-view.hvnly-grid-view {
    grid-template-columns: repeat(3, 1fr) !important;
}

/* Responsive adjustments */
@media (max-width: 1200px) {
    .hvnly-property-grid-view.hvnly-grid-view {
        grid-template-columns: repeat(2, 1fr) !important;
    }
}

@media (max-width: 768px) {
    .hvnly-property-grid-view.hvnly-grid-view {
        grid-template-columns: 1fr !important;
    }
}
</style>
<?php
    }
}