<?php

namespace App\Imports;

use App\Models\ObjectModel;
use App\Models\CarReport;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Carbon\Carbon;

class MultipleFileImport implements ToCollection, WithChunkReading
{
    /**
    * @param Collection $collection
    */
    private $object_name = "";
    private $object_id = null;
    public function collection(Collection $rows)
    {

        $this->object_name = "";
        foreach ($rows as $row) 
        {

            $in_row = $row->toArray();
            
            // dd($in_row[0]);
            
            if($in_row[0] == 'Travel sheet (day/night)' ) {
                // dd($in_row);
                continue;
            }

            if($in_row[0] == '' && $in_row[1] == '' && $in_row[2] == '' && $in_row[3] == ''){
                continue;
            }

            if($in_row[0] == 'Object:'){
                $this->object_name = $in_row[1];

                if(ObjectModel::where('name', $this->object_name)->exists()){
                    $object_details = ObjectModel::where('name', $this->object_name)->first();

                    $this->object_id = $object_details->id;
                }
                else{
                    $object_details = ObjectModel::create([
                        'name' => $in_row[1],
                    ]);

                    $this->object_id = $object_details->id;
                }

                continue;
            }

            if($in_row[0] == 'Period:'){
                continue;
            }
            // Time A

            if($in_row[0] == 'Time A' && $in_row[1] == 'Position A' && $in_row[2] == 'Time B' && $in_row[3] == 'Position B' && $in_row[4] == 'Duration')
            {
                // dd($in_row);
                continue;
            }

            // dd($in_row);
            $formatted_start_time = Carbon::createFromFormat('Y-m-d H:i:s',$row[0])->format('H:i:s');
            $formatted_end_time = Carbon::createFromFormat('Y-m-d H:i:s',$row[2])->format('H:i:s');
            // dd($row);

            $car_report = CarReport::create([
                'object_id' => $this->object_id,
                'start_time' => $row[0],
                'start_position' => $row[1],
                'end_time' => $row[2],
                'end_position' => $row[3],
                'duration' => date( "H:i:s",strtotime($formatted_end_time) - strtotime($formatted_start_time)),
                'length' => preg_replace('/[^\d.]/','',$row[5]),
                'fuel_consumption' => preg_replace('/[^\d.]/','',$row[6]),
                'avg_fuel_consumption' => preg_replace('/[^\d.]/','',$row[7]),
                'fuel_cost' => preg_replace('/[^\d.]/','',$row[8]),
                'driver' => $row[9],
                'trailer' => $row[10],
            ]);

            
        }
    }
    
    // public function headingRow(): int
    // {
    //     return 6;
    // }
    public function chunkSize(): int
    {
        return 1000;
    }
}
