<?php

require_once(MH_DIR_FS_CATALOG . MH_ROOT_PATH . 'common/functions/compatibility.php');
require_once(MH_DIR_CONFIG . 'config_customer_insight/functions/general.php');
require_once(MH_DIR_CONFIG . 'config_customer_insight/classes/notificationbeez.php');
require_once(MH_DIR_CONFIG . 'config_customer_insight.php');
require_once(MH_DIR_CONFIG . 'config_customer_insight/functions/general.php');
require_once(MH_DIR_CONFIG . 'config_customer_insight/classes/viewbeez.php');


mh_define('MH_BYPASS_TEMPLATE', TRUE);

if (mh_get('panel_action')) {

    switch (mh_get('panel_action')) {
        case 'H':
            $notficationsObj = new notificationbeez();
            $notficationsObj->hide(mh_get('notification_id'));
            // todo
            // return ajax success
            break;
        case 'refresh':
            // load all customer insight view modules
            $mailbeez_insight_all_modules = mh_insight_view(null, false);
            $panel_cnt_open = isset($GLOBALS['view_conversations']) ? $GLOBALS['view_conversations']->cnt_open() : 0;
            $panel_cnt_notification = 0;
            if (isset($GLOBALS['view_notifications']) && is_object($GLOBALS['view_notifications'])) {
                $GLOBALS['view_notifications']->data_update();
                $panel_cnt_notification = $GLOBALS['view_notifications']->cnt_notifications();
            }

            $cnt = $panel_cnt_open + $panel_cnt_notification;

            $all_modules_panel = $mailbeez_insight_all_modules;

            $view_panel = array();
            foreach ($all_modules_panel as $sort_order_key => $file) {
                $class = substr($file, 0, strrpos($file, '.'));

                /*
                if ($class == $this->code) {
                    continue;
                }
                */

                $view_module = $GLOBALS[$class];
                if ($view_module->panel_ajax_refresh == false) {
                    continue;
                }


                $start_time = microtime(true);
                $view_module->data_update();
                $panel_content = $view_module->get_view_panel();

                mh_log("processing panel module: $class in %s", microtime(true) - $start_time);

                if ($panel_content == false) {
                    continue;
                }
                $view_panel[$view_module->code] = array('title' => $view_module->title,
                    'panel' => base64_encode(($panel_content)),
                    'code' => $view_module->code,
                    'icon_path' => $view_module->get_icon_path(),
                    'link' => $view_module->get_menu_link()
                );
            }
            // $json_data = json_encode(array('cnt' => $cnt, 'panel' => $view_panel), JSON_FORCE_OBJECT);
            // php < 5.3
            // http://stackoverflow.com/questions/2634009/php-need-json-encode-5-3-functionality-in-5-2
            $json_data = json_encode((object)array('cnt' => $cnt, 'panel' => $view_panel));
            echo $json_data;
            break;
    }


}
