<?php

namespace App\Exports;

use Illuminate\Http\Request;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Sheet;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithTitle;

class BureauExporter implements WithCustomStartCell, WithEvents, WithDrawings, ShouldAutoSize, FromArray, WithTitle
{
    private $request;

    public function __construct(Request $request){
        $this->request = $request;
    }

    public function title(): string
    {
        return $this->request->get('sheet');
    }

    public function array(): array
    {
        return $this->request->get('data');
    }

    public function drawings()
    {
        $drawing = new Drawing();
        $drawing->setName('Logo');
        $drawing->setDescription('Cibil bureau');
        $drawing->setPath(public_path('/assets/front/img/cibil.jpg'));
        $drawing->setHeight(50);
        $drawing->setCoordinates('A1');

        return $drawing;
    }

    public function startCell(): string
    {
        return 'A8';
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                /** @var Sheet $sheet */
                $sheet = $event->sheet;

                $sheet->setCellValue('B2', "DATA SUBMISSION FORM");
                $sheet->setCellValue('B3', "CIBIL INTELLECTUAL PROPERTY");

                $sheet->mergeCells('C2:H2');
                $sheet->setCellValue('C2', "CIBIL COPYRIGHT 2021 CREDIT INFORMATION BUREAU (INDIA) LIMITED. ALL RIGHTS RESERVED.");

                $styleArray1 = [
                    'alignment' => [
                        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
                    ],
                ];

                $cellRange3 = 'C2:H2'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange3)->applyFromArray($styleArray1);

                $sheet->mergeCells('A6:H6');
                $sheet->setCellValue('A6', "Header Segement(TUDF)");

                $sheet->mergeCells('A7:C7');
                $sheet->setCellValue('A7', "Name Segment (PN)");

                $sheet->mergeCells('D7:O7');
                $sheet->setCellValue('D7', "Identification Segment (ID)");

                $sheet->mergeCells('P7:U7');
                $sheet->setCellValue('P7', "Telephone Segment (PT)");

                $sheet->mergeCells('V7:W7');
                $sheet->setCellValue('V7', "Email Contact Segment (EC)");

                $sheet->mergeCells('X7:AG7');
                $sheet->setCellValue('X7', "Address Segment (PA)");

                $sheet->mergeCells('AH7:BR7');
                $sheet->setCellValue('AH7', "Account Segment (TL)");

                $styleArray = [
                    'alignment' => [
                        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
                    ],
                ];

                $cellRange1 = 'A6:H6'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange1)->applyFromArray($styleArray);
                $cellRange2 = 'A7:BR7'; // All headers
                $event->sheet->getDelegate()->getStyle($cellRange2)->applyFromArray($styleArray);

                $event->sheet->getStyle('A6:H6')->applyFromArray([
                    'font' => [
                        'bold' => true
                    ]
                ]);

                $event->sheet->getStyle('A7:BR7')->applyFromArray([
                    'font' => [
                        'bold' => true
                    ]
                ]);

                $col = 'A';
                while(true){
                    $tempCol = $col++;
                    $sheet->getColumnDimension($tempCol)->setAutoSize(true);
                    if($tempCol == $sheet->getHighestDataColumn()){
                        break;
                    }
                }

            },
        ];
    }
}
