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

class Vista_Social_Widget extends \Elementor\Widget_Base {

	public function get_name() {
		return 'vista-social';
	}

	public function get_title() {
		return esc_html__( 'شبکه اجتماعی', 'vista' );
	}

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

	public function get_categories() {
		return [ 'vista-widgets' ];
	}

	public function get_keywords() {
		return [ 'social', 'follow', 'instagram', 'telegram', 'شبکه', 'دنبال' ];
	}

	protected function register_controls() {

		$this->start_controls_section( 'section_socials', [
			'label' => esc_html__( 'شبکه‌های اجتماعی', 'vista' ),
			'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
		] );

		$repeater = new \Elementor\Repeater();

		$repeater->add_control( 'platform_name', [
			'label'   => esc_html__( 'نام پلتفرم', 'vista' ),
			'type'    => \Elementor\Controls_Manager::TEXT,
			'default' => 'اینستاگرام',
		] );

		$repeater->add_control( 'handle', [
			'label'   => esc_html__( 'نام کاربری / Handle', 'vista' ),
			'type'    => \Elementor\Controls_Manager::TEXT,
			'default' => '@username',
		] );

		$repeater->add_control( 'icon_type', [
			'label'   => esc_html__( 'نوع آیکون', 'vista' ),
			'type'    => \Elementor\Controls_Manager::SELECT,
			'default' => 'library',
			'options' => [
				'library' => esc_html__( 'کتابخانه آیکون المنتور', 'vista' ),
				'custom'  => esc_html__( 'شبکه اجتماعی ایرانی', 'vista' ),
			],
		] );

		$repeater->add_control( 'social_icon', [
			'label'     => esc_html__( 'انتخاب آیکون', 'vista' ),
			'type'      => \Elementor\Controls_Manager::ICONS,
			'default'   => [ 'value' => 'fab fa-instagram', 'library' => 'fa-brands' ],
			'condition' => [ 'icon_type' => 'library' ],
		] );

		$repeater->add_control( 'iranian_platform_icon', [
			'label'     => esc_html__( 'انتخاب پلتفرم ایرانی', 'vista' ),
			'type'      => \Elementor\Controls_Manager::SELECT,
			'default'   => 'icon-aparat',
			'options'   => [
				'icon-aparat' => esc_html__( 'آپارات', 'vista' ),
				'icon-bale'   => esc_html__( 'بله', 'vista' ),
				'icon-eitaa'  => esc_html__( 'ایتا', 'vista' ),
				'icon-rubik'  => esc_html__( 'روبیکا', 'vista' ),
				'icon-sapp'   => esc_html__( 'سروش', 'vista' ),
			],
			'condition' => [ 'icon_type' => 'custom' ],
		] );

		$repeater->add_control( 'profile_url', [
			'label'       => esc_html__( 'لینک پروفایل', 'vista' ),
			'type'        => \Elementor\Controls_Manager::URL,
			'placeholder' => 'https://your-link.com',
		] );

		$this->add_control( 'social_list', [
			'label'   => esc_html__( 'لیست شبکه‌ها', 'vista' ),
			'type'    => \Elementor\Controls_Manager::REPEATER,
			'fields'  => $repeater->get_controls(),
			'default' => [
				[
					'platform_name' => 'Instagram',
					'handle'        => '@vista_ui',
					'icon_type'     => 'library',
					'social_icon'   => [ 'value' => 'fab fa-instagram', 'library' => 'fa-brands' ],
				],
			],
			'title_field' => '{{{ platform_name }}}',
		] );

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty( $settings['social_list'] ) ) return;
		?>
		<div class="vista-social-section-wrap">
			<div class="soc-wrap rv d2">
				<?php foreach ( $settings['social_list'] as $item ) :
					$url    = ! empty( $item['profile_url']['url'] ) ? $item['profile_url']['url'] : '#';
					$target = ! empty( $item['profile_url']['is_external'] ) ? ' target="_blank" rel="noopener"' : '';
				?>
				<a href="<?php echo esc_url( $url ); ?>" class="soc-card"<?php echo $target; ?>>
					<div class="soc-ico">
						<?php 
						if ( 'custom' === $item['icon_type'] ) : ?>
							<i class="<?php echo esc_attr( $item['iranian_platform_icon'] ); ?>" aria-hidden="true"></i>
						<?php else :
							\Elementor\Icons_Manager::render_icon( $item['social_icon'], [ 'aria-hidden' => 'true' ] ); 
						endif; 
						?>
					</div>
					<div class="soc-info">
						<div class="soc-name"><?php echo esc_html( $item['platform_name'] ); ?></div>
						<div class="soc-handle"><?php echo esc_html( $item['handle'] ); ?></div>
					</div>
				</a>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}

	protected function content_template() {
		?>
		<# if ( settings.social_list ) { #>
		<div class="vista-social-section-wrap">
			<div class="soc-wrap">
				<# _.each( settings.social_list, function( item ) { 
					var iconHTML = '';
					if ( item.icon_type === 'custom' ) {
						iconHTML = '<i class="' + item.iranian_platform_icon + '" aria-hidden="true"></i>';
					} else {
						var render = elementor.helpers.renderIcon( view, item.social_icon, { 'aria-hidden': true }, 'i' , 'object' );
						iconHTML = render.value;
					}
				#>
				<div class="soc-card">
					<div class="soc-ico">{{{ iconHTML }}}</div>
					<div class="soc-info">
						<div class="soc-name">{{{ item.platform_name }}}</div>
						<div class="soc-handle">{{{ item.handle }}}</div>
					</div>
				</div>
				<# }); #>
			</div>
		</div>
		<# } #>
		<?php
	}
}