<?php

namespace App\Exports;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ExportTest implements FromView, ShouldAutoSize, WithColumnFormatting, WithCustomValueBinder, WithMapping, WithStyles
{
    public function __construct($data)
    {
        $this->data = $data;
    }

    public function collection(): Collection
    {
        //
    }

    public function view(): View
    {
        // TODO: Implement view() method.
        return view('test.excelExport')->with($this->data);

        return view('test.invoice')->with($this->data);
    }

    private function searchByValue($sheet, $needValue, $partial = true): array
    {
        $ret = [
            'col' => null,
            'row' => null,
        ];
        foreach ($sheet->getRowIterator() as $row) {
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(true);
            foreach ($cellIterator as $cell) {
                if ($partial) {
                    if (strpos($cell->getValue(), $needValue) !== false) {
                        $ret['col'] = $cell->getColumn();
                        $ret['row'] = $cell->getRow();
                    }
                } else {
                    if ($cell->getValue() == $needValue) {
                        $ret['col'] = $cell->getColumn();
                        $ret['row'] = $cell->getRow();
                    }
                }
            }
        }

        return $ret;
    }

    public function styles(Worksheet $sheet)
    {

        $sumValueInRow = $this->searchByValue($sheet, 'Végösszeg', true)['row'];
        $headValueInRow = $this->searchByValue($sheet, 'Cikkszám', true)['row'];
        $lastCell = $sheet->getHighestColumn();
        $lastRow = $sheet->getHighestRow();

        // orderProfitcenter data style in return

        // rowsHead style in return

        // data range border Style
        $dataRange = 'A'.($headValueInRow + 1).':'.$lastCell.$sumValueInRow;
        $sheet->getStyle($dataRange)->applyFromArray([
            'borders' => [
                'outline' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                ],
                'inside' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
        ]);
        // footer style in return

        return [
            '1' => [
                'borders' => [
                    'outline' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                    ],
                ],
                'font' => [
                    'bold' => true,
                ],
            ],
            $headValueInRow => [
                'borders' => [
                    'outline' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                    ],
                    'inside' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    ],

                ],
                'font' => [
                    'bold' => true,
                ],
            ],
            $sumValueInRow => [
                'borders' => [
                    'outline' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                    ],
                    'inside' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    ],

                ],
                'font' => [
                    'bold' => true,
                ],
            ],

        ];
        /*        return [
                    // Style the first row as bold text.
                    1    => ['font' => ['bold' => true]],

                    // Styling a specific cell by coordinate.
                    'B2' => ['font' => ['italic' => true]],

                    // Styling an entire column.
                    'C'  => ['font' => ['size' => 16]],
                ];*/
    }

    public function map($row): array
    {
        // TODO: Implement map() method.
        dd('asd');
    }

    public function columnFormats(): array
    {
        // TODO: Implement columnFormats() method.
        return [];
    }

    public function bindValue(Cell $cell, $value)
    {
        // TODO: Implement bindValue() method.
        // dd([$cell,$value]);
        // dd($cell->getColumn());
        // dd($cell->getDataType());
        // $cell->setDataType(DataType::TYPE_STRING);
        $noConvertToNumeric = ['A'];
        $cell->setValueExplicit($value, DataType::TYPE_STRING);
        if (is_numeric($value)) {
            if (! in_array($cell->getColumn(), $noConvertToNumeric)) {
                $cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
            }
        }

        return true;

        return $cell->getColumn();
    }
}
