<?php

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromView;
use Carbon\Carbon;
class CarReportExport implements FromView
{
     /**
    * @return \Illuminate\Support\Collection
    */
    protected $data;
    protected $period;

    public function __construct(array $data, $period)
    {
        $this->data = $data;
        $this->period = $period;
    }

    public function view(): View
    {
        $data = $this->fakeData();
        // dd($data);
        return view('report.export', compact('data'));
    }

    public function fakeData(){

        $carREportdata = [];
        
        foreach($this->data as $value){

            $total_duration = '00:00:00';
            $total_length = 0;
            $total_fuel_consumption = 0;
            $total_avg_fuel_consumption = 0;
            $total_fuel_cost = 0;
            
            $trip_duration_during_work = '00:00:00';
            $length_during_work = 0;
            $fuel_cons_during_work = 0;
            $avg_fuel_cons_during_work = 0;
            $fuel_cost_during_work = 0;
            $start = '07:31:00';
            $end   = '17:29:00';


            $after_work_start_time = '17:30:00';
            $after_work_end_time = '07:30:00';
            $trip_duration_after_work = '00:00:00';
            $length_after_work = 0;
            $fuel_cons_after_work = 0;
            $avg_fuel_cons_after_work = 0;
            $fuel_cost_after_work = 0;

           
            $trip_duration_on_weekends = '00:00:00';
            $length_on_weekends = 0;
            $fuel_cons_on_weekends = 0;
            $avg_fuel_cons_on_weekends = 0;
            $fuel_cost_on_weekends = 0;

            foreach($value['car_r_eports'] as $report_value){

                // dd($value);

                $cur_start_time  = Carbon::createFromFormat('Y-m-d H:i:s',$report_value["start_time"])->format('H:i:s');
                $cur_end_time  = Carbon::createFromFormat('Y-m-d H:i:s',$report_value["end_time"])->format('H:i:s');

                $total_duration = date( "H:i:s",strtotime($total_duration) + strtotime($report_value['duration']));
                $total_length = $total_length + $report_value['length'];
                $total_fuel_consumption = $total_fuel_consumption + $report_value['fuel_consumption'];
                $total_avg_fuel_consumption = $total_avg_fuel_consumption + $report_value['avg_fuel_consumption'];
                $total_fuel_cost = $total_fuel_cost + $report_value['fuel_cost'];

                if(!(Carbon::parse($report_value["start_time"])->isWeekend())){

                   $parse_curstart_time = Carbon::parse($cur_start_time);
                    if($parse_curstart_time->isBetween($start, $end)) {
                        dd($cur_start_time);
                        // between 8:00 AM and 8:00 PM
                    } else{
                        dd($cur_start_time);

                    }

                    if($cur_start_time >= $start && $cur_end_time <= $end){
                        $trip_duration_during_work = date( "H:i:s",strtotime($trip_duration_during_work) + strtotime($report_value['duration']));
                        $length_during_work = $length_during_work + $report_value['length'];
                        $fuel_cons_during_work = $fuel_cons_during_work + $report_value['fuel_consumption'];
                        $avg_fuel_cons_during_work = $avg_fuel_cons_during_work + $report_value['avg_fuel_consumption'];
                        $fuel_cost_during_work = $fuel_cost_during_work + $report_value['fuel_cost'];
                    }
    
                    if($cur_start_time >= $after_work_start_time){
                        // dd($cur_start_time, $cur_end_time);

                        $trip_duration_after_work = date( "H:i:s",strtotime($trip_duration_after_work) + strtotime($report_value['duration']));
                        $length_after_work = $length_after_work + $report_value['length'];
                        $fuel_cons_after_work = $fuel_cons_after_work + $report_value['fuel_consumption'];
                        $avg_fuel_cons_after_work = $avg_fuel_cons_after_work + $report_value['avg_fuel_consumption'];
                        $fuel_cost_after_work = $fuel_cost_after_work + $report_value['fuel_cost'];
                    }
                    
                }
                else
                {
                    $trip_duration_on_weekends = date( "H:i:s",strtotime($trip_duration_on_weekends) + strtotime($report_value['duration']));
                    $length_on_weekends = $length_on_weekends + $report_value['length'];
                    $fuel_cons_on_weekends = $fuel_cons_on_weekends + $report_value['fuel_consumption'];
                    $avg_fuel_cons_on_weekends = $avg_fuel_cons_on_weekends + $report_value['avg_fuel_consumption'];
                    $fuel_cost_on_weekends = $fuel_cost_on_weekends + $report_value['fuel_cost'];
                    // dd(Carbon::parse($report_value["start_time"])->format('l'), $report_value["start_time"]);
                    
                }

                

            }
            $value['period'] = $this->period;
            $value['usage_during_work_hours'] = [
                'trip_duration_during_work' => $trip_duration_during_work,
                'length_during_work' => $length_during_work,
                'fuel_cons_during_work' => $fuel_cons_during_work,
                'avg_fuel_cons_during_work' => $avg_fuel_cons_during_work,
                'fuel_cost_during_work' => $fuel_cost_during_work,
            ];

            $value['usage_after_work_hours'] = [
                'trip_duration_after_work' => $trip_duration_after_work,
                'length_after_work' => $length_after_work,
                'fuel_cons_after_work' => $fuel_cons_after_work,
                'avg_fuel_cons_after_work' => $avg_fuel_cons_after_work,
                'fuel_cost_after_work' => $fuel_cost_after_work,
            ];

            $value['usage_in_weekends'] = [
                'trip_duration_on_weekends' => $trip_duration_on_weekends,
                'length_on_weekends' => $length_on_weekends,
                'fuel_cons_on_weekends' => $fuel_cons_on_weekends,
                'avg_fuel_cons_on_weekends' => $avg_fuel_cons_on_weekends,
                'fuel_cost_on_weekends' => $fuel_cost_on_weekends,
            ];

            $value['total_usage'] = [
                'total_duration' => $total_duration,
                'total_length' => $total_length,
                'total_fuel_consumption' => $total_fuel_consumption,
                'total_avg_fuel_consumption' => $total_avg_fuel_consumption,
                'total_fuel_cost' => $total_fuel_cost,
            ];

            array_push($carREportdata,$value);

        }
        // dd($carREportdata);


        return $carREportdata;

    }

}
