<?php

namespace App\Exports\V3\AccidentReceipt;

use App\Models\Province;
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 CountryReport implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
{
    public function __construct(private array $data){}

    public function view(): View
    {
        $data = $this->data;

        $country = array_search(-1, array_column($data['data'], 'province_id'));

        $activities[] = [
            'name' => 'کل کشور',
            'tedade' => $data['data'][$country]->tedade,
            'police_rah' => $data['data'][$country]->police_rah,
            'gozaresh_gasht' => $data['data'][$country]->gozaresh_gasht,
            'kol_sabt_shode' => $data['data'][$country]->kol_sabt_shode,
            'vasel_shode' => $data['data'][$country]->vasel_shode ?? 0,
            'vasel_shode_bimeh' => $data['data'][$country]->vasel_shode_bimeh ?? 0,
            'vasel_shode_daghi' => $data['data'][$country]->vasel_shode_daghi ?? 0,
            'vasel_shode_final' => $data['data'][$country]->vasel_shode_final ?? 0,
        ];

        foreach (Province::all() as $province) {

            $existingProvince = array_search($province->id, array_column($data['data'], 'province_id'));

            if ($existingProvince) {
                $activities[] = [
                    'name' => $province->name_fa,
                    'tedade' => $data['data'][$existingProvince]->tedade,
                    'police_rah' => $data['data'][$existingProvince]->police_rah,
                    'gozaresh_gasht' => $data['data'][$existingProvince]->gozaresh_gasht,
                    'kol_sabt_shode' => $data['data'][$existingProvince]->kol_sabt_shode,
                    'vasel_shode' => $data['data'][$existingProvince]->vasel_shode ?? 0,
                    'vasel_shode_bimeh' => $data['data'][$existingProvince]->vasel_shode_bimeh ?? 0,
                    'vasel_shode_daghi' => $data['data'][$existingProvince]->vasel_shode_daghi ?? 0,
                    'vasel_shode_final' => $data['data'][$existingProvince]->vasel_shode_final ?? 0,
                ];
            } else {
                $activities[] = [
                    'name' => $province->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.CountryReport', [
            'data' => $activities,
            'fromDate' => $data['fromDate'],
            'toDate' => $data['toDate'],
        ]);
    }

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

    public function drawings(): array
    {
        $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): array
    {
        return [
            // Style the first row as bold text.
            'A:BA' =>    [
                'font' => [
                    'name' => 'B Nazanin'
                ]
            ],
        ];
    }
}
