<?php

App::uses('AppController', 'Controller');

/**
 * Analytics Controller
 *
 * @property Analytic $Analytic
 */
class WidgetController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
    }

    public $components = array(
        'Highcharts.Highcharts',
    );

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_wgaccess_month() {
        $this->loadModel('Analytic');
        $this->Analytic->recursive = -1;

        $mesAnterior = (int) (date('m')) - 1;

        // grafico
        $dbreturn = $this->Analytic->find('all', array(
            'group' => "DATE_FORMAT(Analytic.created,'%m-%d')",
            'fields' => array(
                'count(Analytic.page_title) AS total',
                "DATE_FORMAT(Analytic.created,'%d-%m') AS created"
            ),
            'conditions' => array(
                'Analytic.page_slug NOT LIKE' => '%admin%',
                'Analytic.page_title NOT LIKE' => '%nalytics%',
                'Analytic.created >=' => date('Y-' . $mesAnterior . '-1 00:00')
            ),
            'order' => 'Analytic.created',
        ));

        $visitanteUnico = $this->Analytic->find('all', array(
            'group' => "DATE_FORMAT(Analytic.created,'%m-%d')",
            'fields' => array(
                'count(distinct Analytic.ip) AS total',
                "DATE_FORMAT(Analytic.created,'%d-%m') AS created"
            ),
            'conditions' => array(
                'Analytic.page_slug NOT LIKE' => '%admin%',
                'Analytic.created >=' => date('Y-' . $mesAnterior . '-1 00:00')
            ),
            'order' => 'Analytic.created',
        ));

        $axisX = array();
        $data = array();
        $visitantes = array();
        foreach ($dbreturn as $datas) {
            array_push($axisX, $datas[0]['created']);
            array_push($data, (int) $datas[0]['total']);
        }

        foreach ($visitanteUnico as $visitante) {
            array_push($visitantes, (int) $visitante[0]['total']);
        }

        $chartName = 'total';

        $chart = $this->Highcharts->create($chartName, 'line');
        $label = 'Total de acessos no Mês / por Dia';
        $this->Highcharts->setChartParams($chartName, array(
            'renderTo' => 'piewrapper',
            'title' => $label,
            'titleAlign' => 'left',
            'titleX' => 20,
            'titleY' => 20,
            'legendEnabled' => true,
            'legendLayout' => 'horizontal',
            'legendAlign' => 'center',
            'legendVerticalAlign ' => 'bottom',
            'tooltipEnabled' => false,
            'xAxisLabelsEnabled' => true,
            'xAxisLabelsAlign' => 'right',
            'xAxisLabelsStep' => 1,
            'xAxislabelsX' => 5,
            'xAxisLabelsY' => 20,
            'xAxisCategories' => $axisX,
            'enableAutoStep' => false,
            'creditsEnabled' => false,
        ));

        $line = $this->Highcharts->addChartSeries();
        $line->type = 'spline';
        $line->addName('Page Views')
                ->addColor('#205081')
                ->addData($data);

        $line2 = $this->Highcharts->addChartSeries();
        $line2->type = 'spline';
        $line2->addName('Visitantes Unicos')
                ->addColor('#008548')
                ->addData($visitantes);

        $chart->addSeries($line);
        $chart->addSeries($line2);

        $this->set(compact('chartName'));
        $this->set('label', $label);
        $this->set('axisX', $axisX);
        $this->set('data', $data);
    }

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_wgtotal_visitors() {
        $this->loadModel('Analytic');
        $this->Analytic->recursive = -1;

        $mesAnterior = (int) (date('m')) - 1;

        // grafico
        $dbreturn = $this->Analytic->find('all', array(
            'group' => "DATE_FORMAT(Analytic.created,'%m-%d-%Y')",
            'fields' => array(
                'count(Analytic.page_title) AS total',
                "DATE_FORMAT(Analytic.created,'%d-%m-%Y') AS created"
            ),
            'conditions' => array(
                'Analytic.page_slug NOT LIKE' => '%admin%',
                'Analytic.page_title NOT LIKE' => '%nalytics%',
                'Analytic.created >=' => date('Y-' . $mesAnterior . '-1 00:00')
            ),
            'order' => 'Analytic.created',
        ));

        $visitanteUnico = $this->Analytic->find('all', array(
            'group' => "DATE_FORMAT(Analytic.created,'%m-%d-%Y')",
            'fields' => array(
                'count(distinct Analytic.ip) AS total',
                "DATE_FORMAT(Analytic.created,'%d-%m-%Y') AS created"
            ),
            'conditions' => array(
                'Analytic.page_slug NOT LIKE' => '%admin%',
                'Analytic.created >=' => date('Y-' . $mesAnterior . '-1 00:00')
            ),
            'order' => 'Analytic.created',
        ));

        $total = 0;
        foreach ($dbreturn as $datas) {
            $total += (int) $datas[0]['total'];
        }

        $newVisitors = 0;
        foreach ($visitanteUnico as $visitante) {
            $newVisitors += (int) $visitante[0]['total'];
        }
        $returningVisitors = $total - $newVisitors;

        $data = array(
            array(
                'name' => 'Visitantes Novos',
                'y' => $newVisitors,
                'color' => '#008548',
                'sliced' => true,
                'selected' => true
            ), array(
                'name' => 'Retornos',
                'y' => $returningVisitors,
                'color' => '#205081'
            )
        );

        $chartName = 'total';

        $chart = $this->Highcharts->create($chartName, 'pie');
        $label = 'Visitantes No Mês';
        $this->Highcharts->setChartParams($chartName, array(
            'renderTo' => 'wgtotalvisitors',
            'title' => $label,
            'plotOptionsShowInLegend' => true,
            'creditsEnabled' => false,
            'plotBackgroundColor' => null,
            'plotBorderWidth' => null,
            'plotShadow' => true,
        ));

        $line = $this->Highcharts->addChartSeries();
        $line->addName('Visitantes')
                ->addData($data);

        $chart->addSeries($line);

        $this->set(compact('chartName'));
        $this->set('label', $label);
    }

    /**
     * Ultimos contatos pelo site
     */
    public function admin_wglast_contacts() {
        $this->loadModel('SiteContact');
        $this->SiteContact->recursive = 0;
        $siteContacts = $this->SiteContact->find('all', array(
            'fields' => array(
                'id',
                'name',
                'created',
                'email'
            ),
            'limit' => 10,
            'order' => 'SiteContact.id DESC'
        ));
        $this->set(compact('siteContacts'));
    }

    /**
     * Ultimos posts do site
     */
    public function admin_wglast_posts() {
        $this->loadModel('Node');
        $this->Node->recursive = 0;
        $nodes = $this->Node->find('all', array(
            'limit' => 10,
            'order' => 'Node.created DESC'
        ));
        $this->set(compact('nodes'));
    }

}
