<?php

namespace Elementor;

use RivaxStudio\Traits\Rivax_Group_Control_Query;
use RivaxStudio\Traits\Rivax_Global_Widget_Controls;
use RivaxStudio\Traits\Rivax_Post_skin_base;

if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly


class Rivax_Post_Stellar_Widget extends Widget_Base {

    use Rivax_Group_Control_Query;
    use Rivax_Global_Widget_Controls;
    use Rivax_Post_skin_base;

    public function get_name() {
        return 'rivax-post-stellar';
    }

    public function get_title() {
        return esc_html__('Post Stellar', 'newsfy');
    }

    public function get_icon() {
        return 'eicon-image-rollover rivax-e-icon';
    }

    protected function get_html_wrapper_class() {
        return $this->get_name() . '-widget';
    }

    public function get_categories() {
        return ['rivax-elements'];
    }


    protected function register_layout_controls() {

        $this->start_controls_section(
            'section_layout',
            [
                'label' => esc_html__( 'Layout', 'newsfy' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );


        $this->add_responsive_control(
            'columns',
            [
                'label'              => esc_html__( 'Columns', 'newsfy' ),
                'type'               => Controls_Manager::SELECT,
                'default'            => '3',
                'options'            => [
                    '1' => '1',
                    '2' => '2',
                    '3' => '3',
                    '4' => '4',
                    '5' => '5',
                    '6' => '6',
                ],
                'selectors' => [
                    '{{WRAPPER}} .post-item' => 'width: calc(100% / {{SIZE}});',
                ],
            ]
        );

        $this->end_controls_section();

    }


    protected function register_post_controls() {

        $this->start_controls_section(
            'section_post_settings',
            [
                'label' => esc_html__( 'Post Settings', 'newsfy' ),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'content_position',
            [
                'label'     => esc_html__( 'Content Position', 'newsfy' ),
                'type'      => Controls_Manager::SELECT,
                'default'   => 'bottom',
                'options'   => [
                    'center'      => esc_html__( 'Center', 'newsfy' ),
                    'bottom'      => esc_html__( 'Bottom', 'newsfy' ),
                    'relative'    => esc_html__( 'Auto', 'newsfy' ),
                ],
                'render_type'    => 'template',
                'prefix_class' => 'content-',
            ]
        );

        $this->add_control(
            'content_style',
            [
                'label'     => esc_html__( 'Content Style', 'newsfy' ),
                'type'      => Controls_Manager::SELECT,
                'options'   => [
                    'style-1'     => esc_html__( 'Style 1', 'newsfy' ),
                    'style-2'     => esc_html__( 'Style 2', 'newsfy' ),
                    'style-3'     => esc_html__( 'Style 3', 'newsfy' ),
                    'style-4'     => esc_html__( 'Style 4', 'newsfy' ),
                    'style-5'     => esc_html__( 'Style 5', 'newsfy' ),
                    'style-6'     => esc_html__( 'Style 6', 'newsfy' ),
                ],
                'default'   => 'style-1',
            ]
        );

        $this->add_control(
            'read_more_text',
            [
                'label'       => esc_html__( 'Read More Text', 'newsfy' ),
                'type'        => Controls_Manager::TEXT,
                'default'     => esc_html__( 'Read More', 'newsfy' ),
                'placeholder' => esc_html__( 'Read More', 'newsfy' ),
                'condition' => [
                    'content_style' => 'style-3',
                ],
            ]
        );


        $this->add_control(
            'align_content',
            [
                'label'     => esc_html__( 'Content Alignment', 'newsfy' ),
                'type'      => Controls_Manager::CHOOSE,
                'options'   => [
                    'left'   => [
                        'title' => esc_html__( 'Left', 'newsfy' ),
                        'icon'  => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => esc_html__( 'Center', 'newsfy' ),
                        'icon'  => 'eicon-text-align-center',
                    ],
                    'right'  => [
                        'title' => esc_html__( 'Right', 'newsfy' ),
                        'icon'  => 'eicon-text-align-right',
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .content-wrapper-inner' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
             'item_height',
            [
            'label'     => esc_html__( 'Item Height', 'newsfy' ),
            'type'      => Controls_Manager::SLIDER,
            'size_units'=> [ 'px', 'vw', 'vh' ],
			'range'     => [
				'px' => [
					'min' => 50,
					'max' => 800,
				],
				'vw' => [
					'min' => 10,
					'max' => 100,
				],
				'vh' => [
					'min' => 10,
					'max' => 100,
				],
			],
            'selectors' => [
                '{{WRAPPER}} .post-wrapper' => 'height: {{SIZE}}{{UNIT}};',
            ],
            'condition' => [
                'content_position!' => 'relative',
            ],
        ]
        );

        $this->add_responsive_control(
            'posts_container_max_width',
            [
                'label'     => esc_html__( 'Posts Container Max Width', 'newsfy' ),
                'type'      => Controls_Manager::SLIDER,
                'size_units'=> [ 'px', '%' ],
                'range'     => [
                    'px' => [
                        'min' => 400,
                        'max' => 1000,
                    ],
                    '%' => [
                        'min' => 40,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .posts-wrapper' => 'max-width: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Image_Size::get_type(),
            [
            'name' => 'thumbnail',
            'exclude' => ['custom'],
            'default' => 'rivax-wide',
			'separator' => 'before',
        ]);

        $this->add_control(
            'skip_lazy_loading',
            [
                'label'     => esc_html__( 'Skip Lazy Loading', 'newsfy' ),
                'description' => sprintf(esc_html__('Use this to remove lazy loading from %s Largest Contentful Paint (LCP) %s image.', 'newsfy'), '<a href="' . esc_url('https://gtmetrix.com/dont-lazy-load-lcp-image.html') . '" target="_blank">', '</a>'),
                'type'      => Controls_Manager::SELECT,
                'options'   => [
                    '0'     => esc_html__( 'No', 'newsfy' ),
                    '1'     => esc_html__( '1 Image', 'newsfy' ),
                ],
            ]
        );

        $this->register_title_controls();


        $this->register_terms_controls();

	    $this->add_control(
		    'terms_position',
		    [
			    'label'     => esc_html__( 'Terms Position', 'newsfy' ),
			    'type'      => Controls_Manager::SELECT,
			    'options'   => [
				    'top'     => esc_html__( 'Top', 'newsfy' ),
				    'inline'  => esc_html__( 'Inline', 'newsfy' ),
			    ],
			    'default'   => 'inline',
			    'condition' => [
				    'show_terms' => 'yes',
			    ],
		    ]
	    );

	    $this->register_author_controls();

	    $this->add_control(
		    'author_position',
		    [
			    'label'     => esc_html__( 'Author Position', 'newsfy' ),
			    'type'      => Controls_Manager::SELECT,
			    'options'   => [
				    'top'     => esc_html__( 'Top', 'newsfy' ),
				    'inline'  => esc_html__( 'Inline', 'newsfy' ),
			    ],
			    'default'   => 'inline',
			    'condition' => [
				    'show_author' => 'yes',
			    ],
		    ]
	    );


        $this->register_date_controls();

	    $this->register_comments_controls();

	    $this->register_views_count_controls();

	    $this->register_reading_time_controls();

	    $this->register_excerpt_controls();

        $this->add_control('link_wrapper', [
            'label' => esc_html__('Item Wrapper Link', 'newsfy'),
            'description' => esc_html__('Add link to whole item.', 'newsfy'),
            'type' => Controls_Manager::SWITCHER,
            'separator' => 'before',
        ]);

        $this->end_controls_section();

    }


    protected function register_style_post_controls() {

	    $this->start_controls_section('section_style_container',
		    [
			    'label' => esc_html__('Container', 'newsfy'),
			    'tab' => Controls_Manager::TAB_STYLE,
		    ]
	    );

        $this->add_responsive_control('container_padding',
            [
                'label' => esc_html__('Padding', 'newsfy'),
                'type' => Controls_Manager::DIMENSIONS,
                'selectors' => [
                    '{{WRAPPER}} .rivax-stellar-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]);

	    $this->add_group_control(
		    Group_Control_Background::get_type(),
		    [
			    'name' => 'images_overlay_color',
			    'label' => esc_html__('Overlay', 'newsfy'),
			    'exclude' => [ 'image' ],
			    'selector' => '{{WRAPPER}} .images-wrapper::after',
		    ]
	    );

	    $this->add_group_control(
		    Group_Control_Border::get_type(),
		    [
			    'name' => 'container_border',
			    'selector' => '{{WRAPPER}} .rivax-stellar-wrapper',
		    ]);

        $this->add_control('container_border_color_dark_mode',
            [
                'label' => esc_html__('Border Color - Dark Mode', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'condition' => [
                    'container_border_border!' => '',
                ],
                'selectors' => [
                    'html[scheme="dark"] {{WRAPPER}} .rivax-stellar-wrapper' => 'border-color: {{VALUE}};',
                ],
            ]);

	    $this->add_responsive_control('container_border_radius',
		    [
			    'label' => esc_html__('Border Radius', 'newsfy'),
			    'type' => Controls_Manager::DIMENSIONS,
			    'selectors' => [
				    '{{WRAPPER}} .rivax-stellar-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			    ],
		    ]);

	    $this->add_group_control(Group_Control_Box_Shadow::get_type(),
		    [
			    'name' => 'container_box_shadow',
			    'selector' => '{{WRAPPER}} .rivax-stellar-wrapper',
		    ]);

	    $this->end_controls_section();



        $this->start_controls_section('section_style_post',
            [
            'label' => esc_html__('Items', 'newsfy'),
            'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

	    $this->add_responsive_control('top_content_padding',
		    [
			    'label' => esc_html__('Top Content Padding', 'newsfy'),
			    'type' => Controls_Manager::DIMENSIONS,
			    'selectors' => [
				    '{{WRAPPER}} .post-wrapper .top-content-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			    ],
		    ]);

	    $this->add_responsive_control('content_padding',
		    [
			    'label' => esc_html__('Content Padding', 'newsfy'),
			    'type' => Controls_Manager::DIMENSIONS,
			    'selectors' => [
				    '{{WRAPPER}} .post-wrapper .content-wrapper-inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			    ],
		    ]);

	    $this->add_responsive_control('content_margin',
		    [
			    'label' => esc_html__('Content Margin', 'newsfy'),
			    'type' => Controls_Manager::DIMENSIONS,
			    'selectors' => [
				    '{{WRAPPER}} .post-wrapper .content-wrapper-inner' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			    ],
		    ]);


        $this->add_control('item_content_background',
            [
                'label' => esc_html__('Content Background', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .content-wrapper-inner' => 'background: {{VALUE}};',
                ],
        ]);

        $this->add_control('read_more_text_color',
            [
                'label' => esc_html__('Read More Color', 'newsfy'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .post-wrapper .read-more' => 'color: {{VALUE}};',
                ],
                'condition' => [
                    'content_style' => 'style-3',
                ],
            ]);

        $this->add_group_control(
             Group_Control_Border::get_type(),
            [
            'name' => 'item_border',
            'selector' => '{{WRAPPER}} .post-wrapper',
        ]);

        $this->end_controls_section();

    }


    protected function register_controls() {

        $this->register_layout_controls();
        $this->register_query_builder_controls();
        $this->register_post_controls();

        $this->register_style_post_controls();
        $this->register_style_title_controls();
        $this->register_style_terms_controls();
        $this->register_style_meta_controls();
	    $this->register_style_excerpt_controls();

    }



    protected function render()
    {
        $settings = $this->get_settings_for_display();

        $widget_path_name = str_replace('rivax-', '', $this->get_name() );
        include RIVAX_THEME_DIR . '/elementor/templates/' . $widget_path_name . '/' . $widget_path_name . '.php';
    }

}