<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Carbon\Carbon;

class StatisticsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function get(Request $request)
    {
        $types = ['temperature','humidity','pressure'];

        $type = $request->input('type', $types[0]);
        $startdate = $request->input('startdate',NULL);
        $enddate = $request->input('enddate',NULL);
        $range = $request->input('range',NULL);
        if (!in_array($type,$types)) $type = $types[0];
        if ($startdate == NULL || $startdate == '') $startdate = "now()-1d";
        else $startdate = sprintf("'%s'",Carbon::parse($startdate)->toDateTimeString());

        if ($enddate == NULL || $enddate == '') $enddate = "now()";
        else $enddate = sprintf("'%s'",Carbon::parse($enddate)->toDateTimeString());

        if ($range == NULL || $range == '') $range = '356d';
        else {
            if (!preg_match('/^(\d+)[dhm]$/',$range)) {
                $range='356d';
            }
        }

        $q = sprintf("select time,min(value),max(value),mean(value),last(value) from bme280_value where host='balkon' and type='%s' and time >= %s and time <= %s group by time(%s);",$type,$startdate,$enddate,$range);
        \Debugbar::info($q);
        $result = \InfluxDB::query($q);
        $points = $result->getPoints();
        $a = [];
        foreach ($points as $p) {
            $a[] = [ 'time' => $p["time"], 'min'=> $p["min"], 'max' => $p['max'], 'mean' => $p['mean'], 'last' => $p['last']];
        }

        return response()->json($a);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
