<?php

namespace App\Export;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Input;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class PropertyCompetitorExport implements FromArray, WithHeadings, ShouldAutoSize, WithEvents
{
    public function __construct($dataTableData = "")
    {
        $this->dataTableData = $dataTableData;
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $cellRange = 'A1:Q1'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setBold(true);
                //$event->sheet->setAutoFilter($cellRange);
                $event->sheet->setTitle('Property Competitor Export');
            },
        ];
    }

    public function headings(): array
    {

        $header = [
            'Property'
        ];

        foreach ($this->dataTableData['date'] as $date) {
            $header[] = $date;
        }

        $header[] = 'Currency';

        return $header;
    }

    public function array(): array
    {

        $dataTableData = [];

        foreach ($this->dataTableData['value'] as $propertyKey => $propertyValue) {

            $dataTableData[$propertyKey] = [
                'name' => $this->dataTableData['property'][$propertyKey]
            ];

            foreach ($propertyValue as $dateKey => $value) {
                $dataTableData[$propertyKey][$dateKey] = $value['amount'];
            }

            $dataTableData[$propertyKey]['currency'] = $value['currency'];

        }


        return [$dataTableData];
    }

}
