<?php
namespace addons\webman\echart\Driver;

use addons\webman\grid\Filter;
use Carbon\Carbon;
use ExAdmin\ui\component\echart\Echart;
use ExAdmin\ui\component\echart\LineChart;
use ExAdmin\ui\contract\EchartAbstract;
use Illuminate\Database\Eloquent\Builder;

class Eloquent extends EchartAbstract
{
    /**
     * @var Builder
     */
    protected $builder;

    public function initialize(Echart $echart, $repository)
    {
        parent::initialize($echart, $repository); // TODO: Change the autogenerated stub
        $this->builder = $this->repository->newQuery();
    }

    /**
     * 筛选
     * @param array $rule
     * @return mixed
     */
    public function filter(array $rule)
    {
        new Filter($this->builder,$rule);
    }

    public function count($text,\Closure $query = null, $dateField = 'created_at'){
        $this->parseAggregate(__FUNCTION__,$text,'*',$query,$dateField);
    }

    public function max($text, $field, \Closure $query = null, $dateField = 'created_at'){
        $this->parseAggregate(__FUNCTION__,$text,$field,$query,$dateField);
    }

    public function min($text, $field, \Closure $query = null, $dateField = 'created_at'){
        $this->parseAggregate(__FUNCTION__,$text,$field,$query,$dateField);
    }

    public function sum($text, $field, \Closure $query = null, $dateField = 'created_at'){
        $this->parseAggregate(__FUNCTION__,$text,$field,$query,$dateField);
    }

    public function avg($text, $field, \Closure $query = null, $dateField = 'created_at'){
        $this->parseAggregate(__FUNCTION__,$text,$field,$query,$dateField);
    }

    protected function parseAggregate($method,$text, $field, \Closure $query = null, $dateField = 'created_at'){
        $data = [];
        if($this->echart instanceof LineChart){
            foreach (array_column($this->echart->xAxisData,'value') as $item) {
                $data[] = $this->aggregate($method,$field,$query,$dateField,$item);
            }
        }else{
            $data =  $this->aggregate($method,$field,$query,$dateField,$this->echart->getDateFilterValue());
        }

        $this->echart->data($text,$data);
    }

    protected function aggregate($method,$field,\Closure $query = null,$dateField= 'created_at',$dateValue=null){
        $builder = clone $this->builder;
        if($query){
            call_user_func($query,$builder);
        }

        if($dateValue){
            switch ($dateValue) {
                case 'yesterday':
                    $builder->whereBetween($dateField,[Carbon::yesterday()->format("Y-m-d 00:00:00"),Carbon::yesterday()->format("Y-m-d 23:59:59")]);
                    break;
                case 'today':
                    $builder->whereBetween($dateField,[Carbon::today()->format("Y-m-d 00:00:00"),Carbon::today()->format("Y-m-d 23:59:59")]);
                    break;
                case 'week':
                    $start = Carbon::now()->startOfWeek()->toDateString();
                    $end = Carbon::now()->endOfWeek()->toDateString();
                    $builder->whereBetween($dateField,[$start,$end]);
                    break;
                case 'month':
                    $builder->whereBetween($dateField,[Carbon::now()->startOfMonth()->format("Y-m-d 00:00:00"),Carbon::now()->endOfMonth()->format("Y-m-d 23:59:59")]);
                    break;
                case 'year':
                    $builder->whereBetween($dateField,[Carbon::now()->startOfYear()->format("Y-m-d 00:00:00"),Carbon::now()->endOfYear()->format("Y-m-d 23:59:59")]);
                    break;
                default:
                    $builder->whereBetween($dateField,$dateValue);
            }
        }

        return $builder->$method($field);

    }
}
