<?php

namespace App\Http\Controllers;

use Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Temperature extends Controller
{
    public function csvtemp(Request $request){
            $headers = [
                    'Cache-Control'       => 'must-revalidate, post-check=0, pre-check=0'
                ,   'Content-type'        => 'text/csv'
                ,   'Content-Disposition' => 'attachment; filename=temp.csv'
                ,   'Expires'             => '0'
                ,   'Pragma'              => 'public'
            ];
    
            $sd = $request->input("start");
            $ed = $request->input("end");
            $u = $request->input("user");
            $c = $request->input("computer");

            $wh = [];

            if ($u != '') $wh[] = ['user', '=', $u];
            if ($sd != '') $wh[] = ['date', '>=', $sd];
            if ($sd != '') $wh[] = ['date', '<=', $ed];
            $queries = DB::table('temp')->where($wh)->get();

            $list = $queries->toArray();
            
            $callback = function() use ($list) 
            {
                $FH = fopen('php://output', 'w');
                fputcsv($FH, ['Date','User','Computer','Recv','Send']);
                $first = true;
                $recv = 0;
                $send = 0;

                foreach ($list as $r) { 
                    
                    $in = $r->recv - $recv;
                    $out = $r->send - $send;
                
                    if ($in > 1000000000 || $in < 0 || 
                        $out > 1000000000 || $out < 0 || 
                        $first == true) {
                          $in = $out = 0;
                    }
                    // date - r->date > 300  -> date + 300 = 0
                    if (!$first) {
                        $d1 = new \DateTime($r->date);
                        $d2 = new \DateTime($date);

                        $diff = $d1->getTimestamp() - $d2->getTimestamp();

                        $in = $in / $diff;
                        $out = $out / $diff;

                        if ($diff > 500) {
                            $in = $out = 0;
                            $ts = $d2->getTimestamp() + 300;
                            $ndate = new \DateTime();
                            $ndate->setTimestamp($ts);
                            $ndate = $ndate->format('Y-m-d H:i:s');
                            fputcsv($FH, [$ndate,$r->user,$r->computer,$in,$out]);
                            
                        }
                    }

                    fputcsv($FH, [$r->date,$r->user,$r->computer,$in,$out]);

                    $recv = $r->recv;
                    $send = $r->send;
                    $date = $r->date;
                    $first = false;
                }
                fclose($FH);
            };
            
            
            return Response::stream($callback, 200, $headers);
        
    }

    public function search(Request $request){
        
        $sd = $request->input("start_date");
        $ed = $request->input("end_date");
        $u = $request->input("user");
        $c = $request->input("computer");

        return view('graph', ['start' => $sd, 'end' => $ed, 'user'=> $u]);
    }


}
