<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Addons_Widget_Audio_Player extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-audio-player',
			'title'          => 'Sifency Audio Player',
			'icon'           => 'eicon-play-o',
			'category'       => array( 'sifency-advance-widgets' ),
			'keywords'       => array( 'audio', 'music', 'playlist', 'player', 'sound', 'mp3', 'ogg' ),
			'script_depends' => array( 'sifency-audio-player-script' ),
			'style_depends'  => array( 'sifency-audio-player-style', 'sifency-typ-style' ),
		);}

	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Audio Content', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'layout_type',
			array(
				'label'   => esc_html__( 'Layout Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'standard',
				'options' => array(
					'standard' => esc_html__( 'Standard (Row)', 'sifency-addons' ),
					'card'     => esc_html__( 'Card (Poster)', 'sifency-addons' ),
					'minimal'  => esc_html__( 'Minimal (Compact)', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'source_type',
			array(
				'label'   => esc_html__( 'Source Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'single',
				'options' => array(
					'single'   => esc_html__( 'Single Track', 'sifency-addons' ),
					'playlist' => esc_html__( 'Playlist', 'sifency-addons' ),
				),
			)
		);

		$single_condition = array( 'source_type' => 'single' );

		$this->add_control(
			'audio_source',
			array(
				'label'      => esc_html__( 'Audio File', 'sifency-addons' ),
				'type'       => Controls_Manager::MEDIA,
				'media_type' => 'audio',
				'dynamic'    => array( 'active' => true ),
				'condition'  => $single_condition,
			)
		);
		$this->add_control(
			'external_url',
			array(
				'label'     => esc_html__( 'Or External URL', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'condition' => array(
					'source_type'       => 'single',
					'audio_source[url]' => '',
				),
			)
		);
		$this->add_control(
			'track_title',
			array(
				'label'     => esc_html__( 'Track Title', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Audio Title', 'sifency-addons' ),
				'condition' => $single_condition,
			)
		);
		$this->add_control(
			'artist_name',
			array(
				'label'     => esc_html__( 'Artist Name', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Artist Name', 'sifency-addons' ),
				'condition' => $single_condition,
			)
		);
		$this->add_control(
			'cover_image',
			array(
				'label'     => esc_html__( 'Cover Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'condition' => array(
					'source_type'  => 'single',
					'layout_type!' => 'minimal',
				),
			)
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'track_title',
			array(
				'label'   => esc_html__( 'Title', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Song Title',
			)
		);
		$repeater->add_control(
			'artist_name',
			array(
				'label'   => esc_html__( 'Artist', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'Artist',
			)
		);
		$repeater->add_control(
			'audio_file',
			array(
				'label'      => esc_html__( 'Audio File', 'sifency-addons' ),
				'type'       => Controls_Manager::MEDIA,
				'media_type' => 'audio',
			)
		);
		$repeater->add_control(
			'external_file',
			array(
				'label' => esc_html__( 'External URL (Optional)', 'sifency-addons' ),
				'type'  => Controls_Manager::TEXT,
			)
		);
		$repeater->add_control(
			'cover_image',
			array(
				'label' => esc_html__( 'Cover Image', 'sifency-addons' ),
				'type'  => Controls_Manager::MEDIA,
			)
		);
		$this->add_control(
			'playlist_items',
			array(
				'label'       => esc_html__( 'Playlist Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'track_title' => 'Track 1',
						'artist_name' => 'Artist 1',
					),
					array(
						'track_title' => 'Track 2',
						'artist_name' => 'Artist 2',
					),
				),
				'title_field' => '{{{ track_title }}}',
				'condition'   => array( 'source_type' => 'playlist' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_settings',
			array(
				'label' => esc_html__( 'Player Settings', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'show_playlist_list',
			array(
				'label'     => esc_html__( 'Show Playlist Queue', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'source_type' => 'playlist' ),
			)
		);
		$this->add_control(
			'show_download',
			array(
				'label'   => esc_html__( 'Show Download Button', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'show_time',
			array(
				'label'   => esc_html__( 'Show Time Duration', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_volume',
			array(
				'label'   => esc_html__( 'Show Volume Control', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'autoplay',
			array(
				'label'   => esc_html__( 'Autoplay', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->add_control(
			'loop',
			array(
				'label'   => esc_html__( 'Loop Track/Playlist', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'           => 'Item Wrapper',
				'selector'        => '.sifency-item-style',
				'hover_selector'  => '.sifency-ap-playlist-item.active',
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
				'condition'       => array( 'source_type' => 'playlist' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'heading_style',
				'label'           => 'Title',
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
				'selector'        => '.sifency-heading-style',
				'hover_selector'  => '.sifency-ap-playlist-item.active:hover .sifency-heading-style',
				'color_only'      => true,
				'exclude'         => array( 'alignment', 'bg', 'radius', 'stroke', 'shadow', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'content_style',
				'label'           => 'Artist',
				'selector'        => '.sifency-content-style',
				'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
				'hover_selector'  => '.sifency-ap-playlist-item.active:hover .sifency-content-style',
				'color_only'      => true,
				'exclude'         => array( 'alignment', 'bg', 'radius', 'stroke', 'shadow', 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'ap_time_style',
				'label'      => 'Time',
				'selector'   => '.sifency-ap-time',
				'color_only' => true,
				'exclude'    => array( 'alignment', 'bg', 'radius', 'stroke', 'shadow', 'transition', 'tabs', 'border' ),
				'condition'  => array( 'show_time' => 'yes' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'sifency_style',
				'label'     => 'Playlist Icons',
				'selector'  => '.sifency-ap-pl-icon',
				'condition' => array( 'source_type' => 'playlist' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'download_style',
				'label'     => 'Download Icon',
				'selector'  => '.sifency-ap-download-btn',
				'condition' => array( 'show_download' => 'yes' ),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'    => 'vol_style',
				'label'     => 'Volume Icon',
				'selector'  => '.sifency-ap-volume-btn',
				'condition' => array( 'show_volume' => 'yes' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'prefix'   => 'play_button_style',
				'label'    => 'Play Button',
				'selector' => '.sifency-ap-play-btn',
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'   => 'cover_style',
				'label'    => 'Cover Image Style',
				'selector' => '.sifency-ap-cover img',
				'exclude'  => array( 'tabs', 'transition' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'prefix'    => 'thumbnail_style',
				'label'     => 'Thumbnail Style',
				'selector'  => '.sifency-ap-pl-thumb img',
				'condition' => array( 'source_type' => 'playlist' ),
				'exclude'   => array( 'tabs', 'transition' ),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id              = esc_attr( $context['dom_id'] );
		$settings                = $this->get_settings_for_display();
		$animation_manager       = new Sifency_Addons_Animation_Manager( $settings );
		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes();

		$tracks = array();

		if ( 'playlist' === $settings['source_type'] && ! empty( $settings['playlist_items'] ) ) {
			foreach ( $settings['playlist_items'] as $item ) {
				$track_url = ! empty( $item['audio_file']['url'] ) ? $item['audio_file']['url'] : $item['external_file'];
				if ( empty( $track_url ) ) {
					continue;
				}

				$img_url = '';
				if ( ! empty( $item['cover_image']['id'] ) ) {
					$img_url = wp_get_attachment_image_url( $item['cover_image']['id'], 'medium' );
				} elseif ( ! empty( $item['cover_image']['url'] ) ) {
					$img_url = $item['cover_image']['url'];
				}

				$tracks[] = array(
					'title'  => $item['track_title'],
					'artist' => $item['artist_name'],
					'src'    => $track_url,
					'cover'  => $img_url,
				);
			}
		} else {
			// Single Mode
			$url = ! empty( $settings['audio_source']['url'] ) ? $settings['audio_source']['url'] : $settings['external_url'];

			$img_url = '';
			if ( ! empty( $settings['cover_image']['id'] ) ) {
				$img_url = wp_get_attachment_image_url( $settings['cover_image']['id'], 'medium' );
			} elseif ( ! empty( $settings['cover_image']['url'] ) ) {
				$img_url = $settings['cover_image']['url'];
			}

			if ( ! empty( $url ) ) {
				$tracks[] = array(
					'title'  => $settings['track_title'],
					'artist' => $settings['artist_name'],
					'src'    => $url,
					'cover'  => $img_url,
				);
			}
		}

		if ( empty( $tracks ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div>Please add audio tracks.</div>';
			}
			return;
		}

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-audio-player-wrapper' );
		$this->add_render_attribute( 'player', 'class', array( 'sifency-audio-player sifency-item-style', 'sifency-ap-layout-' . $settings['layout_type'] ) );

		$player_data = array(
			'autoplay' => $settings['autoplay'],
			'loop'     => $settings['loop'],
			'tracks'   => $tracks,
		);
		$this->add_render_attribute( 'player', 'data-ap-settings', wp_json_encode( $player_data ) );

		$current_track = $tracks[0];
		$has_playlist  = count( $tracks ) > 1;

		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . $animation_wrapper_attrs ); ?>>
			
			<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'player' ) ); ?>>
				
				<audio class="sifency-html5-audio" src="<?php echo esc_url( $current_track['src'] ); ?>" preload="metadata"></audio>

				<?php if ( 'minimal' !== $settings['layout_type'] ) : ?>
				<div class="sifency-ap-cover">
					<img src="<?php echo ! empty( $current_track['cover'] ) ? esc_url( $current_track['cover'] ) : 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'; ?>" class="sifency-ap-cover-img" alt="Cover">
				</div>
				<?php endif; ?>

				<div class="sifency-ap-content">
					
					<?php if ( 'minimal' !== $settings['layout_type'] ) : ?>
					<div class="sifency-ap-meta">
						<div class="sifency-ap-title sifency-heading-style"><?php echo esc_html( $current_track['title'] ); ?></div>
						<div class="sifency-ap-artist sifency-content-style"><?php echo esc_html( $current_track['artist'] ); ?></div>
					</div>
					<?php endif; ?>

					<div class="sifency-ap-controls-area">
						
						<div class="sifency-ap-main-controls">
							<?php if ( $has_playlist ) : ?>
							<button class="sifency-ap-nav-btn sifency-ap-prev-btn">
								<i class="eicon-backward"></i>
							</button>
							<?php endif; ?>

							<button class="sifency-ap-play-btn">
								<i class="eicon-play"></i>
							</button>

							<?php if ( $has_playlist ) : ?>
							<button class="sifency-ap-nav-btn sifency-ap-next-btn">
								<i class="eicon-forward"></i>
							</button>
							<?php endif; ?>
						</div>

						<div class="sifency-ap-progress-container">
							<input type="range" class="sifency-ap-seek-bar" value="0" min="0" max="100" step="0.1">
							<div class="sifency-ap-progress-bg"></div>
							<div class="sifency-ap-progress-fill"></div>
						</div>

						<div class="sifency-ap-extras">
							<?php if ( 'yes' === $settings['show_time'] ) : ?>
							<div class="sifency-ap-time">
								<span class="sifency-ap-current-time">0:00</span> / <span class="sifency-ap-total-time">0:00</span>
							</div>
							<?php endif; ?>

							<?php if ( 'yes' === $settings['show_volume'] ) : ?>
							<div class="sifency-ap-volume-wrap">
								<button class="sifency-ap-volume-btn sifency-ap-control-icon"><i class="icon-gauge-1"></i></button>
								<div class="sifency-ap-volume-slider-wrap">
									<input type="range" class="sifency-ap-volume-slider" min="0" max="1" step="0.05" value="1">
								</div>
							</div>
							<?php endif; ?>

							<?php if ( 'yes' === $settings['show_download'] ) : ?>
							<a href="<?php echo esc_url( $current_track['src'] ); ?>" download class="sifency-ap-download-btn sifency-ap-control-icon">
								<i class="eicon-download-bold"></i>
							</a>
							<?php endif; ?>
						</div>
					</div>
				</div>
			</div>

			<?php if ( 'playlist' === $settings['source_type'] && 'yes' === $settings['show_playlist_list'] && count( $tracks ) > 1 ) : ?>
			<div class="sifency-ap-playlist-container">
				<?php foreach ( $tracks as $index => $track ) : ?>
					<?php $animation_item_attrs = $animation_manager->get_item_attributes(); ?>
					<div class="sifency-ap-playlist-item <?php echo $index === 0 ? 'active' : ''; ?>" data-index="<?php echo esc_attr( $index ); ?>" <?php echo wp_kses_post( $animation_item_attrs ); ?>>
						<div class="sifency-ap-pl-thumb">
							<?php if ( ! empty( $track['cover'] ) ) : ?>
								<img src="<?php echo esc_url( $track['cover'] ); ?>" alt="thumb">
							<?php else : ?>
								<i class="eicon-music"></i>
							<?php endif; ?>
						</div>
						<div class="sifency-ap-pl-info">
							<span class="sifency-ap-pl-title sifency-heading-style"><?php echo esc_html( $track['title'] ); ?></span>
							<span class="sifency-ap-pl-artist sifency-content-style"><?php echo esc_html( $track['artist'] ); ?></span>
						</div>
						<div class="sifency-ap-pl-icon"><i class="eicon-play"></i></div>
					</div>
				<?php endforeach; ?>
			</div>
			<?php endif; ?>

		</div>
		<?php
	}
}
