<?php 
class AnalyticController extends Controller {

	/**
	* @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 getStatisticTypeByClassName($className) {
	
	  switch ($className) {
	      case 'Queue': 
	      return Statistic::StatisticTargetTypeQueue;
	      break;
	      case 'Groups' : 
	      return Statistic::StatisticTargetTypeGroup;
	      break;
	  }
	}
	
	public function getParentView($view,$params) {
  	  return $this->renderPartial('application.modules.analytics.views.default.'.$view,$params,true);
	}
	
	public function getNavBarTargetDropdown(&$model) {
	
	    $items = array();
	    $permission = new Permissions();
	    $result = $permission->getPermissionsArray(array('type'=>'statisticQueues'));
	    if (!empty($result)) {
		$queues = array();
		foreach ($result as $attributes) {
		  $queue = Queue::model()->findByPk($attributes['itemId']);
		  $queues[] = $queue;
		  if (!$model) {
		    $model = $queue;
		  } 
		}

		// FIXME ! 
		$items[] = array(
			    'label'=>__('QUEUES'),
			    'active'=>'0',
		    );
		    
		foreach ($queues as $queue) {
		    if($queue) { 
		    
		      $items[] = array(
			      'label'=>$queue->name,
			      'linkOptions'=>array('class'=>'targetLink'),
			      'url'=>url('analyticsDailyReport/default/index',array('id'=>$queue->id,'targetType'=>Statistic::StatisticTargetTypeQueue)), 
			      'active'=>'0',
		      );
		    } 
		}
	    }
	    $result = $permission->getPermissionsArray(array('type'=>'statisticGroup'));
	    if (!empty($result)) {
		$groups = array();
		foreach ($result as $attributes) {
		  $group = Groups::model()->findByPk($attributes['itemId']);
		  $groups[] = $group;
		  if (!$model) {
		    $model = $group;
		  } 
		  
		}
		if  (!empty($items)) {
		  $items[] = '---';
		} 
		$items[] = array(
			    'label'=>__('GROUPS'),
			    'active'=>'0',
		    );
		foreach ($groups as $group) {
		    if ($group) {
			$items[] = array(
			  'label'=>$group->name,
			  'url'=>url('analyticsDailyReport/default/index',array('id'=>$group->id,'targetType'=>Statistic::StatisticTargetTypeGroup)),
			  'active'=>'0',
			);
		    }
		} 
	    } 
	    if (empty($items)) {
	      $items = null;
	    } 
	    return $items;
	}
	
	public function getNavRangeDropdown($model=NULL,$defaultType ='') {
	    $items = array();
	    $rangeTypes =  array(
	      Statistic::readable(Statistic::StatisticTimeRangeTypeDay) => Statistic::StatisticTimeRangeTypeDay,
	      Statistic::readable(Statistic::StatisticTimeRangeTypeWeek) =>Statistic::StatisticTimeRangeTypeWeek,
 	      Statistic::readable(Statistic::StatisticTimeRangeTypeMonth) => Statistic::StatisticTimeRangeTypeMonth,
 	      Statistic::readable(Statistic::StatisticTimeRangeTypeYear)=> Statistic::StatisticTimeRangeTypeYear,
	    );
	    foreach ( $rangeTypes as $name => $range ) {
		$items[] = array(
		  'label'=>$name,
		  'active'=>'0',
		  'url'=> url($this->module->id.'/default/index',
		      array(
			'id'=>isset($_GET['id']) ? $_GET['id'] : isset($model) ? $model->id : '',
			'targetType'=>$this->getTargetType($model),
			'range'=> $range
		      )
		  ));
	    }
	    return $items;
	}
        
        public function getTargetType($model = null)
        {

            $targetType = Statistic::StatisticTargetTypeGroup;
            if (isset($_GET["targetType"])) {
                return $_GET["targetType"];
            }

            if ($model) {
                return $this->getStatisticTypeByClassName(get_class($model));
            }

            return $targetType;
        }
	
	public function actionCreateReport(){
	
		$attributes = array();
		
		
		if (isset($_GET['id'])) {
		  $targetId = $_GET['id'];
		} else {
		  $targetId = '';
		}
		// process Range Type 
		if (isset($_GET['range'])) {
// 		  dump($_GET['range']);
		  $range = $_GET['range'];
		} else {
		  $range = Statistic::StatisticTimeRangeTypeDay;
		}
		
		// parse DATE 
		if (isset($_GET['date'])) {
		
		  $DOY = $_GET['date'];
		  


		  switch($range) {
		  case Statistic::StatisticTimeRangeTypeDay:
		      $DOY = strtotime($DOY);
		      $year = date('Y',$DOY);		      
		      $DOY = date('z',$DOY)+1;
		      break;
		  case Statistic::StatisticTimeRangeTypeWeek:

		      $year = $DOY;
		      $week = isset($_GET['week']) ? ($_GET['week']) : date('W');
		      $week = strlen($week) < 2 ? "0".$week : $week;
              $DOY =   date('Y-m-d',strtotime($year.'-W'.$week.'-1'));
		      break;
		  case Statistic::StatisticTimeRangeTypeMonth:
                
              $delimiter =substr(phpUserDatePattern(),1,1);
		      $dateData = explode($delimiter,$DOY);
		      if ($delimiter == '-') {
                $dateData = array_reverse($dateData);
		      }
              $month = $dateData[0];
              $year = $dateData[1];
              
		      break;
		  case Statistic::StatisticTimeRangeTypeYear:
		      $year =  isset($_GET['date']) ? $_GET['date'] : date('Y');
		      break;
		  
		  }

		} else {
		  $DOY = date('z')+1;
		}

		
		switch($range) {
		  case Statistic::StatisticTimeRangeTypeDay :
		  $output = $this->getDailyOutput($year,$targetId,$DOY);
		  break;
		  case Statistic::StatisticTimeRangeTypeWeek:
		      $output = $this->getWeeklyOutput($year,$targetId,$DOY);
		      break;
		  case Statistic::StatisticTimeRangeTypeMonth:
		      $output = $this->getMonthlyOutput($year,$targetId,$month);
		      break;
		  case Statistic::StatisticTimeRangeTypeYear:
		      $output = $this->getYearlyOutput($year,$targetId);
		      break;
		  
		  default:
		      $output = $this->getDailyOutput($year,$targetId,$DOY);
		      break;
		  
		}
		
//     		echo $this->getRandomOoutput();
		echo($output);

	}
	
	public function getRandomOoutput() {
	    $output = "State,".__('ANALYTIC_LEGEND_SUCCESSFULL').",".__('ANALYTIC_LEGEND_TIMEOUT').",".__('ANALYTIC_LEGEND_NO_AGENTS').",".__('ANALYTIC_LEGEND_BREAK_OUT');
	    for($i=1;$i<=8;$i++){
		$output.= "\nWarteschlange".$i.",".rand(0, 84).",".rand(0, 84).",".rand(0, 84).",".rand(0, 84);
	    }
	    return $output;
	}

	public function actionAjaxGetWeekDropdown() {
        $week = isset($_POST['week']) ? $_POST['week'] : (int)date('W');
        $year = isset($_POST['year']) ? $_POST['year'] : date('Y');
        echo $this->renderPartial('application.modules.analytics.views.default.weekDropdown',array('year'=>$year,'week'=>$week),true);
	}
	
	public function getDailyOutput($year,$targetId,$DOY) {
	}
	
	public function getWeeklyOutput($year,$targetId,$DOY ) {
	}
	
	
	public function getMonthlyOutput($year,$targetId,$month) {
	}
	
	public function getYearlyOutput($year,$targetId) {  
	}
	
	
	public function getAvailableModels() {
        return array(
            'Groups',
            'Queue'
        );
	}
}