<?php 

    namespace Pixebuilder\Element;

    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;

    class Item{

        static function is_pro(){
            return false;
        }

        // default value for items
        static function get_default(){
            return '';
        }

        // blank, composite
        static function get_data(){
            return '';
        }

        // basic, composite, query, woocommerce
        static function get_type(){
            return 'basic';
        }
        
        // slug of the item
        static function get_name(){
            return 'default_element';
        }

        static function get_title(){
            return esc_html__('Default Element', 'pixebuilder');
        }

        static function get_icon(){
            return '';
        }

        static function get_dependencies(){
            return array();
        }

        static function get_keywords(){
            return array();
        }
        
        static function render_element($settings){
            if( empty($settings['_element']) ) return '';

            $item_class = \Pixe_Builder::get_element($settings['_element']);
            if( !empty($item_class) && class_exists($item_class) ){
                return $item_class::render($settings);
            }

            return '';
        }

        static function render($settings){
            $item_class = static::item_class($settings);

            $extra_class  = empty($settings['_element'])? '': ' pixe-item-' . $settings['_element'];
            $extra_class .= empty($settings['class'])? '': ' ' . trim($settings['class']);
            $extra_class .= empty($item_class)? '': ' ' . trim($item_class);
            $extra_class .= pixe_get_responsive_class($settings);
            $extra_class .= pixe_animation_class($settings);

            $atts = static::item_data(array(
                'id' => empty($settings['_id'])? '': $settings['_id'],
                'anim-location' => pixe_animation_atts($settings),
                'scroll-motion' => pixe_scroll_motion_atts($settings),
            ), $settings);

            $content = static::render_content($settings);

            $ret = '';
            if( $content === true || !empty($content) ){
                $ret .= '<div class="pixe-item' . esc_attr($extra_class) . '" ';
                $ret .= empty($settings['id'])? '': 'id="' . esc_attr($settings['id']) . '" ';
                $ret .= pixe_get_data_atts($atts);
                $ret .= ' >';
                if( $content !== true ){
                    $ret .= $content;
                }
                $ret .= '</div>';
            }
            
            return $ret;
        }

        static function item_class($settings){
            return '';
        }
        static function item_data($atts, $settings){
            return $atts;
        }
        static function render_content($settings){
            return '';
        }

    }

    add_filter('wp_ajax_pixe_render_item', 'Pixebuilder\Element\ajax_render_item');
    add_filter('wp_ajax_nopriv_pixe_render_item', 'Pixebuilder\Element\ajax_render_item');
    function ajax_render_item(){

        // check nonce
        if( !check_ajax_referer('pixe-editor', 'security', false) ){
            pixe_get_error_message('nonce', true);
            die();
        }

        global $pixe_is_admin, $pixe_request_id, $pixe_request_type;
        $builder_html_safe = '';

        if( !empty($_POST['is-admin']) ){
            $pixe_is_admin = true;
        }
        if( !empty($_POST['id']) ){
            $pixe_request_id = absint(wp_unslash($_POST['id']));
        }
        if( !empty($_POST['type']) ){
            $pixe_request_type = sanitize_text_field(wp_unslash($_POST['type']));
        }
        if( !empty($_POST['site']) ){
            global $pixe_site;
            $pixe_site = sanitize_text_field(wp_unslash($_POST['site']));
        }
        
        if( !empty($_POST['settings']) ){
            $settings = wp_unslash($_POST['settings']); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below using array_walk_recursive
            if( !is_array($settings) ) { $settings = array(); }
            array_walk_recursive($settings, 'pixe_sanitize_builder_data');
            
            $item_class = \Pixe_Builder::get_element($settings['_element']);
            if( !empty($item_class) && class_exists($item_class) ){
                $builder_html_safe = $item_class::render_content($settings);
            }
        }

        die($builder_html_safe); // phpcs:ignore WordPress.Security.EscapeOutput -- Output escaped during element rendering.
    }