<?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 \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_Animation_Manager;
use Sifency\Traits\Sifency_Projects_Controls;

class Sifency_Addons_Widget_Projects 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_Projects_Controls;

    public function get_name() {
        return 'sifency-addons-projects';
    }
    
    public function get_title() {
        return esc_html__('Sifency Portfolio', 'sifency-addons');
    }
    
    public function get_icon() {
        return 'eicon-gallery-grid';
    }
    
    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-portfolio-style', 'sifency-icon' ];
    }

    protected function register_controls() {
        $carousel_condition = [
            'condition' => [
                'layout_style' => 'carousel'
            ]
        ];
        $this->add_sifency_projects_controls();
        $this->add_sifency_carousel_controls($carousel_condition);
        $this->add_sifency_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Project Heading',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Project Content',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_button_style_controls();
        Sifency_Elementor_Parity_Controls::register( $this );
        $this->add_sifency_animation_controls();
    }
    
    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $this->add_render_attribute('_wrapper', 'class', 'sifency-portfolio-wrapper');
        $animation_attrs = $animation_manager->get_wrapper_attributes();
        if ( ! empty($animation_attrs) ) {
            preg_match('/class="([^"]+)"/', $animation_attrs, $class_matches);
            if (isset($class_matches[1])) { $this->add_render_attribute('_wrapper', 'class', $class_matches[1]); }
        }
        $is_carousel = ('carousel' === $settings['layout_style']);
        if ($is_carousel) {
            $this->add_render_attribute('layout_container', 'class', 'swiper-container');
            
            $slide_count = 0;
            if ('posts' === $settings['source']) {
                $query_args = method_exists($this, 'get_sifency_query_args') ? $this->get_sifency_query_args() : [];
                $query_args['post_type'] = $settings['query_post_type'] ?? 'portfolio';
                $query_args['posts_per_page'] = $settings['posts_per_page'] ?? -1;
                $query = new \WP_Query( $query_args );
                $slide_count = $query->post_count;
            } else {
                $slide_count = count($settings['project_list'] ?? []);
            }
            
            $slider_options = $this->get_sifency_swiper_options($settings, $slide_count);
            $this->add_render_attribute('_wrapper', 'class', 'sifency-carousel-wrapper');
            $this->add_render_attribute('_wrapper', 'data-swiper-options', wp_json_encode($slider_options));
        } else {
            $this->add_render_attribute('layout_container', 'class', 'sifency-portfolio--' . $settings['layout_style']);
        }

        ?>
        <div <?php echo wp_kses_post ($this->get_render_attribute_string('layout_container')); ?>>
            <?php if ($is_carousel) { echo '<div class="swiper-wrapper">'; } ?>

            <?php
            if ( 'posts' === $settings['source'] ) {
                $this->render_posts_content($settings, $animation_manager, $query ?? null);
            } 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>
        <?php
    }

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

        foreach ($settings['project_list'] as $index => $item) {
            $oi = $index + 1;
            $parity_class = ($oi % 2 !== 0) ? 'odd-item' : 'even-item';
            
            $link_key = 'link_'.$index;
            $this->remove_render_attribute($link_key);
            $this->add_link_attributes($link_key, $item['link']);
            $item_container_tag = $is_carousel ? 'div' : 'article';
            $item_container_classes = $is_carousel ? ['swiper-slide'] : ['sifency-item-style', 'sifency-portfolio-item', $parity_class];
            $item_container_attrs = $animation_manager->get_item_attributes($item_container_classes);
            ?>
            <<?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-portfolio-item <?php echo esc_attr($parity_class); ?>">
                <?php endif; ?>

                    <?php if ( ! empty($item['image']['url']) ) : ?>
                        <div class="sifency-portfolio-image">
                            <?php  ?>
                            <a <?php echo wp_kses_post($this->get_render_attribute_string($link_key)); ?>>
                                <?php  ?>
                                <?php echo wp_kses_post(\Elementor\Group_Control_Image_Size::get_attachment_image_html( $item, 'thumbnail', 'image' )); ?>
                            </a>
                        </div>
                    <?php endif; ?>
                    <div class="sifency-portfolio-content">
                        <h3 class="sifency-portfolio-title sifency-heading-style"><a <?php echo wp_kses_post($this->get_render_attribute_string($link_key)); ?><?php  ?>><?php echo esc_html($item['title']); ?></a></h3>
                        <?php if ( ! empty($item['category']) ) : ?>
                            <div class="sifency-portfolio-categories sifency-content-style"><?php echo esc_html($item['category']); ?></div>
                        <?php endif; ?>
                        <?php if ( 'yes' === $settings['show_read_more'] && ! empty( $settings['read_more_text'] ) ) : ?>
                            <a <?php  ?> <?php echo wp_kses_post($this->get_render_attribute_string($link_key)); ?> class="sifency-button-style">
                                <?php echo esc_html( $settings['read_more_text'] ); ?>
                                <i class="icon-right"></i>
                            </a>
                        <?php endif; ?>
                    </div>

                <?php if ($is_carousel) : ?>
                    </article>
                <?php endif; ?>
            </<?php  ?><?php echo esc_html ($item_container_tag); ?>>
            <?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'] ?? 'portfolio';
            $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-portfolio-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-portfolio-item <?php echo esc_attr($parity_class); ?>">
                <?php endif; ?>

                    <?php if ( has_post_thumbnail() ) : ?>
                        <div class="sifency-portfolio-image">
                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( $settings['thumbnail_size'] ?? 'medium' ); ?></a>
                        </div>
                    <?php endif; ?>
                    <div class="sifency-portfolio-content">
                        <h3 class="sifency-portfolio-title sifency-heading-style"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <div class="sifency-portfolio-categories sifency-content-style">
                            <?php the_terms( get_the_ID(), 'portfolio_category', '', ', ' ); ?>
                        </div>
                        <?php if ( 'yes' === $settings['show_read_more'] && ! empty( $settings['read_more_text'] ) ) : ?>
                            <a href="<?php the_permalink(); ?>" class="sifency-button-style">
                                <?php echo esc_html( $settings['read_more_text'] ); ?>
                                <i class="icon-right"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                    
                <?php if ($is_carousel) : ?>
                    </article>
                <?php endif; ?>
            </<?php  ?><?php echo esc_html($item_container_tag); ?>>
            <?php
        endwhile;
        wp_reset_postdata();
    }
}