<?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;

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

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

    public function fakeData(){
        
        $carREportdata = [
            'filter' => [],
            'values' => [],
            'usage_during_work_hours' => [],
            'usage_after_work_hours' => [],
            'total_usage' => [],
        ];
        $data = [
            'filter' => [],
            'values' => [],
            'usage_during_work_hours' => [],
            'usage_after_work_hours' => [],
            'total_usage' => [],
        ];

        $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;
        
        foreach($this->data as $value){

            dd($value);

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

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

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

            if($cur_start_time >= $after_work_start_time){
                $trip_duration_after_work = date( "H:i:s",strtotime($trip_duration_after_work) + strtotime($value['duration']));
                $length_after_work = $length_after_work + $value['length'];
                $fuel_cons_after_work = $fuel_cons_after_work + $value['fuel_consumption'];
                $avg_fuel_cons_after_work = $avg_fuel_cons_after_work + $value['avg_fuel_consumption'];
                $fuel_cost_after_work = $fuel_cost_after_work + $value['fuel_cost'];
            }

            array_push($data['values'], [
                'start_time' => $value['start_time'],
                'start_position' => $value['start_position'],
                'end_time' => $value['end_time'],
                'end_position' => $value['end_position'],
                'duration' => $value['duration'],
                'length' => $value['length'],
                'fuel_consumption' => $value['fuel_consumption'],
                'avg_fuel_consumption' => $value['avg_fuel_consumption'],
                'fuel_cost' => $value['fuel_cost'],
                'driver' => $value['driver'],
                'trailer' => $value['trailer'],
            ]);
        }

        $data['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,
        ];

        $data['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,
        ];

        $data['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,
        ];

        $data['filter'] = [
            'object' => 'Financa 01-706-PR',
            'period' => '2021-10-04 00:00:00 - 2021-10-10 00:00:00',
        ];

        return $data;

    }

}
