<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class IDSC_Elementor_Gallery extends \Elementor\Widget_Base {

    public function get_name() {
        return 'idsc_gallery';
    }

    public function get_title() {
        return __( 'Gallery', 'idsc' );
    }

    public function get_icon() {
        return 'eicon-gallery-grid';
    }

    public function get_categories() {
        return array( 'idsc' );
    }

    public function get_keywords() {
        return array( 'gallery', 'images', 'grid', 'photos' );
    }

    protected function register_controls() {
        $this->start_controls_section( 'section_content', array(
            'label' => __( 'Gallery', 'idsc' ),
        ) );

        $this->add_control( 'images', array(
            'label'   => __( 'Images', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::GALLERY,
            'default' => array(),
        ) );

        $this->add_control( 'columns', array(
            'label'   => __( 'Columns', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => '3',
            'options' => array(
                '1' => '1',
                '2' => '2',
                '3' => '3',
                '4' => '4',
                '5' => '5',
                '6' => '6',
            ),
        ) );

        $this->add_control( 'gap', array(
            'label'   => __( 'Gap (px)', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SLIDER,
            'range'   => array( 'px' => array( 'min' => 0, 'max' => 50 ) ),
            'default' => array( 'size' => 10 ),
        ) );

        $this->add_control( 'aspect_ratio', array(
            'label'   => __( 'Aspect Ratio', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'original',
            'options' => array(
                'original' => __( 'Original', 'idsc' ),
                'square'   => __( 'Square', 'idsc' ),
                '4:3'      => '4:3',
                '16:9'     => '16:9',
            ),
        ) );

        $this->add_control( 'border_radius', array(
            'label'   => __( 'Border Radius (px)', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SLIDER,
            'range'   => array( 'px' => array( 'min' => 0, 'max' => 50 ) ),
            'default' => array( 'size' => 0 ),
        ) );

        $this->add_control( 'link_to', array(
            'label'   => __( 'Link To', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'none',
            'options' => array(
                'none'       => __( 'None', 'idsc' ),
                'media'      => __( 'Media File', 'idsc' ),
                'attachment' => __( 'Attachment Page', 'idsc' ),
            ),
        ) );

        $this->add_control( 'captions', array(
            'label'        => __( 'Show Captions', 'idsc' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'label_on'     => __( 'Yes', 'idsc' ),
            'label_off'    => __( 'No', 'idsc' ),
            'return_value' => 'yes',
            'default'      => '',
        ) );

        $this->end_controls_section();
    }

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

        $ids = array();
        if ( ! empty( $s['images'] ) && is_array( $s['images'] ) ) {
            foreach ( $s['images'] as $image ) {
                if ( ! empty( $image['id'] ) ) {
                    $ids[] = $image['id'];
                }
            }
        }

        $atts  = ' ids="' . esc_attr( implode( ',', $ids ) ) . '"';
        $atts .= ' columns="' . esc_attr( $s['columns'] ) . '"';
        $atts .= ' gap="' . esc_attr( $s['gap']['size'] ?? 10 ) . '"';
        $atts .= ' aspect_ratio="' . esc_attr( $s['aspect_ratio'] ) . '"';
        $atts .= ' border_radius="' . esc_attr( $s['border_radius']['size'] ?? 0 ) . '"';
        $atts .= ' link_to="' . esc_attr( $s['link_to'] ) . '"';
        $atts .= $s['captions'] === 'yes' ? ' captions="yes"' : '';

        echo do_shortcode( '[idsc_gallery' . $atts . ']' );
    }
}
