<?php

namespace AppBundle\Utils\Services;

class StatsManager
{

    protected $em;
    protected $history;

    public function __construct($em, $history)
    {
        $this->em = $em;
        $this->history = $history;
    }

    /**
     * @param $user
     * @return array
     */
    public function getTalentStats($user)
    {   
        $records = $this->history->recordShow(null, "r", 'AppBundle\Entity\Company', $user->getCompany()->getId(), 0);

        return array('countViews' => count($records));
    }

    /**
     * @param $idCompany
     * @return array
     */
    public function getTerritoryDashboard($idCompany, $dateArray)
    {
        $arrByAd = array();
        $arrDatasetsAd = array();
        $arrDatasetsCompany = array();
        $arrayRecords = array();
        $arrayRecords['company'] = array();
        $lineDatesLabel = array();
        $nbVisitTalent = 0;
        $nbVisitCompany = 0;
        $nbVisitAnonym = 0;

        $territoryAds = $this->em->getRepository('AppBundle:TerritoryAd')->findBy(array(
            'territory' => $idCompany
        ));
        $nbVisitTalentByAd = 0;
        $nbVisitCompanyByAd  = 0;
        $nbVisitAnonymByAd  = 0;
        $companyAds = array();
        foreach ($territoryAds as $territoryAd) {
            $ad = $territoryAd->getAd();
            if($ad) {
                $arrByAd[$ad->getId()]['count'] = 0;
                $recordAd = $this->history->recordShow(null, "r", 'AppBundle\Entity\Ad', $ad->getId(), 0, $dateArray);
                foreach ($recordAd as $record) {
                    if ($record->getUserId() == 0) {
                        $nbVisitAnonym = $nbVisitAnonym + 1;
                        $nbVisitAnonymByAd = $nbVisitAnonymByAd + 1;
                    } else {
                        $user = $this->em->getRepository('AppBundle:User')->findOneById($record->getUserId());
                        if ($user) { 
                            if ($user->hasRole('ROLE_TALENT')) {
                                $nbVisitTalent = $nbVisitTalent + 1;
                                $nbVisitTalentByAd = $nbVisitTalentByAd + 1;
                            }
                            if ($user->hasRole('ROLE_COMPANY')) {
                                $nbVisitCompany = $nbVisitCompany + 1;
                                $nbVisitCompanyByAd = $nbVisitCompanyByAd + 1;
                            }
                        } else {
                            $nbVisitAnonym = $nbVisitAnonym + 1;
                            $nbVisitAnonymByAd = $nbVisitAnonymByAd + 1;
                        }

                    }
                    $date = $record->getCtime();
                    $stringDate = $date->format('Y-m-d');
                    $arrayRecords['company'][$stringDate][$idCompany] = 0;
                    if (!isset($arrayRecords['ad'][$stringDate][$ad->getId()])) {
                        $arrayRecords['ad'][$stringDate][$ad->getId()] = 1;
                    } else {
                        $arrayRecords['ad'][$stringDate][$ad->getId()] += 1;
                    }
                    if (!in_array($stringDate, $lineDatesLabel)) {
                        if ($stringDate != null) {
                            $lineDatesLabel[] = $stringDate;
                        }
                    }
                    $arrByAd[$ad->getId()]['count'] ++;
                }
                $arrByAd[$ad->getId()]['object'] = $ad;
            }
        }

        $recordCompany = $this->history->recordShow(null, "r", 'AppBundle\Entity\Company', $idCompany, 0, $dateArray);
        $nbVisitTalentByCompany = 0;
        $nbVisitCompanyByCompany  = 0;
        $nbVisitAnonymByCompany  = 0;
        foreach ($recordCompany as $record) {
            if ($record->getUserId() == 0) {
                $nbVisitAnonym = $nbVisitAnonym + 1;
                $nbVisitAnonymByCompany++;
            } else {
                $user = $this->em->getRepository('AppBundle:User')->findOneById($record->getUserId());
                if ($user) { 
                    if ($user->hasRole('ROLE_TALENT')) {
                        $nbVisitTalent = $nbVisitTalent + 1;
                        $nbVisitTalentByCompany++;
                    }
                    if ($user->hasRole('ROLE_COMPANY')) {
                        $nbVisitCompany = $nbVisitCompany + 1;
                        $nbVisitCompanyByCompany++;
                    }
                }
            }

            $date = $record->getCtime();
            $stringDate = $date->format('Y-m-d');
            if(!isset($arrayRecords['ad'][$stringDate][$idCompany])){
                $arrayRecords['ad'][$stringDate][$idCompany] = 0;
            }
            if (!isset($arrayRecords['company'][$stringDate][$idCompany])) {
                $arrayRecords['company'][$stringDate][$idCompany] = 1;
            } else {
                $arrayRecords['company'][$stringDate][$idCompany] += 1;
            }
            if (!in_array($stringDate, $lineDatesLabel)) {
                if ($stringDate != null) {
                    $lineDatesLabel[] = $stringDate;
                }
            }
        }
        if (isset($arrayRecords['ad'])) {
            ksort($arrayRecords['ad']);
            foreach ($arrayRecords['ad'] as $recordAd) {
                $count = 0;
                foreach ($recordAd as $countView) {
                    $count = $count + $countView;
                }
                $arrDatasetsAd[] = $count;
            }
        }

        if (isset($arrayRecords['company'])) {
            ksort($arrayRecords['company']);
            foreach ($arrayRecords['company'] as $recordCompany) {
                $count = 0;
                foreach ($recordCompany as $countView) {
                    $count = $count + $countView;
                }
                $arrDatasetsCompany[] = $count;
            }
        }

        sort($lineDatesLabel, SORT_NATURAL | SORT_FLAG_CASE);
        $lineRecords = array(
            'labels' => $lineDatesLabel,
            'datasetsAd' => $arrDatasetsAd,
            'datasetsCompany' => $arrDatasetsCompany
        );
        
        return array(
            "lineRecords" => $lineRecords,
            "donughtRecords" => array($nbVisitTalent, $nbVisitCompany, $nbVisitAnonym),
            "totalRecords" => $nbVisitTalent + $nbVisitCompany + $nbVisitAnonym,
            "totalRecordsCompany" => $nbVisitTalentByCompany + $nbVisitCompanyByCompany + $nbVisitAnonymByCompany,
            "arrayByAds" => $arrByAd
        );
    }

