<?php
if (!defined('ABSPATH')) {
    exit;
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Image_Size;
use \Elementor\Repeater;
use \Elementor\Icons_Manager;
use \Sifency\Traits\Sifency_Arg_Controls;
use \Sifency\Traits\Sifency_Animation_Controls;
use \Sifency\Traits\Sifency_Elementor_Parity_Controls;
use Sifency\Traits\Sifency_Core_Text_Style;

use Sifency\Traits\Sifency_Button_Style_Controls;
use Sifency\Traits\Sifency_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Icon_Style_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Services_Controls;

class Sifency_Addons_Widget_Services extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Arg_Controls;
    use Sifency_Core_Text_Style;

    use Sifency_Button_Style_Controls;
    use Sifency_Item_Style_Controls;
    use Sifency_Addons_Slider_Controls;
    use Sifency_Services_Controls;

    public function get_name() {
        return 'sifency-addons-services';
    }
    
    public function get_title() {
        return esc_html__('Sifency Services', 'sifency-addons');
    }
    
    public function get_icon() {
        return 'eicon-info-box';
    }
    
    public function get_categories() {
        return ['sifency-advance-widgets'];
    }
    public function get_script_depends() {
        return $this->get_carousel_script_depends();
    }

    public function get_style_depends() {
        return [ 'sifency-typo-style' , 'swiper', 'sifency-swiper-style', 'sifency-frontend-style', 'sifency-services-style', 'sifency-icon' ];
    }
    protected function register_controls() {
        $carousel_condition = [
            'condition' => [
                'layout_style' => 'carousel'
            ]
        ];
        $this->add_sifency_services_controls();
        $this->add_sifency_carousel_controls($carousel_condition);
        $this->add_sifency_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Service Heading',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Service Content',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_button_style_controls();
        $this->add_sifency_icon_style_controls();
        Sifency_Elementor_Parity_Controls::register( $this );
        $this->add_sifency_animation_controls();
    }

/**
     * Render the widget output on the frontend.
     *
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-services-wrapper'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        $is_carousel = ('carousel' === $settings['layout_style']);
        $layout_container_attrs = '';
        
        if ($is_carousel) {
            $this->add_render_attribute('layout_container', 'class', 'swiper-container');
            $this->add_render_attribute('wrapper', 'class', 'sifency-carousel-wrapper'); 
            
            $slide_count = ('posts' === $settings['source'])
                ? (new \WP_Query(array_merge($this->get_sifency_query_args() ?? [], ['post_type' => $settings['query_post_type'] ?? 'service'])))->post_count
                : count($settings['service_list'] ?? []);

            $slider_options = $this->get_sifency_swiper_options($settings, $slide_count);
            $this->add_render_attribute('wrapper', 'data-swiper-options', wp_json_encode($slider_options));
            
            $layout_container_attrs = $this->get_render_attribute_string('layout_container');
        } else {
            $this->add_render_attribute('layout_container', 'class', 'sifency-services-grid');
            $layout_container_attrs = $this->get_render_attribute_string('layout_container');
        }
        ?>
        <div <?php  ?><?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . ' ' . $wrapper_attrs); ?>>
            <div <?php  ?><?php echo wp_kses_post($layout_container_attrs); ?>>
                <?php if ($is_carousel) { echo '<div class="swiper-wrapper">'; } ?>

                <?php
                if ( 'posts' === $settings['source'] ) {
                    $query = isset($query) ? $query : null;
                    $this->render_posts_content($settings, $animation_manager, $query);
                } else {
                    $this->render_custom_content($settings, $animation_manager);
                }
                ?>

                <?php if ($is_carousel) { echo '</div>'; } ?>
                
                <?php

                if ($is_carousel) {
                    if ( 'yes' === ($settings['show_arrows'] ?? 'no') ) { ?>
                        <div class="sifency-slider-nav-container">
                            <div class="sifency-slider-nav-arrow sifency-slider-nav-prev swiper-button-prev">
                                <i class="icon-left"></i>
                            </div>
                            <div class="sifency-slider-nav-arrow sifency-slider-nav-next swiper-button-next">
                                <i class="icon-right"></i>
                            </div>
                        </div>
                    <?php }
                    if ( ! empty($settings['show_pagination']) ) { 
                        echo '<div class="swiper-pagination"></div>';
                    }
                }
                ?>
            </div>
        </div>
        <?php
    }

    private function render_custom_content($settings, Sifency_Animation_Manager $animation_manager) {
        if ( empty($settings['service_list']) ) { return; }
        
        $is_carousel = ('carousel' === $settings['layout_style']);

        foreach ($settings['service_list'] as $index => $item) {
            $oi = $index + 1;
            $parity_class = ($oi % 2 !== 0) ? 'odd-item' : 'even-item';
            $item_container_tag = $is_carousel ? 'div' : 'article';
            $item_container_classes = $is_carousel ? ['swiper-slide'] : ['sifency-item-style', 'sifency-service-item', $parity_class];
            $item_container_attrs = $animation_manager->get_item_attributes($item_container_classes);
            ?>
            <<?php  ?><?php echo esc_html($item_container_tag); ?> <?php echo wp_kses_post($item_container_attrs); ?>>
                <?php
                if ($is_carousel) : ?>
                    <article class="sifency-item-style sifency-service-item <?php echo esc_attr($parity_class); ?>">
                <?php endif; ?>

                    <?php if ( ! empty($item['icon']['value']) ) : ?>
                        <div class="sifency-service-icon sifency-icon-style"><?php \Elementor\Icons_Manager::render_icon($item['icon']); ?></div>
                    <?php endif; ?>
                    <h3 class="sifency-service-title sifency-heading-style"><?php echo esc_html($item['title']); ?></h3>
                    <?php if ( ! empty($item['excerpt']) ) : ?>
                        <div class="sifency-service-excerpt sifency-content-style"><?php echo wp_kses_post($item['excerpt']); ?></div>
                    <?php endif; ?>
                    <a href="<?php echo esc_url($item['link']['url']); ?>" class="sifency-service-button sifency-button-style">
                        <?php echo esc_html($settings['read_more_text']); ?>
                        <i class="icon-right"></i>
                    </a>

                <?php if ($is_carousel) : ?>
                    </article>
                <?php endif; ?>
            </<?php echo esc_html($item_container_tag); ?><?php  ?>>
            <?php
        }
    }

    private function render_posts_content($settings, Sifency_Animation_Manager $animation_manager, $query = null) {
        if (is_null($query)) {
            $query_args = method_exists($this, 'get_sifency_query_args') ? $this->get_sifency_query_args() : [];
            $query_args['post_type'] = $settings['query_post_type'] ?? 'service';
            $query = new \WP_Query( $query_args );
        }

        if ( ! $query->have_posts() ) { return; }

        $is_carousel = ('carousel' === $settings['layout_style']);

        while( $query->have_posts() ) : $query->the_post();
            $oi = $query->current_post + 1;
            $parity_class = ($oi % 2 !== 0) ? 'odd-item' : 'even-item';
            
            $item_container_tag = $is_carousel ? 'div' : 'article';
            $item_container_classes = $is_carousel ? ['swiper-slide'] : ['sifency-item-style', 'sifency-service-item', $parity_class];
            $item_container_attrs = $animation_manager->get_item_attributes($item_container_classes);
            ?>
            <<?php  ?><?php echo esc_html($item_container_tag); ?> <?php echo wp_kses_post($item_container_attrs); ?>>
                <?php if ($is_carousel) : ?>
                    <article class="sifency-item-style sifency-service-item <?php echo esc_attr($parity_class); ?>">
                <?php endif; ?>

                    <?php if ( has_post_thumbnail() ) : ?>
                        <div class="sifency-service-image"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium_large'); ?></a></div>
                    <?php endif; ?>
                    <h3 class="sifency-service-title sifency-heading-style"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <div class="sifency-content-style sifency-service-excerpt"><?php the_excerpt(); ?></div>
                    <a href="<?php the_permalink(); ?>" class="sifency-service-button sifency-button-style"><?php echo esc_html($settings['read_more_text']); ?><i class="icon-right"></i></a>

                <?php if ($is_carousel) : ?>
                    </article>
                <?php endif; ?>
            </<?php  ?><?php echo esc_html($item_container_tag); ?>>
        <?php endwhile;
        wp_reset_postdata();
    }
}