<%!
from django.utils.translation import ugettext as _
from contentstore.views.helpers import xblock_studio_url
import json
%>
<%
xblock_url = xblock_studio_url(xblock)
show_inline = xblock.has_children and not xblock_url
section_class = "level-nesting" if show_inline else "level-element"
collapsible_class = "is-collapsible" if xblock.has_children else ""
label = xblock.display_name or xblock.scope_ids.block_type
messages = json.dumps(xblock.validate().to_json())
%>

<%namespace name='static' file='static_content.html'/>

<%block name="header_extras">
<script type="text/template" id="xblock-validation-messages-tpl">
    <%static:include path="js/xblock-validation-messages.underscore" />
</script>
</%block>

<script>
    require(["jquery", "js/factories/xblock_validation"], function($, XBlockValidationFactory) {
        XBlockValidationFactory(
            ${messages},
            $.parseJSON("${bool(xblock_url)}".toLowerCase()),  // xblock_url will be None or a string
            $.parseJSON("${bool(is_root)}".toLowerCase()),  // is_root will be None or a boolean
            $('div.xblock-validation-messages[data-locator="${xblock.location | h}"]')
        );
    });
</script>

% if not is_root:
    % if is_reorderable:
    <li class="studio-xblock-wrapper is-draggable" data-locator="${xblock.location | h}" data-course-key="${xblock.location.course_key | h}">
    % else:
    <div class="studio-xblock-wrapper" data-locator="${xblock.location | h}" data-course-key="${xblock.location.course_key | h}">
    % endif

    <section class="wrapper-xblock ${section_class} ${collapsible_class}">
% endif

<header class="xblock-header xblock-header-${xblock.category}">
    <div class="xblock-header-primary">
        <div class="header-details">
            % if show_inline:
            <a href="#" data-tooltip="${_('Expand or Collapse')}" class="action expand-collapse collapse">
                <i class="icon-caret-down ui-toggle-expansion"></i>
                <span class="sr">${_('Expand or Collapse')}</span>
            </a>
            % endif
            <span class="xblock-display-name">${label | h}</span>
        </div>
        <div class="header-actions">
            <ul class="actions-list">
                % if not is_root:
                    % if not show_inline:
                        <li class="action-item action-edit">
                            <a href="#" class="edit-button action-button">
                                <i class="icon-pencil"></i>
                                <span class="action-button-text">${_("Edit")}</span>
                            </a>
                        </li>
                        <li class="action-item action-duplicate">
                            <a href="#" data-tooltip="${_("Duplicate")}" class="duplicate-button action-button">
                            <i class="icon-copy"></i>
                            <span class="sr">${_("Duplicate")}</span>
                            </a>
                        </li>
                    % endif
                    <li class="action-item action-delete">
                        <a href="#" data-tooltip="${_("Delete")}" class="delete-button action-button">
                        <i class="icon-trash"></i>
                        <span class="sr">${_("Delete")}</span>
                        </a>
                    </li>
                    % if is_reorderable:
                        <li class="action-item action-drag">
                            <span data-tooltip="${_('Drag to reorder')}" class="drag-handle action"></span>
                        </li>
                    % endif
                % endif
            </ul>
        </div>
    </div>
    % if not is_root:
        <div class="wrapper-xblock-message xblock-validation-messages" data-locator="${xblock.location | h}"/>
        % if xblock_url:
        <div class="xblock-header-secondary">
            <div class="meta-info">${_('This block contains multiple components.')}</div>
            <ul class="actions-list">
                <li class="action-item action-view">
                    <a href="${xblock_url}" class="action-button">
                        ## Translators: this is a verb describing the action of viewing more details
                        <span class="action-button-text">${_('View')}</span>
                        <i class="icon-arrow-right"></i>
                    </a>
                </li>
            </ul>
        </div>
        % endif
    % endif
 </header>

% if is_root:
    <div class="wrapper-xblock-message xblock-validation-messages" data-locator="${xblock.location | h}"/>
% endif

% if is_root or not xblock_url:
    <article class="xblock-render">
    ${content}
    </article>
% else:
    <div class="xblock-message-area">
    ${content}
% endif

% if not is_root:
    </section>
    % if is_reorderable:
        </li>
    % else:
        </div>
    % endif
% endif
