<?php

namespace Unlockafe_addons\Widgets;

use Unlockafe_addons\Traits\Utils as UnlockafeUtils;
use Unlockafe_addons\Traits\Widget_helper;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Group_Control_Border;
use \Elementor\Repeater;

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

/**
 * Footer Instagram Widget
 *
 * @since 1.0.0
 */
class Footer_Instagram extends Widget_Base
{

    use Widget_helper, UnlockafeUtils;

    /**
     * Retrieve the widget name.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget name.
     */
    public function get_name()
    {
        return 'unlockafe-footer-instagram';
    }

    /**
     * Retrieve the widget title.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title()
    {
        return __('WPRealizer - Instagram Feed', 'unlock-addons-for-elementor');
    }

    /**
     * Retrieve the widget icon.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon()
    {
        return 'eicon-instagram-gallery ulicon';
    }

    /**
     * Retrieve the list of categories the widget belongs to.
     *
     * Used to determine where to display the widget in the editor.
     *
     * Note that currently Elementor supports only one category.
     * When multiple categories passed, Elementor uses the first one.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget categories.
     */
    public function get_categories()
    {
        return ['unlockafe-addons'];
    }

    /**
     * Retrieve the list of scripts the widget depended on.
     *
     * Used to set scripts dependencies required to run the widget.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget scripts dependencies.
     */
    public function get_script_depends()
    {
        return [];
    }

    public function get_style_depends()
    {
        return ['unlockafe-global'];
    }

    /**
     * Register the widget controls.
     *
     * Adds different input fields to allow the user to change and customize the widget settings.
     *
     * @since 1.0.0
     *
     * @access protected
     */

    protected function register_controls()
    {
        $this->register_controls_section();
        $this->style_tab_content();
    }

