<?php
if ( ! defined( 'ABSPATH' ) ) exit;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Code_Snippet extends Sifency_Addons_Widget_Base {
    use Sifency_Addons_Button_Style_Controls;
    use Sifency_Addons_Help_Controls;
	public function __construct( $data = [], $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = [
        'name'       => 'sifency_code_snippet',
        'title'      => 'Sifency Code Pro',
        'icon'       => 'eicon-code',
        'category' => [ 'sifency-advance-widgets' ],
        'keywords'      => ['code', 'snippet', 'syntax', 'highlight', 'prism'],
        'style_depends'     => [ 'sifency-helper-style', 'sifency-prism-vendor', 'sifency-prism-line-vendor' ],
        'script_depends'    => [ 'prism', 'prism-line-numbers', 'sifency-code-snippets-script' ],
    ];}
    protected function register_controls() {
        $this->start_controls_section('content_section', [
            'label' => esc_html__( 'Snippet Configuration', 'sifency-addons' ),
            'tab' => Controls_Manager::TAB_CONTENT,
        ]);
        $this->add_control('layout', [
            'label' => esc_html__( 'Design Layout', 'sifency-addons' ),
            'type' => Controls_Manager::SELECT,
            'default' => 'macos',
            'options' => [
                'minimal'  => esc_html__( 'Minimalist', 'sifency-addons' ),
                'macos'    => esc_html__( 'macOS Window', 'sifency-addons' ),
                'terminal' => esc_html__( 'Terminal Bash', 'sifency-addons' ),
                'glass'    => esc_html__( 'Glassmorphism', 'sifency-addons' ),
                'gradient' => esc_html__( 'Gradient Border', 'sifency-addons' ),
            ],
        ]);

        $this->add_control('file_name', [
            'label' => esc_html__( 'File Name / Title', 'sifency-addons' ),
            'type' => Controls_Manager::TEXT,
            'default' => 'index.js',
            'placeholder' => esc_html__( 'e.g. style.css', 'sifency-addons' ),
            'condition' => [ 'layout' => ['macos', 'terminal', 'glass'] ],
        ]);

        $this->add_control('language', [
            'label' => esc_html__( 'Language', 'sifency-addons' ),
            'type' => Controls_Manager::SELECT,
            'default' => 'javascript',
            'options' => [
                'markup'     => 'HTML',
                'css'        => 'CSS',
                'javascript' => 'JavaScript',
                'php'        => 'PHP',
                'python'     => 'Python',
                'sql'        => 'SQL',
            ],
        ]);

        $this->add_control('code', [
            'label' => esc_html__( 'Code Snippet', 'sifency-addons' ),
            'type' => Controls_Manager::CODE,
            'language' => 'javascript',
            'rows' => 10,
            'default' => 'function greet() { console.log("Hello World"); }',
        ]);

        $this->add_control('show_line_numbers', [
            'label' => esc_html__( 'Line Numbers', 'sifency-addons' ),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ]);

        $this->add_control('show_copy', [
            'label' => esc_html__( 'Copy Button', 'sifency-addons' ),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ]);
        $this->end_controls_section();
        $this->add_sifency_addons_help_controls();
        // --- STYLE: CONTAINER ---
        $this->start_controls_section('section_style_container', [
            'label' => esc_html__( 'Container Styling', 'sifency-addons' ),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_group_control(Group_Control_Background::get_type(), [
            'name' => 'container_bg',
            'label' => esc_html__( 'Editor Background', 'sifency-addons' ),
            'types' => [ 'classic', 'gradient' ],
            'selector' => '{{WRAPPER}} .sifency-code-wrapper, {{WRAPPER}} pre',
        ]);

        $this->add_responsive_control('container_padding', [
            'label' => esc_html__( 'Padding', 'sifency-addons' ),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => [ 'px', '%', 'em' ],
            'selectors' => [ '{{WRAPPER}} .code-body pre' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}} !important;' ],
        ]);

        $this->add_group_control(Group_Control_Border::get_type(), [
            'name' => 'container_border',
            'selector' => '{{WRAPPER}} .sifency-code-wrapper',
        ]);

        $this->add_control('container_radius', [
            'label' => esc_html__( 'Border Radius', 'sifency-addons' ),
            'type' => Controls_Manager::SLIDER,
            'selectors' => [ '{{WRAPPER}} .sifency-code-wrapper' => 'border-radius: {{SIZE}}px; overflow: hidden;' ],
        ]);

        $this->add_group_control(Group_Control_Box_Shadow::get_type(), [
            'name' => 'container_shadow',
            'selector' => '{{WRAPPER}} .sifency-code-wrapper',
        ]);
        $this->end_controls_section();
        // --- STYLE: HEADER ---
        $this->start_controls_section('section_style_header', [
            'label' => esc_html__( 'Header / Title Bar', 'sifency-addons' ),
            'tab' => Controls_Manager::TAB_STYLE,
            'condition' => [ 'layout!' => 'minimal' ],
        ]);

        $this->add_control('header_bg', [
            'label' => esc_html__( 'Header Background', 'sifency-addons' ),
            'type' => Controls_Manager::COLOR,
            'selectors' => [ '{{WRAPPER}} .code-header' => 'background-color: {{VALUE}};' ],
        ]);

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'title_typography',
            'label' => esc_html__( 'File Title Typography', 'sifency-addons' ),
            'selector' => '{{WRAPPER}} .file-title',
        ]);

        $this->add_control('title_color', [
            'label' => esc_html__( 'File Title Color', 'sifency-addons' ),
            'type' => Controls_Manager::COLOR,
            'selectors' => [ '{{WRAPPER}} .file-title' => 'color: {{VALUE}};' ],
        ]);
        $this->end_controls_section();
        // --- STYLE: SYNTAX HIGHLIGHTING ---
        $this->start_controls_section('section_style_syntax', [
            'label' => esc_html__( 'Syntax Highlighting', 'sifency-addons' ),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_group_control(Group_Control_Typography::get_type(), [
            'name' => 'code_typography',
            'label' => esc_html__( 'Code Font', 'sifency-addons' ),
            'selector' => '{{WRAPPER}} pre, {{WRAPPER}} code',
        ]);

        $this->add_control('syntax_keyword', [
            'label' => esc_html__( 'Keywords', 'sifency-addons' ),
            'type' => Controls_Manager::COLOR,
            'default' => '#c678dd',
            'selectors' => [ '{{WRAPPER}} .token.keyword' => 'color: {{VALUE}} !important;' ],
        ]);

        $this->add_control('syntax_function', [
            'label' => esc_html__( 'Functions', 'sifency-addons' ),
            'type' => Controls_Manager::COLOR,
            'default' => '#61afef',
            'selectors' => [ '{{WRAPPER}} .token.function' => 'color: {{VALUE}} !important;' ],
        ]);

        $this->add_control('syntax_string', [
            'label' => esc_html__( 'Strings', 'sifency-addons' ),
            'type' => Controls_Manager::COLOR,
            'default' => '#98c379',
            'selectors' => [ '{{WRAPPER}} .token.string' => 'color: {{VALUE}} !important;' ],
        ]);

        $this->add_control('syntax_comment', [
            'label' => esc_html__( 'Comments', 'sifency-addons' ),
            'type' => Controls_Manager::COLOR,
            'default' => '#7f848e',
            'selectors' => [ '{{WRAPPER}} .token.comment' => 'color: {{VALUE}} !important; font-style: italic;' ],
        ]);

        $this->add_control('line_number_color', [
            'label' => esc_html__( 'Line Number Color', 'sifency-addons' ),
            'type' => Controls_Manager::COLOR,
            'selectors' => [ '{{WRAPPER}} .line-numbers-rows span::before' => 'color: {{VALUE}} !important;' ],
            'condition' => [ 'show_line_numbers' => 'yes' ],
        ]);
        $this->end_controls_section();
        $this->start_controls_section('style_section', [
            'label' => esc_html__( 'Visual Styles', 'sifency-addons' ),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('font_size', [
            'label' => esc_html__( 'Font Size', 'sifency-addons' ),
            'type' => Controls_Manager::SLIDER,
            'range' => [ 'px' => [ 'min' => 10, 'max' => 30 ] ],
            'default' => [ 'size' => 14 ],
            'selectors' => [ '{{WRAPPER}} pre' => 'font-size: {{SIZE}}{{UNIT}} !important;' ],
        ]);
        $this->end_controls_section();
                $this->start_controls_section('section_style_scrollbar', [
            'label' => esc_html__( 'Custom Scrollbar', 'sifency-addons' ),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('scrollbar_width', [
            'label' => esc_html__( 'Scrollbar Width (px)', 'sifency-addons' ),
            'type' => Controls_Manager::SLIDER,
            'range' => [ 'px' => [ 'min' => 2, 'max' => 12 ] ],
            'selectors' => [
                '{{WRAPPER}} pre::-webkit-scrollbar' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->add_control('scrollbar_thumb_color', [
            'label' => esc_html__( 'Thumb Color', 'sifency-addons' ),
            'type' => Controls_Manager::COLOR,
            'selectors' => [ '{{WRAPPER}} pre::-webkit-scrollbar-thumb' => 'background: {{VALUE}}; border-radius: 10px;' ],
        ]);
        $this->end_controls_section();
        $this->add_Sifency_Addons_Button_Style_Controls([
            'condition' => ['show_copy' == "yes" ],
            'label'           => esc_html__( 'Copy Button', 'sifency-addons' ),
            'selector' => '.copy-btn',
        ]);
    }
    protected function render_dom( array $context ) {
        $wrapper_id = esc_attr( $context['dom_id'] );
        $settings = $this->get_settings_for_display();

        $code   = $settings['code'];
        $lang   = esc_attr( $settings['language'] );
        $layout = esc_attr( $settings['layout'] );

        $line_numbers = ( $settings['show_line_numbers'] === 'yes' ) ? 'line-numbers' : '';
        $pre_classes  = trim( $line_numbers . ' language-' . $lang );
        ?>
        
        <div id="<?php echo esc_attr ( $wrapper_id ); ?>" class="sifency-code-wrapper layout-<?php echo esc_attr( $layout ); ?>">

            <?php if ( in_array( $layout, [ 'macos', 'terminal', 'glass', 'gradient' ], true ) ) : ?>
                <div class="code-header">
                    <div class="window-buttons">
                        <span class="dot red"></span>
                        <span class="dot yellow"></span>
                        <span class="dot green"></span>
                    </div>

                    <?php if ( ! empty( $settings['file_name'] ) ) : ?>
                        <div class="file-title">
                            <?php echo esc_html( $settings['file_name'] ); ?>
                        </div>
                    <?php endif; ?>

                    <?php if ( $settings['show_copy'] === 'yes' ) : ?>
                        <button type="button" class="copy-btn">
                            <?php esc_html_e( 'Copy', 'sifency-addons' ); ?>
                        </button>
                    <?php endif; ?>
                </div>
            <?php endif; ?>

            <div class="code-body">
                <pre class="<?php echo esc_attr( $pre_classes ); ?>">
            <code><?php echo esc_html( $code ); ?></code>
                </pre>
            </div>

        </div>
        <?php
    }

}