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

use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Repeater;
use \Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Core_Text_Style;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;
use Sifency\Traits\Sifency_Author_Box_Controls;
use Sifency\Traits\Sifency_Image_Style_Controls;

class Sifency_Widget_Authorbox extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Core_Text_Style;
    use Sifency_Image_Style_Controls;
    use Sifency_Author_Box_Controls;

    public function get_name() {
        return 'author-box-sifency';
    }

    public function get_title() {
        return __('Sifency Author Box', 'sifency-addons');
    }

    public function get_icon() {
        return 'eicon-user-circle-o';
    }

    public function get_categories() {
        return ['sifency-post-components'];
    }

    public function get_style_depends() {
        return [ 'sifency-typo-style' , 'sifency-frontend-style', 'sifency-icon', 'sifency-post-single-style' ];
    }
    
    protected function register_controls() {
        $this->add_sifency_author_box_controls();
        $this->add_sifency_image_style_controls();
        $this->update_control(
            'image_style_section',
            [
                'label' => __( 'Author Image', 'sifency-addons' ),
            ]
        );
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Author Name',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Author Bio',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);
        $this->add_authorbox_icon_style_controls();
        $this->add_sifency_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
    }

    /**
     * Render the widget output on the frontend.
     *
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $author_name = ''; $author_bio = ''; $author_avatar_html = '';
        if ( 'current' === $settings['source'] ) {
            if ( ! get_the_author_meta('ID') ) {
                if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                    echo '<div class="sifency-placeholder">' . esc_html__('Author Box is set to "Current Post Author" but no author was found. Make sure you are on a Single Post page or set the preview in Theme Builder.', 'sifency-addons') . '</div>';
                }
                return;
            }
            $author_name = get_the_author();
            $author_bio = get_the_author_meta('description');
            $author_avatar_html = get_avatar( get_the_author_meta('ID'), 240 );
        } else {
            $author_name = $settings['custom_author_name'];
            $author_bio = $settings['custom_author_bio'];
            if ( ! empty( $settings['custom_author_avatar']['url'] ) ) {
                $author_avatar_html = '<img src="' . esc_url($settings['custom_author_avatar']['url']) . '" alt="' . esc_attr($author_name) . '" />';
            }
        }
        $wrapper_classes = ['sifency-author-box'];
        $wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
        $author_name_tag = ! empty( $settings['link']['url'] ) ? 'a' : 'div';
        $this->add_render_attribute( 'author_name_tag', 'class', 'sifency-author-box__name sifency-heading-style' );
        if ( 'a' === $author_name_tag ) {
            $this->add_link_attributes( 'author_name_tag', $settings['link'] );
        }
        ?>
        <div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
            <?php if ( $author_avatar_html ) : ?>
                <div class="sifency-author-box__avatar sifency-image-container">
                    <div class="sifency-image">
                        <?php 
                        echo wp_kses_post($author_avatar_html);
                        ?>
                    </div>
                    <div class="sifency-image-overlay"></div>
                </div>
            <?php endif; ?>

            <div class="sifency-author-box__text">
                <?php if ( $author_name ) : ?>
                    <<?php echo tag_escape($author_name_tag); ?> <?php
                        echo wp_kses_post ($this->get_render_attribute_string( 'author_name_tag' )); ?>>
                        <?php
                        echo esc_html( $author_name ); ?>
                    </<?php 
                    echo tag_escape($author_name_tag); ?>>
                <?php endif; ?>
                
                <?php if ( $author_bio ) : ?>
                    <div class="sifency-author-box__bio sifency-content-style">
                        <?php echo esc_html($author_bio); ?>
                    </div>
                <?php endif; ?>

                <?php if ( ! empty($settings['social_icon_list']) ) : ?>
                    <div class="sifency-author-box__social">
                        <?php foreach ( $settings['social_icon_list'] as $item ) :
                            $social_link_key = 'social-link-' . $item['_id'];
                            if ( ! empty( $item['link']['url'] ) ) {
                                $this->add_link_attributes( $social_link_key, $item['link'] );
                            }
                            $this->add_render_attribute( $social_link_key, 'class', 'sifency-icon-style' );
                            ?>
                            <a <?php 
                                echo wp_kses_post ($this->get_render_attribute_string($social_link_key)); ?>>
                                <?php \Elementor\Icons_Manager::render_icon( $item['social_icon'], [ 'aria-hidden' => 'true' ] ); ?>
                            </a>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        <?php
    }
}