<?php

class DefaultController extends AnalyticController
{

    public function accessRules()
    {

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

    public function actionIndex()
    {
        $model = new Queue();
        $defaultType = Statistic::StatisticTargetTypeQueue;
        $this->render('index', array('rangeDropdown' => $this->getNavRangeDropdown($model, $defaultType)));
    }

    public function actionCreateReport()
    {
        return parent::actionCreateReport();
    }

    public function getQueues()
    {
        $queues = array();
        $permission = new Permissions();

        $user = app()->user->sudoUserInstance;
        if (!$user) {
            app()->user->logout();
            app()->user->loginRequired();
        }

        $result = $permission->getPermissionsArray(array('groupId' => $user->group, 'type' => 'statisticQueues'), true);

        foreach ($result as $permissionAttributes) {
            $queues[] = Queue::model()->findByPk($permissionAttributes['itemId']);
        }

        return $queues;
    }

    public function generateQueueOutput($year, $DOY, $rangeType)
    {
        // get all queues - FIXME: need some permission here!
        $queues = $this->getQueues();

        switch ($rangeType) {
            case Statistic::StatisticTimeRangeTypeDay:
                $rangeAttribute = 'day';
                break;
            case Statistic::StatisticTimeRangeTypeWeek:
                $rangeAttribute = 'week';
                $DOY = (int) date('W', strtotime($DOY));

                break;
            case Statistic::StatisticTimeRangeTypeMonth:
                $rangeAttribute = 'month';
                break;
            case Statistic::StatisticTimeRangeTypeYear:
                $rangeAttribute = 'year';
                break;
        }

        $output = "State,";
        $output .= __('ANALYTIC_LEGEND_SUCCESSFULL') . ",";
        $output .= __('ANALYTIC_LEGEND_TIMEOUT') . ",";
        $output .= __('ANALYTIC_LEGEND_NO_AGENTS') . ",";
        $output .= __('ANALYTIC_LEGEND_BREAK_OUT') . "\n";

        foreach ($queues as $queue) {
            $attributes = array();
            $attributes[$rangeAttribute] = (int) $DOY;
            $attributes["year"] = $year;
            $attributes["target_id"] = $queue->id;
            $attributes["time_range_type"] = $rangeType;
            $attributes["target_type"] = Statistic::StatisticTargetTypeQueue;
            $stat = Statistic::model()->findByAttributes($attributes);

            if ($stat == null) {
                $stat = new Statistic();
            }
            $this->overwriteEmptyAttributes($stat);

            $output .= str_replace(',', '', $queue->name);
            $output .= isset($stat->counter_success) ? "," . $stat->counter_success : ',0';
            $output .= isset($stat->counter_missed_timeout) ? "," . $stat->counter_missed_timeout : ',0';
            $output .= isset($stat->counter_missed_no_agents) ? "," . $stat->counter_missed_no_agents : ',0';
            $output .= isset($stat->counter_missed) ? "," . $stat->counter_missed : ',0';
            $output .= "\n";
        }
        return $output;
    }

    // fix for statistic models wich wehre creatated by the Web-UI
    public function overwriteEmptyAttributes(&$stat)
    {
        $attributes = array('counter_success', 'counter_missed_timeout', 'counter_missed_no_agents', 'counter_missed');
        foreach ($attributes as $attribute) {
            if ($stat->$attribute === '') {
                $stat->$attribute = '0';
            }
        }
    }

    public function getDailyOutput($year, $targetId, $DOY)
    {
        return $this->generateQueueOutput($year, $DOY, Statistic::StatisticTimeRangeTypeDay);
    }

    public function getWeeklyOutput($year, $targetId, $DOY)
    {
        return $this->generateQueueOutput($year, $DOY, Statistic::StatisticTimeRangeTypeWeek);
    }

    public function getMonthlyOutput($year, $targetId, $month)
    {
        return $this->generateQueueOutput($year, $month, Statistic::StatisticTimeRangeTypeMonth);
    }

    public function getYearlyOutput($year, $targetId)
    {
        return $this->generateQueueOutput($year, $year, Statistic::StatisticTimeRangeTypeYear);
    }
}
