<?php
if (!defined('ABSPATH')) {
    exit;
}
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
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_Animation_Manager;
use Sifency\Traits\Sifency_Event_Controls;

class Sifency_Addons_Widget_Event 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_Event_Controls;

    public function get_name() {
        return 'sifency-addons-event';
    }
    
    public function get_title() {
        return esc_html__('Sifency Event', 'sifency-addons');
    }
    
    public function get_icon() {
        return 'eicon-calendar';
    }
    
    public function get_categories() {
        return ['sifency-advance-widgets'];
    }

     public function get_style_depends() {
	    return [ 'sifency-typo-style' , 'sifency-frontend-style' , 'sifency-icon', 'sifency-events-style' ];
    }

    public function get_script_depends() {
        return [ 'sifency-frontend-script' ];
    } 

    protected function register_controls() {
        $this->add_sifency_event_controls();
        $this->add_sifency_item_style_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Event Heading',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Event Content',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'meta_style',
            'label'    => 'Event Meta',
            'selector' => '.sifency-meta-style',
            'target_children' => false,
        ]);
        $this->add_sifency_animation_controls();
        $this->add_sifency_button_style_controls([
			'section_label' => esc_html__( 'Button Container', 'sifency-addons' ),
		]);
        Sifency_Elementor_Parity_Controls::register( $this, [
            'selectors' => [
                'odd_item'  => '{{WRAPPER}} .odd-item',
                'even_item' => '{{WRAPPER}} .even-item',
                'title'     => '.sifency-event-item__title a',
            ],
        ] );
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_classes = ['sifency-event-wrapper', 'sifency-event-grid'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        ?>
        <div <?php 
            
            echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php
            if ( 'posts' === $settings['source'] ) {
                $this->render_posts_content($settings, $animation_manager);
            } else {
                $this->render_custom_content($settings, $animation_manager);
            }
            ?>
        </div>
        <?php
    }

    private function render_custom_content($settings, Sifency_Animation_Manager $animation_manager) {
        if ( empty($settings['event_list']) ) { return; }
        
        foreach ($settings['event_list'] as $index => $item) {
            $oi = $index + 1;
            $parity_class = ($oi % 2 !== 0) ? 'odd-item' : 'even-item';
            
            $tag = 'div';
            $link_key = 'link_' . $index;
            $this->remove_render_attribute($link_key);
            if ( ! empty($item['link']['url']) ) {
                $tag = 'a';
                $this->add_link_attributes($link_key, $item['link']);
            }
            $item_classes = ['sifency-item-style', 'sifency-event-item', $parity_class];
            $item_attrs = $animation_manager->get_item_attributes($item_classes);
            ?>
            <<?php echo esc_html($tag); ?> <?php echo wp_kses_post ($this->get_render_attribute_string($link_key) . ' ' . $item_attrs); ?>>
                <div class="sifency-event-item__content">
                    <h3 class="sifency-event-item__title sifency-heading-style"><?php echo esc_html($item['title']); ?></h3>
                    <div class="sifency-event-item__meta">
                        <?php if ($item['date']) : ?><span class="sifency-event-item__date sifency-meta-style"><?php echo esc_html($item['date']); ?></span><?php endif; ?>
                        <?php if ($item['location']) : ?><span class="sifency-event-item__location sifency-meta-style"><?php echo esc_html($item['location']); ?></span><?php endif; ?>
                    </div>
                    <div class="sifency-event-description">
                        <?php if ($item['excerpt']) : ?><span class="sifency-content-style"><?php echo wp_kses_post($item['excerpt']); ?></span><?php endif; ?>
                    </div>
                </div>
                <div class="sifency-event-item__button-wrap">
                    <span class="sifency-event-item__button sifency-button-style"><?php echo esc_html($settings['read_more_text']); ?><i class="icon-right"></i></span>
                </div>
            </<?php echo esc_html($tag); ?>>
            <?php
        }
    }

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

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

        while( $query->have_posts() ) : $query->the_post();
            $oi = $query->current_post + 1;
            $parity_class = ($oi % 2 !== 0) ? 'odd-item' : 'even-item';
            $meta = get_post_meta(get_the_ID(), 'event_single_metabox', true) ?: [];
            $date = $meta['event_start_date'] ?? get_the_date();
            $location = $meta['event_location'] ?? '';
            $item_classes = ['sifency-item-style', 'sifency-event-item', $parity_class];
            $item_attrs = $animation_manager->get_item_attributes($item_classes);
            ?>
            <a href="<?php the_permalink(); ?>" <?php echo wp_kses_post ($item_attrs); ?>>
                <div class="sifency-event-item__content">
                    <h3 class="sifency-event-item__title sifency-heading-style"><?php the_title(); ?></h3>
                    <div class="sifency-event-item__meta">
                        <?php if ($date) : ?><span class="sifency-event-item__date sifency-meta-style"><?php echo esc_html($date); ?></span><?php endif; ?>
                        <?php if ($location) : ?><span class="sifency-event-item__location sifency-meta-style"><?php echo esc_html($location); ?></span><?php endif; ?>
                    </div>
                </div>
                <div class="sifency-event-item__button-wrap">
                    <span class="sifency-event-item__button sifency-button-style"><?php echo esc_html($settings['read_more_text']); ?> <i class="icon-right"></i></span>
                </div>
            </a>
        <?php endwhile;
        wp_reset_postdata();
    }
}