<?php

use eftec\bladeone\BladeOne;

$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
if(!defined("BLADEONE_MODE")) {
    define("BLADEONE_MODE", 1); // (optional) 1=forced (test),2=run fast (production), 0=automatic, default value.
}
$blade = new BladeOne($views, $cache);

$ribbon_promo_theme = "swiper";
$ribbon_promo_buttons = array();
$ribbon_promo_settings = [
    "slides_per_view" => 5,
    "effect" => null
];
$ribbon_promo_pages = isset($module_params['ribbon_promo_pages']) ? $module_params['ribbon_promo_pages'] : array();
$ribbon_promo_pages_link = isset($module_params['ribbon_promo_pages_link']) ? $module_params['ribbon_promo_pages_link'] : array();
$ribbon_promo_pages_icons = isset($module_params['ribbon_promo_pages_icons']) ? $module_params['ribbon_promo_pages_icons'] : 0;
if( isset($module_params['ribbon_promo_theme']) && $module_params['ribbon_promo_theme'] != "" ) {
    $ribbon_promo_theme = $module_params['ribbon_promo_theme'];
}

if( isset($module_params['ribbon_promo_settings']) && is_array($module_params['ribbon_promo_settings']) ) {
    $ribbon_promo_settings = array_merge($ribbon_promo_settings, $module_params['ribbon_promo_settings']);
}

//echo '<pre>', var_export($module_params, true), '</pre>';
//echo '<pre>', var_export($ribbon_promo_settings, true), '</pre>';


require_once(__DIR__ . "/themes/{$ribbon_promo_theme}/theme_includes.php");
/*$optres['css'][] = $module_css_path . "amodules/ribbon_promo_pages/themes/{$ribbon_promo_theme}/css/{$ribbon_promo_theme}_promo_ribbon.css";
$optres['js'][] = $path . "amodules/ribbon_promo_pages/themes/{$ribbon_promo_theme}/js/{$ribbon_promo_theme}_promo_ribbon.js";*/

$optres['css'][] = $module_css_path . "amodules/ribbon_promo_pages/css/{$ribbon_promo_theme}-promo_ribbon.css";
$optres['js'][] = $path . "amodules/ribbon_promo_pages/js/{$ribbon_promo_theme}-promo_ribbon.js";



if(is_array($ribbon_promo_pages) && count($ribbon_promo_pages) > 0 ) {
    $ribbon_promo_catdoms = implode(", ", $ribbon_promo_pages);
    $q = "SELECT * FROM domaincategories WHERE parent_id='$domain_id' AND catdom_id IN ($ribbon_promo_catdoms) AND is_promo_cat='1' AND active='yes' ORDER BY order_seq ASC;";
    //echo $q;
    $r = @mysql_query($q);
    if ($r && mysql_num_rows($r) > 0) {
        while ($row = mysql_fetch_assoc($r)) {
            $ribbon_promo_buttons[$row['catdom_id']] = array_map('stripslashes', $row);
        }
    }
}
//echo '<pre>', var_export($ribbon_promo_buttons, true), '</pre>';
if (is_array($ribbon_promo_buttons) && count($ribbon_promo_buttons) > 0) {
    $num_elements = count($ribbon_promo_buttons) + 0;
    $ribbon_promo_cnt = 1;
    $data_merge = 1;
    if($num_elements < 6) {
        $data_merge = (5 % $num_elements == 0) ? 5 / $num_elements : 5 / $num_elements;
    }
    $cnt = 0;
    foreach ($ribbon_promo_buttons as $ribbon_promo_id => $ribbon_promo_data) {
        $extra_data = [];
        $extra_data['ribbon_promo_url'] = "$base_url/" . $ribbon_promo_data['friendly_url'] . '.html';
        $extra_data['ribbon_promo_title'] = preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $ribbon_promo_data['title_el']));

        if (isset($ribbon_promo_pages_link[$ribbon_promo_id]) && trim($ribbon_promo_pages_link[$ribbon_promo_id]) != "") {
            $extra_data['ribbon_promo_title'] = preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $ribbon_promo_pages_link[$ribbon_promo_id]));
        }

        $extra_data['ribbon_promo_title_alt'] = trim(preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $ribbon_promo_data['title_el_alt'])));

        if ($extra_data['ribbon_promo_title_alt'] == "") {
            $extra_data['ribbon_promo_title_alt'] = $extra_data['ribbon_promo_title'];
        }

        // Check for the icon
        $extra_data['icon'] = null;
        if(trim($ribbon_promo_data['logo_picture']) != "") {
            $icon_filename = $ribbon_promo_data['logo_picture'];
            $icon_file = __DIR__ . "/../../images/promos/{$ribbon_promo_data['parent_id']}/{$ribbon_promo_data['catdom_id']}/icons/$icon_filename";
            if(file_exists($icon_file)) {
                $extra_data['icon'] = $path . "images/promos/{$ribbon_promo_data['parent_id']}/{$ribbon_promo_data['catdom_id']}/icons/$icon_filename";
            }
        }

        // Add delay
        $extra_data['delay'] = $cnt == 0 ? 1000 : 3500;

        $ribbon_promo_buttons[$ribbon_promo_id] = array_merge($ribbon_promo_buttons[$ribbon_promo_id], $extra_data);


        if (strpos($_SERVER['REQUEST_URI'], $extra_data['ribbon_promo_url']) !== false) {
            unset($ribbon_promo_buttons[$ribbon_promo_id]);
        }

        $cnt++;
    }

    $debug_promos = [
        "ribbon_promo_buttons" => $ribbon_promo_buttons,
        "use_icons" => $ribbon_promo_pages_icons == 1,
        "num_elements" => $num_elements,
        "data_merge"    => $data_merge,
        "settings"  => $ribbon_promo_settings,
        "id"            => "ribbon-" . number_format(microtime(true), 0, '', '') . rand(1000, 9999)
    ];

    if(isset($_REQUEST['debug_promos'])) {
        echo '<pre>', var_export($debug_promos, true), '</pre>';
    }

    $promo_ribbon_view = $blade->run("$ribbon_promo_theme/ribbon_default",
        [
            "ribbon_promo_buttons" => $ribbon_promo_buttons,
            "use_icons" => $ribbon_promo_pages_icons == 1,
            "num_elements" => $num_elements,
            "data_merge"    => $data_merge,
            "settings"  => $ribbon_promo_settings,
            "id"            => "ribbon-" . number_format(microtime(true), 0, '', '') . rand(1000, 9999)
        ]);

    echo $promo_ribbon_view;

}