<?php
/**
 * @package CleverOgre
 * @subpackage OgreAlert
 * @since OgreAlert 0.1.7
 */

if (!defined('ABSPATH')) exit;

if (!isset($id) || !\OgreAlert\Alert::valid($id)) return;

// Initialize Attribute Arrays
$data = array(
    'id' => $id,
);
$classes = array(
    'ogrealert-message',
);
$style = array(
    'display' => 'none',
);

// Priority
$priority = get_post_meta($id, '_ogrealert_priority', true);
if (empty($priority)) $priority = 'normal';
$data['priority'] = $priority;
$classes[] = "ogrealert-message__priority-{$priority}";
$style['background-color'] = \OgreAlert\Settings::get("message_{$priority}_background_color");
$style['color'] = \OgreAlert\Settings::get("message_{$priority}_text_color");

// Dismiss Duration
$dismiss_type = get_post_meta($id, '_ogrealert_duration', true);
if (empty($dismiss_type)) $dismiss_type = \OgreAlert\Settings::get('message_dismiss_duration');
$dismiss_duration = \OgreAlert\Settings::parse_dismiss_duration($dismiss_type);
$data['dismiss-type'] = $dismiss_type;
$data['dismiss-duration'] = $dismiss_duration;
$classes[] = "ogrealert-message__dismiss-{$dismiss_type}";

// Filter Attribute Arrays
$data = apply_filters('ogrealert/message_template_data', $data, $id);
$classes = apply_filters('ogrealert/message_template_classes', $classes, $id);
$style = apply_filters('ogrealert/message_template_style', $style, $id);

// Format data attributes
array_walk($data, function (&$value, $prop) {
    $value = "data-{$prop}=\"" . esc_attr($value) . '"';
});

// Format styling
array_walk($style, function (&$value, $prop) {
    $value = "{$prop}: {$value};";
});

?>
<li class="ogrealert-messages__list-item">
    <article id="ogrealert-<?php esc_attr_e($id); ?>" <?php echo implode(' ', $data); ?> class="<?php esc_attr_e(implode(' ', $classes)); ?>" style="<?php esc_attr_e(implode(' ', $style)); ?>">
        <h3 class="ogrealert-title screen-reader-text"><?php esc_html_e(get_the_title($id)); ?></h3>
        <div class="ogrealert-container" style="width: <?php esc_attr_e(\OgreAlert\Settings::get('message_container')); ?>px;">
            <div class="ogrealert-content content"><?php \OgreAlert\Alert::get_content($id, true); ?></div>
        </div>
        <?php if ($dismiss_type != 'none') { ?>
        <a class="ogrealert-action ogrealert-dismiss" href="#" title="<?php esc_attr_e('Dismiss message', 'ogrealert'); ?>"><?php esc_html_e('Dismiss', 'ogrealert'); ?></a>
        <?php } ?>
    </article>
</li>
