<?php
namespace Softech_Addons_for_Elementor\Widgets;

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

if ( ! class_exists( '\\Softech_Addons_for_Elementor\\Includes\\SITS_Single_Post_Widget_Base' ) ) {
    require_once SITS_ELEMENTOR_ADDONS_PLUGIN_PATH . 'includes/class-single-post-widget-base.php';
}

use Elementor\Controls_Manager;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Softech_Addons_for_Elementor\Includes\SITS_Single_Post_Widget_Base;

class sitsel_author_box extends SITS_Single_Post_Widget_Base {

    protected function get_style_handle(): string { return 'sitsel-author-box-css'; }
    protected function get_style_file(): string { return 'assets/css/sitsel-author-box.css'; }
    protected function get_script_handle(): string { return 'sitsel-author-box-js'; }
    protected function get_script_file(): string { return 'assets/js/sitsel-author-box.js'; }

    public function get_name() { return 'sitsel_author_box'; }
    public function get_title() { return __( 'SITS Author Box', 'softech-addons-for-elementor' ); }
    public function get_icon() { return 'eicon-person'; }
    public function get_keywords() { return [ 'author', 'bio', 'profile', 'single', 'posts' ]; }

    protected function _register_controls() {
        $this->start_controls_section( 'section_content', [ 'label' => esc_html__( 'Content', 'softech-addons-for-elementor' ) ] );

        $this->add_control( 'layout', [
            'label'   => esc_html__( 'Layout', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::SELECT,
            'default' => 'horizontal',
            'options' => [
                'horizontal' => esc_html__( 'Horizontal', 'softech-addons-for-elementor' ),
                'vertical'    => esc_html__( 'Vertical', 'softech-addons-for-elementor' ),
                'card'        => esc_html__( 'Card', 'softech-addons-for-elementor' ),
            ],
        ] );

        $this->add_control( 'content_align', [
            'label'   => esc_html__( 'Content Alignment', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::SELECT,
            'default' => 'left',
            'options' => [
                'left'   => esc_html__( 'Left', 'softech-addons-for-elementor' ),
                'center' => esc_html__( 'Center', 'softech-addons-for-elementor' ),
                'right'  => esc_html__( 'Right', 'softech-addons-for-elementor' ),
            ],
        ] );

        $this->add_control( 'avatar_show', [
            'label'   => esc_html__( 'Show Avatar', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ] );

        $this->add_responsive_control( 'avatar_size', [
            'label' => esc_html__( 'Avatar Size', 'softech-addons-for-elementor' ),
            'type' => Controls_Manager::SLIDER,
            'size_units' => [ 'px' ],
            'range' => [ 'px' => [ 'min' => 32, 'max' => 220 ] ],
            'default' => [ 'size' => 96, 'unit' => 'px' ],
            'condition' => [ 'avatar_show' => 'yes' ],
            'selectors' => [
                '{{WRAPPER}} .sitsel-author-box-avatar' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ] );

        $this->add_control( 'avatar_position', [
            'label'   => esc_html__( 'Avatar Position', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::SELECT,
            'default' => 'left',
            'options' => [
                'left'   => esc_html__( 'Left', 'softech-addons-for-elementor' ),
                'right'  => esc_html__( 'Right', 'softech-addons-for-elementor' ),
            ],
            'condition' => [ 'layout' => 'horizontal' ],
        ] );

        $this->add_responsive_control( 'avatar_radius', [
            'label' => esc_html__( 'Avatar Radius', 'softech-addons-for-elementor' ),
            'type' => Controls_Manager::SLIDER,
            'size_units' => [ 'px', '%' ],
            'default' => [ 'size' => 999, 'unit' => 'px' ],
            'selectors' => [
                '{{WRAPPER}} .sitsel-author-box-avatar' => 'border-radius: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .sitsel-author-box-avatar img' => 'border-radius: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .sitsel-author-box-avatar-placeholder' => 'border-radius: {{SIZE}}{{UNIT}};',
            ],
        ] );

        $this->add_control( 'name_tag', [
            'label'   => esc_html__( 'Name HTML Tag', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::SELECT,
            'default' => 'h3',
            'options' => [
                'h1'   => 'H1',
                'h2'   => 'H2',
                'h3'   => 'H3',
                'h4'   => 'H4',
                'h5'   => 'H5',
                'h6'   => 'H6',
                'div'  => 'div',
                'span' => 'span',
            ],
        ] );

        $this->add_control( 'display_mode', [
            'label'   => esc_html__( 'Display Elements', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::SELECT,
            'default' => 'full',
            'options' => [
                'full'          => esc_html__( 'Full Profile', 'softech-addons-for-elementor' ),
                'profile'       => esc_html__( 'Avatar, Name & Bio', 'softech-addons-for-elementor' ),
                'avatar_name'   => esc_html__( 'Avatar & Name', 'softech-addons-for-elementor' ),
                'name_bio'      => esc_html__( 'Name & Bio', 'softech-addons-for-elementor' ),
                'name_only'     => esc_html__( 'Name Only', 'softech-addons-for-elementor' ),
                'avatar_only'   => esc_html__( 'Avatar Only', 'softech-addons-for-elementor' ),
            ],
        ] );

        $this->add_control( 'link_author_archive', [
            'label'   => esc_html__( 'Link Name/Avatar to Author Archive', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ] );

        $this->add_control( 'bio_length', [
            'label'   => esc_html__( 'Bio Length', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::NUMBER,
            'default' => 35,
            'min'     => 10,
            'max'     => 200,
        ] );

        $this->add_control( 'view_posts_button', [
            'label'   => esc_html__( 'Show View Posts Button', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ] );

        $this->add_control( 'view_posts_text', [
            'label'     => esc_html__( 'Button Text', 'softech-addons-for-elementor' ),
            'type'      => Controls_Manager::TEXT,
            'default'   => esc_html__( 'View All Posts', 'softech-addons-for-elementor' ),
            'condition' => [ 'view_posts_button' => 'yes' ],
        ] );

        $this->end_controls_section();

        $this->start_controls_section( 'section_style', [ 'label' => esc_html__( 'Style', 'softech-addons-for-elementor' ), 'tab' => Controls_Manager::TAB_STYLE ] );
        $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'box_background', 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .sitsel-author-box' ] );
        $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'box_border', 'selector' => '{{WRAPPER}} .sitsel-author-box' ] );
        $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'box_shadow', 'selector' => '{{WRAPPER}} .sitsel-author-box' ] );
        $this->add_responsive_control( 'box_padding', [ 'label' => esc_html__( 'Padding', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', 'rem' ], 'selectors' => [ '{{WRAPPER}} .sitsel-author-box' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
        $this->add_responsive_control( 'box_margin', [ 'label' => esc_html__( 'Margin', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', 'em', 'rem' ], 'selectors' => [ '{{WRAPPER}} .sitsel-author-box' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
        $this->add_control( 'name_color', [ 'label' => esc_html__( 'Name Color', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sitsel-author-box-name' => 'color: {{VALUE}};', '{{WRAPPER}} .sitsel-author-box-name a' => 'color: {{VALUE}};' ] ] );
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'name_typography', 'selector' => '{{WRAPPER}} .sitsel-author-box-name' ] );
        $this->add_control( 'bio_color', [ 'label' => esc_html__( 'Bio Color', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sitsel-author-box-bio' => 'color: {{VALUE}};' ] ] );
        $this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'bio_typography', 'selector' => '{{WRAPPER}} .sitsel-author-box-bio' ] );
        $this->add_control( 'social_style', [ 'label' => esc_html__( 'Social Style', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::SELECT, 'default' => 'official', 'options' => [ 'official' => esc_html__( 'Official Colors', 'softech-addons-for-elementor' ), 'custom' => esc_html__( 'Custom Color', 'softech-addons-for-elementor' ), 'mono' => esc_html__( 'Monochrome', 'softech-addons-for-elementor' ) ] ] );
        $this->add_control( 'social_color', [ 'label' => esc_html__( 'Social Color', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sitsel-author-box-social a' => 'color: {{VALUE}};' ], 'condition' => [ 'social_style' => 'custom' ] ] );
        $this->add_responsive_control( 'social_size', [ 'label' => esc_html__( 'Social Size', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px' ], 'range' => [ 'px' => [ 'min' => 10, 'max' => 40 ] ], 'selectors' => [ '{{WRAPPER}} .sitsel-author-box-social a' => 'font-size: {{SIZE}}{{UNIT}};' ] ] );
        $this->add_responsive_control( 'social_gap', [ 'label' => esc_html__( 'Social Spacing', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::SLIDER, 'size_units' => [ 'px' ], 'range' => [ 'px' => [ 'min' => 0, 'max' => 30 ] ], 'selectors' => [ '{{WRAPPER}} .sitsel-author-box-social' => 'gap: {{SIZE}}{{UNIT}};' ] ] );
        $this->end_controls_section();

        $this->start_controls_section( 'section_hover_style', [ 'label' => esc_html__( 'Hover', 'softech-addons-for-elementor' ), 'tab' => Controls_Manager::TAB_STYLE ] );
        $this->add_group_control( Group_Control_Background::get_type(), [ 'name' => 'box_hover_background', 'types' => [ 'classic', 'gradient' ], 'selector' => '{{WRAPPER}} .sitsel-author-box:hover' ] );
        $this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'box_hover_border', 'selector' => '{{WRAPPER}} .sitsel-author-box:hover' ] );
        $this->add_group_control( Group_Control_Box_Shadow::get_type(), [ 'name' => 'box_hover_shadow', 'selector' => '{{WRAPPER}} .sitsel-author-box:hover' ] );
        $this->add_control( 'name_hover_color', [ 'label' => esc_html__( 'Name Hover Color', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sitsel-author-box:hover .sitsel-author-box-name' => 'color: {{VALUE}};', '{{WRAPPER}} .sitsel-author-box:hover .sitsel-author-box-name a' => 'color: {{VALUE}};' ] ] );
        $this->add_control( 'bio_hover_color', [ 'label' => esc_html__( 'Bio Hover Color', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sitsel-author-box:hover .sitsel-author-box-bio' => 'color: {{VALUE}};' ] ] );
        $this->add_control( 'social_hover_color', [ 'label' => esc_html__( 'Social Hover Color', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sitsel-author-box:hover .sitsel-author-social-link' => 'color: {{VALUE}};' ] ] );
        $this->add_control( 'button_hover_color', [ 'label' => esc_html__( 'Button Hover Color', 'softech-addons-for-elementor' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sitsel-author-box:hover .sitsel-author-box-button' => 'color: {{VALUE}};' ] ] );
        $this->end_controls_section();
    }

    protected function render() {
        $settings      = $this->get_settings_for_display();
        $author        = $this->get_author_data();
        $layout        = $settings['layout'] ?? 'horizontal';
        $align         = $settings['content_align'] ?? 'left';
        $name_tag      = in_array( strtolower( (string) ( $settings['name_tag'] ?? 'h3' ) ), [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span' ], true ) ? strtolower( (string) $settings['name_tag'] ) : 'h3';
        $bio_length    = absint( $settings['bio_length'] ?? 35 );
        $avatar_url    = ! empty( $author['avatar'] ) ? (string) $author['avatar'] : '';
        $archive_url   = $author['id'] ? get_author_posts_url( (int) $author['id'] ) : '';
        $display_items = $this->get_display_items( (string) ( $settings['display_mode'] ?? 'full' ) );

        $this->add_render_attribute( 'wrapper', [
            'class'    => [
                'sitsel-author-box',
                'layout-' . $layout,
                'avatar-pos-' . ( $settings['avatar_position'] ?? 'left' ),
                'content-align-' . $align,
                'display-mode-' . ( $settings['display_mode'] ?? 'full' ),
                'social-style-' . ( $settings['social_style'] ?? 'official' ),
            ],
            'itemscope' => 'itemscope',
            'itemtype'  => 'https://schema.org/Person',
        ] );

        echo '<div ' . $this->get_render_attribute_string( 'wrapper' ) . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Render attribute strings are generated by Elementor.
        echo '<div class="sitsel-author-box-inner">';

        if ( in_array( 'avatar', $display_items, true ) && 'yes' === ( $settings['avatar_show'] ?? 'yes' ) ) {
            echo '<div class="sitsel-author-box-avatar">';
            $avatar_markup = $this->get_avatar_markup( $avatar_url, (string) $author['name'] );
            if ( 'yes' === ( $settings['link_author_archive'] ?? 'yes' ) && $archive_url ) {
                echo '<a href="' . esc_url( $archive_url ) . '">' . wp_kses_post( $avatar_markup ) . '</a>';
            } else {
                echo wp_kses_post( $avatar_markup );
            }
            echo '</div>';
        }

        echo '<div class="sitsel-author-box-content">';

        if ( in_array( 'name', $display_items, true ) ) {
            echo '<' . esc_attr( $name_tag ) . ' class="sitsel-author-box-name" itemprop="name">';
            if ( 'yes' === ( $settings['link_author_archive'] ?? 'yes' ) && $archive_url ) {
                echo '<a href="' . esc_url( $archive_url ) . '">' . esc_html( $author['name'] ) . '</a>';
            } else {
                echo esc_html( $author['name'] );
            }
            echo '</' . esc_attr( $name_tag ) . '>';
        }

        if ( in_array( 'bio', $display_items, true ) ) {
            echo '<div class="sitsel-author-box-bio" itemprop="description">' . esc_html( wp_trim_words( (string) $author['bio'], $bio_length ) ) . '</div>';
        }

        if ( in_array( 'posts', $display_items, true ) ) {
            $post_count_label = 1 === (int) $author['post_count']
                ? esc_html__( '1 post', 'softech-addons-for-elementor' )
                : number_format_i18n( (int) $author['post_count'] ) . ' ' . esc_html__( 'posts', 'softech-addons-for-elementor' );
            echo '<div class="sitsel-author-box-post-count">' . esc_html( $post_count_label ) . '</div>';
        }

        if ( in_array( 'website', $display_items, true ) && ! empty( $author['website'] ) ) {
            echo '<a class="sitsel-author-box-website" href="' . esc_url( $author['website'] ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Website', 'softech-addons-for-elementor' ) . '</a>';
        }

        if ( in_array( 'email', $display_items, true ) && ! empty( $author['id'] ) ) {
            $email = get_the_author_meta( 'user_email', (int) $author['id'] );
            if ( $email ) {
                echo '<a class="sitsel-author-box-email" href="mailto:' . esc_attr( antispambot( $email ) ) . '">' . esc_html__( 'Email Author', 'softech-addons-for-elementor' ) . '</a>';
            }
        }

        if ( in_array( 'social', $display_items, true ) && ! empty( $author['social_links'] ) ) {
            echo '<div class="sitsel-author-box-social">';
            foreach ( $author['social_links'] as $network => $url ) {
                $class = 'fab fa-' . sanitize_html_class( $network );
                if ( 'website' === $network ) {
                    $class = 'fas fa-globe';
                } elseif ( 'x' === $network || 'twitter' === $network ) {
                    $class = 'fab fa-twitter';
                }
                echo '<a class="sitsel-author-social-link network-' . esc_attr( $network ) . '" href="' . esc_url( $url ) . '" target="_blank" rel="noopener noreferrer" aria-label="' . esc_attr( ucfirst( $network ) ) . '">';
                echo '<i class="' . esc_attr( $class ) . '" aria-hidden="true"></i>';
                echo '</a>';
            }
            echo '</div>';
        }

        if ( 'yes' === ( $settings['view_posts_button'] ?? 'yes' ) && $archive_url ) {
            echo '<a class="sitsel-author-box-button" href="' . esc_url( $archive_url ) . '">' . esc_html( $settings['view_posts_text'] ?? esc_html__( 'View All Posts', 'softech-addons-for-elementor' ) ) . '</a>';
        }

        echo '</div></div></div>';
    }

    private function get_display_items( string $mode ): array {
        $modes = [
            'full'        => [ 'avatar', 'name', 'bio', 'posts', 'website', 'social', 'email' ],
            'profile'     => [ 'avatar', 'name', 'bio' ],
            'avatar_name' => [ 'avatar', 'name' ],
            'name_bio'    => [ 'name', 'bio' ],
            'name_only'   => [ 'name' ],
            'avatar_only' => [ 'avatar' ],
        ];

        return $modes[ $mode ] ?? $modes['full'];
    }

    private function get_avatar_markup( string $avatar_url, string $author_name ): string {
        if ( '' !== trim( $avatar_url ) ) {
            return sprintf(
                '<img src="%1$s" alt="%2$s" loading="lazy" decoding="async" />',
                esc_url( $avatar_url ),
                esc_attr( $author_name )
            );
        }

        return '
            <span class="sitsel-author-box-avatar-placeholder" aria-hidden="true">
                <svg viewBox="0 0 64 64" role="presentation" focusable="false" aria-hidden="true">
                    <circle cx="32" cy="24" r="12" fill="currentColor" opacity="0.95"></circle>
                    <path d="M14 56c3.6-10.2 12.4-16 18-16s14.4 5.8 18 16" fill="currentColor" opacity="0.95"></path>
                </svg>
            </span>
        ';
    }
}
