<?php
namespace Gridpress_Elementor\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * @since 1.1.0
 */
if (! class_exists('Gridpress_Grid_Elementor')) {

    class Gridpress_Grid_Elementor extends Widget_Base {

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

        public function get_title() {
            return esc_html__( 'Grid Shortcode', 'gridpress-grid-layout-design' );
        }

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

        public function get_categories() {
            return array( 'gridpress-grid-layout-design' );
        }

        protected function register_controls() {

            $gridpress_grid_list = array(
                '' => esc_html__( 'Select Grid', 'gridpress-grid-layout-design' ),
            );

            $gridpress_posts = get_posts( array(
                'post_type'      => 'gridpress_grid',
                'posts_per_page' => -1,
            ) );

            if ( ! is_wp_error( $gridpress_posts ) && ! empty( $gridpress_posts ) ) {

                foreach ( $gridpress_posts as $gridpress_post_obj ) {

                    $gridpress_post_id    = absint( $gridpress_post_obj->ID );
                    $gridpress_post_title = get_the_title( $gridpress_post_id );

                    $gridpress_grid_list[ $gridpress_post_id ] = sprintf(
                        '%s (%s)',
                        esc_html( $gridpress_post_title ),
                        sprintf(
                            /* translators: %d: Grid ID. */
                            esc_html__( 'ID: %d', 'gridpress-grid-layout-design' ),
                            absint( $gridpress_post_id )
                        )
                    );
                }
            }

            $this->start_controls_section(
                'gridpress_grid_section',
                array(
                    'label' => esc_html__( 'Grid Settings', 'gridpress-grid-layout-design' ),
                    'tab'   => Controls_Manager::TAB_CONTENT,
                )
            );

            $this->add_control(
                'gridpress_grid_id',
                array(
                    'label'   => esc_html__( 'Select Grid', 'gridpress-grid-layout-design' ),
                    'type'    => Controls_Manager::SELECT2,
                    'default' => '',
                    'options' => $gridpress_grid_list,
                )
            );

            $this->end_controls_section();
        }

        protected function render() {

            $gridpress_settings = $this->get_settings_for_display();

            $gridpress_grid_id = ! empty( $gridpress_settings['gridpress_grid_id'] )
                ? absint( $gridpress_settings['gridpress_grid_id'] )
                : 0;

            echo do_shortcode(
                sprintf(
                    /* translators: %d: GridPress grid ID. */
                    '[gridpress_grid id="%d"]',
                    absint( $gridpress_grid_id )
                )
            );
        }

        protected function content_template() {
            // Elementor live preview (optional)
        }
    }
}