    protected function register_controls_section()
    {


        $this->start_controls_section(
            'wpr_instagram_feed_section_layout',
            [
                'label' => esc_html__('Layout', 'unlock-addons-for-elementor'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'wpr_design_style',
            [
                'label' => esc_html__('Design Style', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'layout-1' => esc_html__('Layout 1', 'unlock-addons-for-elementor'),
                    'layout-2' => esc_html__('Layout 2', 'unlock-addons-for-elementor'),
                    // 'layout-3' => esc_html__('Layout 3', 'unlock-addons-for-elementor'),
                ],
                'default' => 'layout-1',
            ]
        );

        $this->end_controls_section();


        $this->start_controls_section(
            'wpr_instagram_feed_section',
            [
                'label' => esc_html__('Instagram Feed', 'unlock-addons-for-elementor'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $repeater = new \Elementor\Repeater();

        $repeater->add_control(
            'repeater_condition',
            [
                'label' => __('Field Condition', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'style_1' => __('Style 1', 'unlock-addons-for-elementor'),
                    'style_2' => __('Style 2', 'unlock-addons-for-elementor'),
                    'style_3' => __('Style 3', 'unlock-addons-for-elementor'),
                ],
                'default' => 'style_1',
                'frontend_available' => true,
                'style_transfer' => true,
            ]
        );

        $repeater->add_control(
            'wpr_instagram_feed_image',
            [
                'label' => esc_html__('Instagram Image', 'unlock-addons-for-elementor'),
                'type'  => Controls_Manager::MEDIA,
                'default' => [
                    'url' => \Elementor\Utils::get_placeholder_image_src(),
                ],
                'condition' => [
                    'repeater_condition' => 'style_1',
                ],
            ]
        );

        $repeater->add_control(
            'wpr_instagram_feed_url',
            [
                'label'   => esc_html__('Instagram URL', 'unlock-addons-for-elementor'),
                'type'    => \Elementor\Controls_Manager::URL,
                'default' => [
                    'url'         => '#',
                    'is_external' => false,
                    'nofollow'    => false,
                ],
                'placeholder' => esc_html__('Enter Instagram URL', 'unlock-addons-for-elementor'),
                'label_block' => true,
            ]
        );

        $this->add_control(
            'wpr_instagram_feed_list',
            [
                'label'       => esc_html__('Instagram Images', 'unlock-addons-for-elementor'),
                'type'        => Controls_Manager::REPEATER,
                'fields'      => $repeater->get_controls(),
                'default'     => [
                    [
                        'wpr_instagram_feed_image' => ['url' => \Elementor\Utils::get_placeholder_image_src()],
                        'wpr_instagram_feed_url'   => ['url' => '#'],
                    ],
                    [
                        'wpr_instagram_feed_image' => ['url' => \Elementor\Utils::get_placeholder_image_src()],
                        'wpr_instagram_feed_url'   => ['url' => '#'],
                    ],
                    [
                        'wpr_instagram_feed_image' => ['url' => \Elementor\Utils::get_placeholder_image_src()],
                        'wpr_instagram_feed_url'   => ['url' => '#'],
                    ],
                ],
                'title_field' => '{{{ wpr_instagram_feed_url.url }}}',
            ]
        );

        $this->end_controls_section();
    }

    // style_tab_content
    protected function style_tab_content()
    {
        $this->tp_link_controls_style('wpr_instagram_feed', 'Link Style', '.wpr-el-footer-social a');
    }

    /**
     * Render the widget output on the frontend.
     *
     * Written in PHP and used to generate the final HTML.
     *
     * @since 1.0.0
     *
     * @access protected
     */
    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $btn_id = 'theme_btn';

?>

        <?php if ($settings['wpr_design_style'] == 'layout-2') : ?>

            <div class="footer__top-social wpr-el-footer-social">
                <ul>
                    <?php foreach ($settings['wpr_instagram_feed_list'] as $menu) :
                        $link = !empty($menu['wpr_instagram_feed_url']['url']) ? $menu['wpr_instagram_feed_url']['url'] : '';
                        $target = !empty($menu['wpr_instagram_feed_url']['is_external']) ? '_blank' : '';
                        $rel = !empty($menu['wpr_instagram_feed_url']['nofollow']) ? 'nofollow' : '';
                    ?>
                        <li>
                            <a target="<?php echo esc_attr($target); ?>" rel="<?php echo esc_attr($rel); ?>" href="<?php echo esc_url($link); ?>">
                                <?php if (!empty($menu['wpr_instagram_feed_image']['url'])) : ?>
                                    <img src="<?php echo esc_url($menu['wpr_instagram_feed_image']['url']); ?>" alt="<?php echo esc_attr(get_post_meta(attachment_url_to_postid($menu['wpr_instagram_feed_image']['url']), '_wp_attachment_image_alt', true)); ?>">
                                <?php endif; ?>
                                <i class="fa-solid fa-arrow-up-right"></i>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>

        <?php elseif ($settings['wpr_design_style'] == 'layout-3') : ?>

            <!-- Layout 3 can be added here if needed -->

        <?php else : ?>

            <!-- footer area start  -->
            <footer class="footer-area">
                <div class="footer-widget-wrapper-box">
                    <div class="footer-widget-wrapper">
                        <div class="footer-widget-box">
                            <div class="instagram-posts">
                                <?php foreach ($settings['wpr_instagram_feed_list'] as $item) :
                                    $link = !empty($item['wpr_instagram_feed_url']['url']) ? esc_url($item['wpr_instagram_feed_url']['url']) : '#';
                                    $target = !empty($item['wpr_instagram_feed_url']['is_external']) ? ' target="_blank"' : '';
                                    $rel = !empty($item['wpr_instagram_feed_url']['nofollow']) ? ' rel="nofollow"' : '';
                                ?>
                                    <a href="<?php echo $link; ?>" <?php echo $target . $rel; ?>>
                                        <?php if (!empty($item['wpr_instagram_feed_image']['url'])) : ?>
                                            <img src="<?php echo esc_url($item['wpr_instagram_feed_image']['url']); ?>"
                                                alt="<?php echo esc_attr(get_post_meta(attachment_url_to_postid($item['wpr_instagram_feed_image']['url']), '_wp_attachment_image_alt', true)); ?>">
                                        <?php endif; ?>
                                    </a>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </footer>

            <!-- footer area end  -->

        <?php endif; ?>


<?php

    }
}


