<?php

namespace SoftMultimediaPlayer;

use Elementor\Plugin;
use Elementor\Controls_Manager;
use Elementor\Widget_Base;

defined( 'ABSPATH' ) || exit();

class Elementor_Player_Widget extends Widget_Base {

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

	public function get_title() {
		return __( 'Multimedia Player', 'soft-multimedia-player' );
	}

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

	public function get_categories() {
		return [ 'basic' ];
	}

	public function get_keywords() {
		return [ 'audio', 'multimedia', 'music', 'soft', 'soft-multimedia-player', 'multimedia player' ];
	}

	public function register_controls() {

		$this->start_controls_section(
			'_section_multimedia_player',
			[
				'label' => __( 'Multimedia Player', 'soft-multimedia-player' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$players = smp_get_players();

		if ( ! empty( $players ) ) {
			$players = wp_list_pluck( $players, 'title', 'id' );
		} else {
			$players = [ '' => __( 'No Player Found', 'soft-multimedia-player' ) ];
		}

		$this->add_control(
			'player_id',
			[
				'label'       => __( 'Select Player', 'soft-multimedia-player' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'default'     => 0,
				'options'     => [ 0 => __( 'Select Player', 'soft-multimedia-player' ) ] + $players,
				'render_type' => 'template',
			]
		);

		//Add align control
		$this->add_control(
			'align',
			[
				'label'   => __( 'Alignment', 'soft-multimedia-player' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => [
					'left'   => [
						'title' => __( 'Left', 'soft-multimedia-player' ),
						'icon'  => 'eicon-text-align-left',
					],
					'center' => [
						'title' => __( 'Center', 'soft-multimedia-player' ),
						'icon'  => 'eicon-text-align-center',
					],
					'right'  => [
						'title' => __( 'Right', 'soft-multimedia-player' ),
						'icon'  => 'eicon-text-align-right',
					],
				],
				'default' => 'center',
			]
		);


		$this->end_controls_section();
	}


	public static function empty_placeholder() {
		$shortcodes = smp_get_players();

		?>

        <div class="smp-placeholder">

            <div class="smp-icon">
                <img src="<?php echo esc_url( SOFT_MULTIMEDIA_PLAYER_ASSETS . '/images/multimedia-player-logo.png' ); ?>">
            </div>

            <h2 class="title"><?php esc_html_e( 'Multimedia Player', 'soft-multimedia-player' ); ?></h2>

            <h3 class="subtitle"><?php esc_html_e( 'Insert Existing Player', 'soft-multimedia-player' ); ?></h3>

            <p class="description"><?php esc_html_e( 'Choose a saved player to insert.', 'soft-multimedia-player' ); ?></p>

            <select class="form-control player_id" name="player_id">
                <option><?php esc_html_e( 'Select a player', 'soft-multimedia-player' ) ?></option>

				<?php
				foreach ( $shortcodes as $shortcode ) : ?>
                    <option
                            value="<?php echo esc_attr( $shortcode['id'] ); ?>"
                            data-image="<?php echo esc_url( SOFT_MULTIMEDIA_PLAYER_ASSETS . '/images/modules/' . $shortcode['type'] . '.svg' ); ?>">
						<?php echo esc_html( $shortcode['title'] ); ?>
                    </option>
				<?php endforeach; ?>
            </select>
        </div>
		<?php
	}

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

		$id                = ! empty( $settings['player_id'] ) ? $settings['player_id'] : 0;
		$align             = ! empty( $settings['align'] ) ? $settings['align'] : 'center';
		$is_editor         = Plugin::$instance->editor->is_edit_mode();
		$shortcode_content = sprintf( '<div class="align%s">%s</div>', $align, do_shortcode( "[multimedia_player id=$id]" ) );

		if ( $is_editor ) {
			if ( $id ) {
				echo $shortcode_content;
			} else {
				$this->empty_placeholder();
			}
		} else {
			echo $shortcode_content;
		}
	}
}
