<?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_Css_Filter;
use Elementor\Group_Control_Typography;
use Softech_Addons_for_Elementor\Includes\SITS_Single_Post_Widget_Base;

class sitsel_post_title extends SITS_Single_Post_Widget_Base {

    protected function get_style_handle(): string {
        return 'sitsel-post-title-css';
    }

    protected function get_style_file(): string {
        return 'assets/css/sitsel-post-title.css';
    }

    protected function get_script_handle(): string {
        return 'sitsel-post-title-js';
    }

    protected function get_script_file(): string {
        return 'assets/js/sitsel-post-title.js';
    }

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

    public function get_title() {
        return  __( 'SITS Post Title', 'softech-addons-for-elementor' );
    }

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

    public function get_keywords() {
        return [ 'post', 'title', 'single', 'heading', 'article' ];
    }

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

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

        $this->add_control( 'title_align', [
            'label'   => esc_html__( 'Alignment', 'softech-addons-for-elementor' ),
            'type'    => Controls_Manager::CHOOSE,
            'default' => 'left',
            'options' => [
                'left'    => [ 'title' => esc_html__( 'Left', 'softech-addons-for-elementor' ), 'icon' => 'eicon-text-align-left' ],
                'center'  => [ 'title' => esc_html__( 'Center', 'softech-addons-for-elementor' ), 'icon' => 'eicon-text-align-center' ],
                'right'   => [ 'title' => esc_html__( 'Right', 'softech-addons-for-elementor' ), 'icon' => 'eicon-text-align-right' ],
                'justify' => [ 'title' => esc_html__( 'Justify', 'softech-addons-for-elementor' ), 'icon' => 'eicon-text-align-justify' ],
            ],
            'selectors' => [ '{{WRAPPER}} .sitsel-post-title' => 'text-align: {{VALUE}};' ],
        ] );

        $this->add_control( 'fallback_title', [
            'label'       => esc_html__( 'Preview Title', 'softech-addons-for-elementor' ),
            'type'        => Controls_Manager::TEXT,
            'default'     => esc_html__( 'Sample Post Title', 'softech-addons-for-elementor' ),
            'description' => esc_html__( 'Used when no current post is available in preview mode.', 'softech-addons-for-elementor' ),
        ] );

        $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_control( 'title_color', [
            'label' => esc_html__( 'Text Color', 'softech-addons-for-elementor' ),
            'type' => Controls_Manager::COLOR,
            'default' => '#111827',
            'selectors' => [ '{{WRAPPER}} .sitsel-post-title' => 'color: {{VALUE}};' ],
        ] );

        $this->add_control( 'title_hover_color', [
            'label' => esc_html__( 'Hover Color', 'softech-addons-for-elementor' ),
            'type' => Controls_Manager::COLOR,
            'selectors' => [ '{{WRAPPER}} .sitsel-post-title-wrapper:hover .sitsel-post-title' => 'color: {{VALUE}};' ],
        ] );

        $this->add_group_control( Group_Control_Typography::get_type(), [
            'name' => 'title_typography',
            'selector' => '{{WRAPPER}} .sitsel-post-title',
        ] );

        $this->add_group_control( Group_Control_Background::get_type(), [
            'name' => 'title_background',
            'types' => [ 'classic', 'gradient' ],
            'selector' => '{{WRAPPER}} .sitsel-post-title-wrapper',
        ] );

        $this->add_group_control( Group_Control_Border::get_type(), [
            'name' => 'title_border',
            'selector' => '{{WRAPPER}} .sitsel-post-title-wrapper',
        ] );

        $this->add_group_control( Group_Control_Box_Shadow::get_type(), [
            'name' => 'title_shadow',
            'selector' => '{{WRAPPER}} .sitsel-post-title-wrapper',
        ] );

        $this->add_group_control( Group_Control_Css_Filter::get_type(), [
            'name' => 'title_filters',
            'selector' => '{{WRAPPER}} .sitsel-post-title-wrapper',
        ] );

        $this->add_responsive_control( 'title_radius', [
            'label' => esc_html__( 'Border Radius', 'softech-addons-for-elementor' ),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => [ 'px', '%', 'em' ],
            'selectors' => [ '{{WRAPPER}} .sitsel-post-title-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
        ] );

        $this->add_responsive_control( 'title_padding', [
            'label' => esc_html__( 'Padding', 'softech-addons-for-elementor' ),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => [ 'px', 'em', 'rem' ],
            'selectors' => [ '{{WRAPPER}} .sitsel-post-title-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
        ] );

        $this->add_responsive_control( 'title_margin', [
            'label' => esc_html__( 'Margin', 'softech-addons-for-elementor' ),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => [ 'px', 'em', 'rem' ],
            'selectors' => [ '{{WRAPPER}} .sitsel-post-title-wrapper' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
        ] );

        $this->end_controls_section();
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $tag      = $this->sanitize_tag( $settings['title_tag'] ?? 'h1' );
        $title    = $this->get_current_post_title( $settings['fallback_title'] ?? $this->get_sample_post_title() );

        $this->add_render_attribute( 'wrapper', [
            'class' => 'sitsel-post-title-wrapper',
            'itemtype' => 'https://schema.org/BlogPosting',
            'itemscope' => 'itemscope',
        ] );

        $this->add_render_attribute( 'title', [
            'class' => 'sitsel-post-title',
            'itemprop' => 'headline',
        ] );

        echo '<div ' . $this->get_render_attribute_string( 'wrapper' ) . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Render attribute strings are generated by Elementor.
        echo '<' . esc_attr( $tag ) . ' ' . $this->get_render_attribute_string( 'title' ) . '>' . esc_html( $title ) . '</' . esc_attr( $tag ) . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Render attribute strings are generated by Elementor.
        echo '</div>';
    }

    private function sanitize_tag( string $tag ): string {
        $allowed = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span' ];
        return in_array( strtolower( $tag ), $allowed, true ) ? strtolower( $tag ) : 'h1';
    }
}
