<?php
    $tmp = '';
    $html = '';
    $additionalClass = '';
    // $html .= "<h2>Magic Slideshow Settings</h2><br/>";
    // $html .= "<p style=\"font-size:15px;\">Learn about all the <a href=\"http://www.magictoolbox.com/magicslideshow/integration/\" target=\"_blank\">Magic Slideshow&trade; settings and examples too!</a>&nbsp;|&nbsp;<a href=\"http://www.magictoolbox.com/contact/\">Get support</a></p>";

    $pageURL = get_admin_url(null, 'admin.php?page=WordPressMagicSlideshow-shortcodes-page');
    $shortcode_img_url = preg_replace('/\/magicslideshow\/constructor\//is', '/magicslideshow/', plugin_dir_url( __FILE__ ));
    $shortcode_img_url .= 'core/admin_graphics/icon.svg';

    $html .= "<h1>Magic Slideshow shortcodes</h1>";
    $html .= "<p>Create Magic Slideshow below, to insert with the <img src=\"".$shortcode_img_url."\" alt=\"Magic Slideshow\" style=\"width:30px; vertical-align: middle;\"/> shortcut into any page or post.</p>";
    $html .= "<p style=\"margin-right:20px; float:right; font-size:15px; white-space: nowrap;\">";

    $html .= "&nbsp;<a href=\"";
    $html .= WordPressMagicSlideshow_url('http://www.magictoolbox.com/magicslideshow/modules/wordpress/',' configuration page resources settings link');
    $html .= "\" target=\"_blank\">Documentation<span class=\"dashicons dashicons-share-alt2\" style=\"text-decoration: none;line-height:1.3;margin-left:5px;\"></span></a>&nbsp;|";

    $html .= "&nbsp;<a href=\"";
    $html .= WordPressMagicSlideshow_url('http://www.magictoolbox.com/magicslideshow/examples/',' configuration page resources examples link');
    $html .= "\" target=\"_blank\">Examples<span class=\"dashicons dashicons-share-alt2\" style=\"text-decoration: none;line-height:1.3;margin-left:5px;\"></span></a>&nbsp;|";

    $html .= "&nbsp;<a href=\"";
    $html .= WordPressMagicSlideshow_url('http://www.magictoolbox.com/contact/','configuration page resources support link');
    $html .= "\" target=\"_blank\">Support<span class=\"dashicons dashicons-share-alt2\" style=\"text-decoration: none;line-height:1.3;margin-left:5px;\"></span></a>&nbsp;";

    $html .= "|&nbsp;<a href=\"";
    $html .= WordPressMagicSlideshow_url('http://www.magictoolbox.com/buy/magicslideshow/','configuration page resources buy link');
    $html .= "\" target=\"_blank\">Buy<span class=\"dashicons dashicons-share-alt2\" style=\"text-decoration: none;line-height:1.3;margin-left:5px;\"></span></a>";
    $html .= "</p><br/><br/><br/>";

    $html .= '<div class="magic-starts-here">';

    $html .= '<div class="tool-cell new-magic-tool"><div class="pro"></div><a href="'.$pageURL.'&id=new" class="simple"></a></div>';

    $table_data = magictoolbox_WordPress_MagicSlideshow_get_saved_data();
    if ($table_data && count($table_data) > 0) {
        foreach($table_data as $value) {
            $tmpid = $value->id;
            $tmpname = $value->name;
            $label = '';
            $tag = 'div';
            $link = '';

            if (magictoolbox_WordPress_MagicSlideshow_is_simple($value->html)) {
                $tag = 'a';
                $link = ' href="'.$pageURL.'&id='.$tmpid.'"';
                $additionalClass = ' simple';
                $allToolOptions = magictoolbox_WordPress_MagicSlideshow_parse_saved_options($value->saved_data);
                $allToolOptions = magictoolbox_WordPress_MagicSlideshow_parse_option_from_string($allToolOptions['additionalOptions']);
                $tmpurl = $allToolOptions['firstImage'];
                $tmpurl = wp_get_attachment_url($tmpurl);
            } else {
                $additionalClass = '';
                $tmpurl = $value->html;

                preg_match('/<\!\[magicslideshow\[([0-9]+)\]\]>/', $tmpurl, $tmpurl);
                if (count($tmpurl) > 0) {
                    $tmpurl = wp_get_attachment_url($tmpurl[1]);
                } else {
                    $tmpurl = '';
                }

                $label = '<div class="label">advanced</div>';
            }

            $text = ' ';

            if ('' == $tmpurl) {
                $text = '';
                $text = 'data-face-text=';
                $all_text = json_decode(json_decode('"'.$value->saved_data.'"'))[0];

                foreach ($all_text as $slide) {
                    foreach ($slide->blocks as $block) {
                        if ('text' == $block->type) {
                            $tmpurl .= " ";
                            $tmpurl .= $block->content;
                        }
                    }
                }

                $tmpurl = preg_replace('/<\s*\/?\s*[^>]*>/is', '', $tmpurl);

                if('' == $tmpurl) {
                    $tmpurl = 'magictoolbox any device magic amazing enjoy simplicity responsive design easy magicslideshow support automatically scale slide block effect fade responsive cube flip friendly bars3d blinds3d';
                }

                $text .= ('"'.$tmpurl.'"');
                $tmpurl = '';
            }

            $tmp .= '<'.$tag.$link.' class="tool-cell'.$additionalClass.'" tool-id="'.$tmpid.'">'.$label.
                        '<button class="copy-magic-tool magic-button dashicons dashicons-admin-page" title="Duplicate"></button>'.
                        '<button class="remove-magic-tool magic-button dashicons dashicons-no-alt" title="Remove"></button>'.
                        '<div class="tool-face" style="background-image: url('.$tmpurl.');" '.$text.'></div>'.
                        '<span class="tool-name">'.$tmpname.'</span>'.
                    '</'.$tag.'>';
        }
    }

    $html .= $tmp;
    $html .= '</div>';
    echo $html;
?>
