<?php

namespace App\Exports\V3\SafetyAndPrivacy;

use App\Models\EdarateShahri;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ProvinceActivityReport implements FromView, WithEvents, ShouldAutoSize, WithDrawings
{
    public function __construct(private array $data, public Request $request){}

    public function view(): View
    {
        $data = $this->data;
        $existedCities = array_keys($data['activities']);
        $edarat = EdarateShahri::query()->where('province_id', '=', $this->request->province_id)->get(['id', 'name_fa']);

        foreach ($edarat as $value) {
            $edareId = $value->id;

            if (!array_key_exists($edareId, $existedCities)) {
                $data['activities'][] = (object) [
                    'edare_shahri_id' => $edareId,
                    'name_fa' => $value->name_fa,
                    'sum' => 0,
                    '89' => ['1' => 0, '2' => 0, '3' => 0],
                    '90' => ['1' => 0, '2' => 0, '3' => 0],
                    '91' => ['1' => 0, '2' => 0, '3' => 0],
                ];
            }
        }

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

    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('K1');
        return [$drawing, $drawing2];
    }
}