<?php

class DefaultController extends AnalyticController
{

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    public function accessRules()
    {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'getAjaxDataForQueue'),
                'expression' => AccessManager::checkSessionPermission($this->module->name),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionIndex()
    {
        $model = new AdvancedQueueReport();
        $data = $this->getQueueModels();

        $queueModel = new Queue();
        $defaultType = Statistic::StatisticTargetTypeQueue;
        $rangeDropdown = $this->getNavRangeDropdown($queueModel, $defaultType);

        $this->render('index', array('model' => $model, 'data' => $data, 'rangeDropdown' => $rangeDropdown));
    }

    protected function getQueueModels()
    {
        $groupId = app()->user->sudoUserInstance->group;
        $permission = new Permissions();
        $result = $permission->getPermissionsArray(array('type' => 'advStatisticQueues', 'groupId' => $groupId), true);
        $data = array();
        if (!empty($result)) {
            foreach ($result as $attributes) {
                $data[] = Queue::model()->findByPk($attributes['itemId']);
            }
        }
        usort($data, array($this, "cmp"));
        return $data;
    }

    protected function cmp($a, $b)
    {
        return strcmp($a->name, $b->name);
    }

    public function renderAgent($data)
    {
        return $data['name'];
    }

    public function renderDatetimeFormat($data, $key, $value)
    {
        if ($data[$value->name] === 'n/a') {
            return $data[$value->name];
        }

        if ($data[$value->name] !== '') {
            return $this->getFormatedDatetime($data[$value->name]);
        }
        return '';
    }

    public function renderSuccessRateFormat($data, $key, $value)
    {
        return $data[$value->name] . ' %';
    }

    public function renderReadyTime($data, $key, $value)
    {
        return "<div title='" . $data['loginLogoutTime'] . "'>" . $this->renderDatetimeFormat($data, $key, $value) . "<div>";
    }

    protected function getFormatedDatetime($valueInSeconds)
    {
        $hours = floor(intval($valueInSeconds) / 3600);
        $result = str_pad($hours, 2, "0", STR_PAD_LEFT) . ':';
        $minutes = floor((intval($valueInSeconds) / 60) % 60);
        $result.= str_pad($minutes, 2, "0", STR_PAD_LEFT) . ':';
        $seconds = floor(intval($valueInSeconds) % 60);
        $result.= str_pad($seconds, 2, "0", STR_PAD_LEFT);
        return $result;
    }

    public function actionGetAjaxDataForQueue()
    {
        $queue_id = @$_REQUEST['queue_id'];
        $grid_key = @$_REQUEST['grid_key'];
        $time_range = @$_REQUEST['time_range'];
        $time_picker_value = @$_REQUEST['time_picker_value'];

        if (!empty($queue_id) && isset($grid_key) && isset($time_range) && isset($time_picker_value)) {
            $advModel = new AdvancedQueueReport();
            $model = Queue::model()->findByPk($queue_id);
            $timePickerValue = $this->getFormatedTimePickerValue($time_range, $time_picker_value);
            $data = $advModel->getQueueReportData($model, $time_range, $timePickerValue);
            $this->renderPartial('_gridContent', array(
                'model' => $advModel,
                'data' => $data,
                'grid_key' => $grid_key
            ));
        }
    }

    protected function getFormatedTimePickerValue($time_range, $time_picker_value)
    {
        $result = $time_picker_value;
        $userPatern = PhonalisaDate::userDatePattern();

        if ($time_range == Statistic::StatisticTimeRangeTypeDay) {
            $result = PhonalisaDate::convertServerDateTo('d.m.Y', PhonalisaDate::newServerDate($time_picker_value, $userPatern, true));
        } else if ($time_range == Statistic::StatisticTimeRangeTypeMonth) {
            if ($userPatern == 'Y-m-d') {
                $userPatern = substr($userPatern, 0, -2);
            } else {
                $userPatern = substr($userPatern, -3);
            }
            $result = PhonalisaDate::convertServerDateTo('m.Y', PhonalisaDate::newServerDate($time_picker_value, $userPatern, true));
        }
        return $result;
    }
}
