<?php

namespace App\Exports\V3\AccidentReceipt;

use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Concerns\FromView;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ProvinceReport implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
{
    public function __construct(private array $data){}

    public function view(): View
    {
        $inputData = $this->data;
        $reportData =$inputData['report'];

        $province = array_search(-1, array_column($reportData, 'province_id'));

        $activities[] = [
            'name' => 'کل استان',
            'tedade' => $reportData['data'][$province]->tedade,
            'police_rah' => $reportData['data'][$province]->police_rah,
            'gozaresh_gasht' => $reportData['data'][$province]->gozaresh_gasht,
            'kol_sabt_shode' => $reportData['data'][$province]->kol_sabt_shode,
            'vasel_shode' => $reportData['data'][$province]->vasel_shode ?? 0,
            'vasel_shode_bimeh' => $reportData['data'][$province]->vasel_shode_bimeh ?? 0,
            'vasel_shode_daghi' => $reportData['data'][$province]->vasel_shode_daghi ?? 0,
            'vasel_shode_final' => $reportData['data'][$province]->vasel_shode_final ?? 0,
        ];

        foreach ($inputData['cities'] as $city) {

            $existingCity = array_search($city->id, array_column($reportData['data'], 'city_id'));

            if ($existingCity) {
                $activities[] = [
                    'name' => $city->name_fa,
                    'tedade' => $reportData['data'][$existingCity]->tedade,
                    'police_rah' => $reportData['data'][$existingCity]->police_rah,
                    'gozaresh_gasht' => $reportData['data'][$existingCity]->gozaresh_gasht,
                    'kol_sabt_shode' => $reportData['data'][$existingCity]->kol_sabt_shode,
                    'vasel_shode' => $reportData['data'][$existingCity]->vasel_shode ?? 0,
                    'vasel_shode_bimeh' => $reportData['data'][$existingCity]->vasel_shode_bimeh ?? 0,
                    'vasel_shode_daghi' => $reportData['data'][$existingCity]->vasel_shode_daghi ?? 0,
                    'vasel_shode_final' => $reportData['data'][$existingCity]->vasel_shode_final ?? 0,
                ];
            } else {
                $activities[] = [
                    'name' => $city->name_fa,
                    'tedade' => "",
                    'police_rah' => "",
                    'gozaresh_gasht' => "",
                    'kol_sabt_shode' => "",
                    'vasel_shode' => "",
                    'vasel_shode_bimeh' => "",
                    'vasel_shode_daghi' => "",
                    'vasel_shode_final' => "",
                ];
            }
        }

        return view('v3.Reports.AccidentReceipt.ProvinceReport', [
            'data' => $activities,
            'fromDate' => $reportData['fromDate'],
            'toDate' => $reportData['toDate'],
        ]);
    }

    /**
     * @return array
     */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function (AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
            },
        ];
    }

    public function drawings()
    {
        $drawing = new Drawing();
        $drawing->setName('Logo');
        $drawing->setDescription('This is my logo');
        $drawing->setPath(public_path('/dist/logo.png'));
        $drawing->setWidth(50);
        $drawing->setHeight(50);
        $drawing->setOffsetX(5);
        $drawing->setOffsetY(5);
        $drawing->setCoordinates('A1');

        $drawing2 = new Drawing();
        $drawing2->setName('Logo');
        $drawing2->setDescription('This is my logo');
        $drawing2->setPath(public_path('/dist/141icon.png'));
        $drawing2->setWidth(50);
        $drawing2->setHeight(50);
        $drawing2->setOffsetX(5);
        $drawing2->setOffsetY(5);
        $drawing2->setCoordinates('B1');
        return [$drawing, $drawing2];
    }

    public function styles(Worksheet $sheet)
    {
        return [
            // Style the first row as bold text.
            'A:BA' =>    [
                'font' => [
                    'name' => 'B Nazanin'
                ]
            ],
        ];
    }
}
