<?php

namespace App\Http\Controllers;

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


class MeasurementController 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);
        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());

        $q = sprintf("select time,value from bme280_value where host='balkon' and type='%s' and time >= %s and time <= %s",$type,$startdate,$enddate);
        \Debugbar::info($q);
        $result = \InfluxDB::query($q);
        $points = $result->getPoints();
        $a = [];
        foreach ($points as $p) {
            $a[] = [ Carbon::createFromFormat("Y-m-d\TH:i:s.u+",$p["time"],'UTC'), $p["value"]];
        }

        return response()->json($a);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // for t in ('temperature','humidity','pressure')
        $a = [];
        foreach (['temperature','humidity','pressure'] as $type) {
            $q = sprintf("select * from bme280_value where host='balkon' and type='%s' order by time desc limit 1",$type);
            $result = \InfluxDB::query($q);
            $points = $result->getPoints();
            foreach ($points as $p) {
                $a[$type] = $p["value"];
                $a["time"] = Carbon::createFromFormat("Y-m-d\TH:i:s.u+",$p["time"],'UTC')->toATOMString();
            }
        }
        $q = sprintf("select * from esp32_value where host='balkon' and type='voltage' order by time desc limit 1",$type);
        $result = \InfluxDB::query($q);
        $points = $result->getPoints();
        foreach ($points as $p) {
            $a["voltage"] = $p["value"];
        }
        return response()->json($a);
    }

    /**
     * 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)
    {
        //
    }
}
