<?php

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

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Repeater;


class Pakrco_Elementor_Amenities extends Elementor\Widget_Base
{

    public function get_name()
    {
        return 'pakrco-amenities';
    }

    public function get_title()
    {
        return esc_html__('Pakrco Amenities', 'pakrco');
    }

    public function get_script_depends()
    {
        return ['pakrco-elementor-amenities', 'slick'];
    }

    public function get_categories()
    {
        return array('pakrco-addons');
    }

    protected function register_controls()
    {

        $this->start_controls_section(
            'section_amenities',
            [
                'label' => esc_html__('Amenities', 'pakrco'),
            ]
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'amenities_title_link',
            [
                'label' => esc_html__('Text Link', 'pakrco'),
                'type' => Controls_Manager::TEXT,
            ]
        );

        $repeater->add_control(
            'amenities_content',
            [
                'label' => esc_html__('Content', 'pakrco'),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $repeater->add_control(
            'amenities_content_sub_title',
            [
                'label' => esc_html__('Sub Title', 'pakrco'),
                'type' => Controls_Manager::TEXT,
            ]
        );

        $repeater->add_control(
            'amenities_content_title',
            [
                'label' => esc_html__('Title', 'pakrco'),
                'type' => Controls_Manager::TEXTAREA,
            ]
        );

        $repeater->add_control(
            'amenities_content_content',
            [
                'label' => esc_html__('Content', 'pakrco'),
                'type' => Controls_Manager::TEXTAREA,
                'rows' => '10',
            ]
        );

        $repeater->add_control(
            'amenities_image',
            [
                'label' => esc_html__('Choose Image', 'pakrco'),
                'type' => Controls_Manager::MEDIA,
                'show_label' => false,
            ]
        );

        $this->add_control(
            'amenities',
            [
                'label' => esc_html__('Items', 'pakrco'),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'title_field' => '{{{ amenities_title_link }}}',
            ]
        );

        $this->add_group_control(
            Elementor\Group_Control_Image_Size::get_type(),
            [
                'name' => 'amenities_image',
                'default' => 'full',
                'separator' => 'none',
            ]
        );

        $this->add_responsive_control(
            'min-height',
            [
                'label' => esc_html__('Height', 'pakrco'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 100,
                        'max' => 1000,
                    ],
                    'vh' => [
                        'min' => 10,
                        'max' => 100,
                    ],
                ],
                'size_units' => ['px', 'vh'],
                'selectors' => [
                    '{{WRAPPER}} .elementor-amenities-item-wrapper' => 'height: {{SIZE}}{{UNIT}}',
                ],
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_text_link',
            [
                'label' => esc_html__('Text Link', 'pakrco'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'text_link_color',
            [
                'label' => esc_html__('Text Color', 'pakrco'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .amenities-link .amenities-title-link' => 'color: {{VALUE}}',
                    '{{WRAPPER}} .amenities-link .amenities-title-link span:before' => 'background-color: {{VALUE}}',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'text_link_typography',
                'selector' => '{{WRAPPER}} .amenities-link .amenities-title-link',
            ]
        );

        $this->add_responsive_control(
            'text_link_alignment',
            [
                'label' => esc_html__('Alignment', 'pakrco'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'flex-start' => [
                        'title' => esc_html__('Left', 'pakrco'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => esc_html__('Center', 'pakrco'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'flex-end' => [
                        'title' => esc_html__('Right', 'pakrco'),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .amenities-link .amenities-title-link' => 'align-items: {{VALUE}}',
                ],
            ]
        );

        $this->add_responsive_control(
            'text_link_padding',
            [
                'label' => esc_html__('Padding', 'pakrco'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .amenities-link .amenities-title-link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_text_content',
            [
                'label' => esc_html__('Text Content', 'pakrco'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'section_text_content_padding',
            [
                'label' => esc_html__('Padding', 'pakrco'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .amenities-content .content-li' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        // Sub Title
        $this->add_control(
            'section_text_content_sub_title_heading',
            [
                'label' => esc_html__('Sub Title', 'pakrco'),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before'
            ]
        );

        $this->add_control(
            'section_text_content_sub_title_color',
            [
                'label' => esc_html__('Text Color', 'pakrco'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .content-li .amenities-content-sub-title' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'sub_title_typography',
                'selector' => '{{WRAPPER}} .content-li .amenities-content-sub-title',
            ]
        );

        $this->add_responsive_control(
            'sub_title_padding',
            [
                'size_units' => ['px', 'em', '%'],
                'label' => esc_html__('Spacing', 'pakrco'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 300,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .content-li .amenities-content-sub-title' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        // Title
        $this->add_control(
            'section_text_content_title_heading',
            [
                'label' => esc_html__('Title', 'pakrco'),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before'
            ]
        );

        $this->add_control(
            'section_text_content_title_color',
            [
                'label' => esc_html__('Text Color', 'pakrco'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .content-li .amenities-content-title' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography',
                'selector' => '{{WRAPPER}} .content-li .amenities-content-title',
            ]
        );

        $this->add_responsive_control(
            'title_padding',
            [
                'size_units' => ['px', 'em', '%'],
                'label' => esc_html__('Spacing', 'pakrco'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 300,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .content-li .amenities-content-title' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        // Title
        $this->add_control(
            'section_text_content_content_heading',
            [
                'label' => esc_html__('Content', 'pakrco'),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before'
            ]
        );

        $this->add_control(
            'section_text_content_content_color',
            [
                'label' => esc_html__('Text Color', 'pakrco'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .content-li .amenities-content-content' => 'color: {{VALUE}}',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'content_typography',
                'selector' => '{{WRAPPER}} .content-li .amenities-content-content',
            ]
        );

        $this->add_responsive_control(
            'content_padding',
            [
                'size_units' => ['px', 'em', '%'],
                'label' => esc_html__('Spacing', 'pakrco'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 300,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .content-li .amenities-content-content' => 'margin-bottom: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    protected function render()
    {

        $settings = $this->get_settings_for_display();

        if (!empty($settings['amenities']) && is_array($settings['amenities'])) {

            $this->add_render_attribute('wrapper', 'class', 'elementor-amenities-item-wrapper');

            ?>
            <div <?php echo pakrco_elementor_get_render_attribute_string('wrapper', $this); // WPCS: XSS ok.
            ?>>
                <div class="amenities-content">
                    <div class="content-ul">
                        <?php $count = 1; ?>
                        <?php foreach ($settings['amenities'] as $index => $items): ?>
                            <?php
                            $content_setting_key = $this->get_repeater_setting_key('content_li', 'content', $index);
                            $this->add_render_attribute($content_setting_key, [
                                'data-setting-key-amenities-content' => 'amenities-' . $count . $this->get_id(),
                                'class' => 'content-li'
                            ]); ?>
                            <div <?php echo pakrco_elementor_get_render_attribute_string($content_setting_key, $this); ?>>
                                <?php if (!empty($items['amenities_content_sub_title'])) : ?>
                                    <div class="amenities-content-sub-title">
                                        <?php echo sprintf('%s', $items['amenities_content_sub_title']); ?>
                                    </div>
                                <?php endif; ?>
                                <?php if (!empty($items['amenities_content_title'])) : ?>
                                    <div class="amenities-content-title">
                                        <?php echo sprintf('%s', $items['amenities_content_title']); ?>
                                    </div>
                                <?php endif; ?>
                                <?php if (!empty($items['amenities_content_content'])) : ?>
                                    <div class="amenities-content-content">
                                        <?php echo sprintf('%s', $items['amenities_content_content']); ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                            <?php $count++; ?>
                        <?php endforeach; ?>
                    </div>
                </div>
                <div class="amenities-image">
                    <?php $count = 1; ?>
                    <?php foreach ($settings['amenities'] as $index => $items): ?>
                        <?php
                        $image_setting_key = $this->get_repeater_setting_key('elementor_amenities_image', 'image', $index);
                        $this->add_render_attribute($image_setting_key, [
                            'data-setting-key-amenities-image' => 'amenities-' . $count . $this->get_id(),
                            'class' => 'elementor-amenities-image'
                        ]); ?>
                        <?php if (!empty($items['amenities_image']['url'])) : ?>
                            <div <?php echo pakrco_elementor_get_render_attribute_string($image_setting_key, $this); ?>>
                                <?php
                                $items['amenities_image_size'] = $settings['amenities_image_size'];
                                $items['amenities_image_custom_dimension'] = $settings['amenities_image_custom_dimension'];
                                echo Group_Control_Image_Size::get_attachment_image_html($items, 'amenities_image');
                                ?>
                            </div>
                        <?php endif; ?>
                        <?php $count++; ?>
                    <?php endforeach; ?>
                </div>
                <div class="amenities-link">
                    <?php $count = 1; ?>
                    <?php foreach ($settings['amenities'] as $index => $items): ?>
                        <?php
                        $link_title_setting_key = $this->get_repeater_setting_key('amenities_title_link', 'link', $index);
                        $this->add_render_attribute($link_title_setting_key, [
                            'data-setting-key-amenities-link' => 'amenities-' . $count . $this->get_id(),
                            'class' => 'amenities-title-link'
                        ]); ?>
                        <?php if (!empty($items['amenities_title_link'])) : ?>
                            <a href="#" <?php echo pakrco_elementor_get_render_attribute_string($link_title_setting_key, $this); ?>>
                                <span><?php echo sprintf('%s', $items['amenities_title_link']); ?></span>
                            </a>
                        <?php endif; ?>
                        <?php $count++; ?>
                    <?php endforeach; ?>
                </div>
            </div>
            <?php
        }

    }

    private function render_image($settings, $amenitie)
    {
        if (!empty($amenitie['amenities_image']['url'])) :
            ?>
            <div class="elementor-amenities-image">
                <?php
                $amenitie['amenities_image_size'] = $settings['amenities_image_size'];
                $amenitie['amenities_image_custom_dimension'] = $settings['amenities_image_custom_dimension'];
                echo Group_Control_Image_Size::get_attachment_image_html($amenitie, 'amenities_image');
                ?>
            </div>
        <?php
        endif;
    }

}

$widgets_manager->register_widget_type(new Pakrco_Elementor_Amenities());