    /**
     * @param $idCompany
     * @return array
     */
    public function getCompanyDashboard($idCompany, $dateArray)
    {   
        $arrByAd = array();
        $arrDatasetsAd = array();
        $arrDatasetsCompany = array();
        $arrayRecords = array();
        $arrayRecords['ad'] = array();
        $arrayRecords['company'] = array();
        $lineDatesLabel = array();
        $nbVisitTalent = 0;
        $nbVisitCompany = 0;
        $nbVisitAnonym = 0;
        $ads = $this->em->getRepository('AppBundle:Ad')->findBy(
            array(
                'company' => $idCompany
            ));
        if ($ads) {
            foreach ($ads as $ad) {
                $nbVisitTalentByAd = 0;
                $nbVisitCompanyByAd  = 0;
                $nbVisitAnonymByAd  = 0;
                $arrByAd[$ad->getId()]['count'] = 0;
                $recordAd = $this->history->recordShow(null, "r", 'AppBundle\Entity\Ad', $ad->getId(), 0, $dateArray);
                if($recordAd){
                    foreach ($recordAd as $record) {
                        if ($record->getUserId() == 0) {
                            $nbVisitAnonym = $nbVisitAnonym + 1;
                            $nbVisitAnonymByAd = $nbVisitAnonymByAd + 1;
                        } else {
                            $user = $this->em->getRepository('AppBundle:User')->findOneById($record->getUserId());
                            if ($user) { 
                                if ($user->hasRole('ROLE_TALENT')) {
                                    $nbVisitTalent = $nbVisitTalent + 1;
                                    $nbVisitTalentByAd = $nbVisitTalentByAd + 1;
                                }
                                if ($user->hasRole('ROLE_COMPANY')) {
                                    $nbVisitCompany = $nbVisitCompany + 1;
                                    $nbVisitCompanyByAd = $nbVisitCompanyByAd + 1;
                                }
                            } else {
                                $nbVisitAnonym = $nbVisitAnonym + 1;
                                $nbVisitAnonymByAd = $nbVisitAnonymByAd + 1;
                            }

                        }
                        $date = $record->getCtime();
                        $stringDate = $date->format('Y-m-d');
                        $arrayRecords['company'][$stringDate][$idCompany] = 0;
                        if (!isset($arrayRecords['ad'][$stringDate][$ad->getId()])) {
                            $arrayRecords['ad'][$stringDate][$ad->getId()] = 1;
                        } else {
                            $arrayRecords['ad'][$stringDate][$ad->getId()] += 1;
                        }
                        if (!in_array($stringDate, $lineDatesLabel)) {
                            if ($stringDate != null) {
                                $lineDatesLabel[] = $stringDate;
                            }
                        }
                        $arrByAd[$ad->getId()]['count'] ++;
                    }
                }
                $arrByAd[$ad->getId()]['object'] = $ad;
            }
        }

        $recordCompany = $this->history->recordShow(null, "r", 'AppBundle\Entity\Company', $idCompany, 0, $dateArray);
        $nbVisitTalentByCompany = 0;
        $nbVisitCompanyByCompany  = 0;
        $nbVisitAnonymByCompany  = 0;
        if($recordCompany){
            foreach ($recordCompany as $record) {
                if ($record->getUserId() == 0) {
                    $nbVisitAnonym = $nbVisitAnonym + 1;
                    $nbVisitAnonymByCompany++;
                } else {
                    $user = $this->em->getRepository('AppBundle:User')->findOneById($record->getUserId());
                    if ($user) { 
                        if ($user->hasRole('ROLE_TALENT')) {
                            $nbVisitTalent = $nbVisitTalent + 1;
                            $nbVisitTalentByCompany++;
                        }
                        if ($user->hasRole('ROLE_COMPANY')) {
                            $nbVisitCompany = $nbVisitCompany + 1;
                            $nbVisitCompanyByCompany++;
                        }
                    }
                }
                if (!isset($arrayRecords['ad'][$stringDate])) {
                    $arrayRecords['ad'][$stringDate][0] = 0;
                }
                $date = $record->getCtime();
                $stringDate = $date->format('Y-m-d');
                if(!isset($arrayRecords['ad'][$stringDate][$idCompany])){
                    $arrayRecords['ad'][$stringDate][$idCompany] = 0;
                }
                if (!isset($arrayRecords['company'][$stringDate][$idCompany])) {
                    $arrayRecords['company'][$stringDate][$idCompany] = 1;
                } else {
                    $arrayRecords['company'][$stringDate][$idCompany] += 1;
                }
                if (!in_array($stringDate, $lineDatesLabel)) {
                    if ($stringDate != null) {
                        $lineDatesLabel[] = $stringDate;
                    }
                }
            }
        }
        
        ksort($arrayRecords['ad']);
        foreach ($arrayRecords['ad'] as $recordAd) {
            $count = 0;
            foreach ($recordAd as $countView) {
                $count = $count + $countView;
            }
            $arrDatasetsAd[] = $count;
        }

        ksort($arrayRecords['company']);
        foreach ($arrayRecords['company'] as $recordCompany) {
            $count = 0;
            foreach ($recordCompany as $countView) {
                $count = $count + $countView;
            }
            $arrDatasetsCompany[] = $count;
        }

        sort($lineDatesLabel, SORT_NATURAL | SORT_FLAG_CASE);
        $lineRecords = array(
            'labels' => $lineDatesLabel,
            'datasetsAd' => $arrDatasetsAd,
            'datasetsCompany' => $arrDatasetsCompany
        );

        return array(
            "lineRecords" => $lineRecords,
            "donughtRecords" => array($nbVisitTalent, $nbVisitCompany, $nbVisitAnonym),
            "totalRecords" => $nbVisitTalent + $nbVisitCompany + $nbVisitAnonym,
            "totalRecordsCompany" => $nbVisitTalentByCompany + $nbVisitCompanyByCompany + $nbVisitAnonymByCompany,
            "arrayByAds" => $arrByAd
        );
    }
}