<?php
namespace Avirtum\GutenDraw\System;

defined( 'ABSPATH' ) || exit;

use Avirtum\GutenDraw\Models\DataModel;

class Shortcode {
    public function __construct() {
        add_action( 'init', [ $this, 'init' ] );
    }

    public function init() {
        add_shortcode( GUTENDRAW_SHORTCODE_NAME, [ $this, 'run' ] );
    }

    public function run( $attributes = [] ) {
        wp_enqueue_style( 'gutendraw-shortcode', GUTENDRAW_PLUGIN_URL . 'assets/css/shortcode.css', [], GUTENDRAW_PLUGIN_VERSION, 'all' );
        wp_enqueue_script( 'gutendraw-shortcode', GUTENDRAW_PLUGIN_URL . 'assets/js/shortcode.js', [], GUTENDRAW_PLUGIN_VERSION, false );
        wp_localize_script( 'gutendraw-shortcode', 'GutendrawShortcode', $this->getGlobals() );

        return self::render( $attributes );
    }

    public function getGlobals() {
        return [
            'api' => [
                'public_url' => esc_url_raw( rest_url( GUTENDRAW_PLUGIN_PUBLIC_REST_URL ) )
            ]
        ];
    }

    public static function render( $attributes = [] ) {
        $attributes = array_change_key_case( $attributes, CASE_LOWER );
        $defaults = [
            'id' => null,
            'title' => false,
            'class' => null
        ];
        $attributes = shortcode_atts( $defaults, $attributes );

        $id = null;
        $title = false;
        $class = null;

        foreach ( $attributes as $key => $value ) {
            if ( !$key || empty($value) ) {
                continue;
            }

            switch ( $key ) {
                case 'id': { $id = intval( $value, 10 ); } break;
                case 'title': { $title = boolval( $value ); } break;
                case 'class': { $class = sanitize_html_class( $value ); } break;
            }
        }

        if ( empty( $id ) ) {
            return '<div class="components-notice is-warning"><div class="components-notice__content"><p>' . esc_html__( 'GutenDraw diagram is not selected.', 'gutendraw' ) . '</p></div></div>';
        }

        $item = DataModel::getItem( $id );
        if ( empty( $item ) ) {
            return '<div class="components-notice is-error"><div class="components-notice__content"><p>' . esc_html__( 'GutenDraw diagram not found.', 'gutendraw' ) . '</p></div></div>';
        }

        $image = DataModel::getItemImage( $id );
        if ( empty( $image ) ) {
            return '<div class="components-notice is-error"><div class="components-notice__content"><p>' . esc_html__( 'GutenDraw diagram image not found.', 'gutendraw' ) . '</p></div></div>';
        }

        if( !preg_match('/<svg[^>]+>/i', $image, $matches )) {
            return '<div class="components-notice is-error"><div class="components-notice__content"><p>' . esc_html__( 'GutenDraw diagram has the wrong format.', 'gutendraw' ) . '</p></div></div>';
        }
        $image = $matches[0] . '</svg>';

        $classes = ['gutendraw'];
        if ( !empty( $class ) ) {
            $classes[] = $class;
        }

        $data  = '<div class="' . esc_attr( implode( ' ', $classes ) ) . '" data-id="' . esc_attr( $id ) . '" >';
        $data .= '<div class="gutendraw-image">';
        $data .= $image;
        $data .= '</div>';
        if ( $title ) {
            $data .= '<div class="gutendraw-title">';
            $data .= $item['title'];
            $data .= '</div>';
        }
        $data .= '</div>';

        return $data;
    }
}