<?php

namespace App\Repositories\Core;

use Carbon\Carbon;

class Stat
{
    public static $is_debug = false;
    public static $force_output = true;

    public static function getNewByPeriod($model, $start, $end)
    {
        return $model->whereBetween('created_at', [$start, $end])->count();
    }

    public static function getTotalAtDate($model, $end)
    {
        return $model->where('created_at', '<', $end)->count();
    }

    public static function renderStatsbyMultiVar($var, $var_option = '')
    {
        self::renderStatsJson(self::getStatsbyMultiVar($var, $var_option));
    }

    public static function renderStatsbyVar($var)
    {
        self::renderStatsJson(self::getStatsbyVar($var));
    }

    public static function renderStatsbyOptions($var, $var_option = '')
    {
        self::renderStatsJson(self::getStatsbyOptions($var, $var_option));
    }

    public static function renderStatsJson($data)
    {
        return json_encode($data, JSON_NUMERIC_CHECK);
    }

    public static function getStatsbyMultiVar($var, $var_option = '')
    {
        if (empty($var_option)) {
            $var_option = $var;
        }
        $options = self::getOption($var_option);
        return self::getStatsbyMultiOptions($var, $options);
    }

    public static function getCountByPeriod($var, $begin, $end)
    {
        $count = self::getModel()
            ->whereBetween($var, $begin, $end)
            ->count();
        return $count;
    }

    public static function getCountbyVar($var)
    {
        $db = self::getInstance()->app->db;
        $data = self::getModel()
            ->select($db::raw("count(id) as y, $var as name"))
            ->groupBy($var)
            ->get();
        // var_Debug::message($data);
        return ($data);
    }


    public static function getStatsbyOptions($var, $var_option = '')
    {
        if (empty($var_option)) {
            $var_option = $var;
        }
        $options = self::getInstance()->controller->getOption($var_option);
        $nb = self::getCountbyOption($var);
        // var_Debug::message($nb);
        foreach ($options as $key => $value) {
            $y = (int) $nb[$key];
            $data[] = ['y' => $y, 'name' => $value];
        }
        // var_Debug::message($data);
        return ($data);
    }

    public static function getCountbyOption($var)
    {
        $db = self::getInstance()->app->db;
        $data = self::getModel()
            ->select($db::raw('count(id) as nb'))
            ->groupBy($var)
            ->get();
        foreach ($data as $key => $value) {
            if (is_array($data[$key])) {
                $data[$key] = (int) $data[$key]['nb'];
            } else {
                $data[$key] = (int) $data[$key]->nb;
            }
        }
        return ($data);
    }

    public static function getStatsbyMultiOptions($var, $options)
    {
        foreach ($options as $key => $value) {
            $nb = self::getCountbyBin($var, $key);
            $data[] = ['y' => $nb, 'name' => $value];
        }
        return ($data);
    }

    public static function getCountbyBin($var, $value)
    {
        $bit = pow(2, $value);
        $count = self::getModel()
            ->where($var, '&', $bit)
            ->count();
        return $count;
    }


    public static function getStatsbyPeriod($begin = '', $end = '', $period = 'days')
    {
        $end = Carbon::now();
        $begin = Carbon::now()->subMonth(1);
        switch ($period) {
        case 'days':
            $periods = DateRange::getPeriodsbyDay($begin, $end);
            break;
        case 'months':
            $periods = DateRange::getPeriodsbyMonth($begin, $end);
            break;
        case 'weeks':
            $periods = DateRange::getPeriodsbyWeek($begin, $end);
            break;
        default:
        }
        return ($periods);
    }

    public static function serializeValues($tab)
    {
        return self::serializeByVar($tab, 'count');
    }

    public static function serializeByVar($tab, $var, $n = 0)
    {
        $collection = collect($tab);

        if ($n) {
            $tab = $collection->pluck($var)->slice(-$n)->toArray();
        } else {
            $tab = $collection->pluck($var)->toArray();
        }

        return implode(",", $tab);
    }

    public static function avgByVar($tab, $var)
    {
        return collect($tab)->pluck($var)->avg();
    }
}
