<?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\Worksheet\Worksheet;

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

    public function collection(): Collection
    {
        //
    }

    public function view(): View
    {
        return view('modules.order.excelExport')->with($this->data);

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

    private function searchByValue($sheet, $needValue, $partial = true): array
    {
        foreach ($sheet->getRowIterator() as $row) {
            $ret = [
                'col' => null,
                'row' => null,
            ];
            $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();
                        $retArray[] = $ret;
                    }
                } else {
                    if ($cell->getValue() == $needValue) {
                        $ret['col'] = $cell->getColumn();
                        $ret['row'] = $cell->getRow();
                        $retArray[] = $ret;
                    }
                }
            }
        }

        // dd($retArray);
        return $retArray;
    }

    private function formatHead(Worksheet $sheet)
    {
        $lastCell = $sheet->getHighestColumn();
        $sheet->getRowDimension(1)->setRowHeight(170);
        $dataRange = 'A1:'.$lastCell.'1';
        $sheet->getStyle($dataRange)->applyFromArray([
            'borders' => [
                'outline' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                ],
            ],
            'font' => [
                'bold' => true,
            ],
        ]);

    }

    private function formatTitleRow(Worksheet $sheet)
    {
        $headValueInRow = $this->searchByValue($sheet, 'Cikkszám', true)[0]['row'];
        $lastCell = $sheet->getHighestColumn();
        $dataRange = 'A'.$headValueInRow.':'.$lastCell.$headValueInRow;
        $sheet->getStyle($dataRange)->applyFromArray([
            'borders' => [
                'outline' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                ],
                'inside' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],

            ],
            'font' => [
                'bold' => true,
            ],
        ]);
    }

    private function formatDayRows(Worksheet $sheet)
    {
        $pointerText = 'Szállítási dátum:';
        $dateValueInRow = $this->searchByValue($sheet, $pointerText, true);
        $lastCell = $sheet->getHighestColumn();
        // fejlécet skipelni kell
        foreach ($dateValueInRow as $item) {
            $row = $item['row'];
            if ($row > 1) {
                $dataRange = 'A'.($row).':'.$lastCell.($row);
                $sheet->getStyle($dataRange)->applyFromArray([
                    'borders' => [
                        'outline' => [
                            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                        ],
                    ],
                    'font' => [
                        'size' => 14,
                    ],
                ]);
            }

        }
    }

    private function formatSumRows(Worksheet $sheet)
    {
        $pointerText = 'Végösszeg';
        $sumValueInRow = $this->searchByValue($sheet, $pointerText, true);
        $lastCell = $sheet->getHighestColumn();
        // fejlécet skipelni kell
        foreach ($sumValueInRow as $item) {
            $row = $item['row'];
            if ($row > 1) {
                $dataRange = 'A'.$row.':'.$lastCell.$row;
                $sheet->getStyle($dataRange)->applyFromArray([
                    'borders' => [
                        'outline' => [
                            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                        ],
                    ],
                    'font' => [
                        'bold' => true,
                    ],
                ]);
            }

        }

    }

    private function setItemRowFormat(Worksheet $sheet, $dataRange)
    {
        $sheet->getStyle($dataRange)->applyFromArray([
            'borders' => [
                'outline' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_MEDIUM,
                ],
                'inside' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
        ]);

    }

    private function formatItemsRows(Worksheet $sheet)
    {

        $pointerText = 'Végösszeg';
        $sumValueInRow = $this->searchByValue($sheet, $pointerText, true);
        $lastCell = $sheet->getHighestColumn();

        if ($this->data['orderType'] == 'weekly') {
            $pointerText = 'Szállítási dátum:';
            $dateValueInRow = $this->searchByValue($sheet, $pointerText, true);
            for ($i = 0; $i < count($sumValueInRow); $i++) {
                $dataRange = 'A'.($dateValueInRow[$i]['row'] + 1).':'.$lastCell.($sumValueInRow[$i]['row'] - 1);
                $this->setItemRowFormat($sheet, $dataRange);
            }
        } else {
            $headValueInRow = $this->searchByValue($sheet, 'Cikkszám', true)[0]['row'];
            $dataRange = 'A'.($headValueInRow + 1).':'.$lastCell.($sumValueInRow[0]['row'] - 1);
            $this->setItemRowFormat($sheet, $dataRange);
        }

    }

    public function styles(Worksheet $sheet)
    {
        $this->formatHead($sheet);
        $this->formatTitleRow($sheet);
        $this->formatDayRows($sheet);
        $this->formatItemsRows($sheet);
        $this->formatSumRows($sheet);

        return;

        return [

            /*$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();
    }
}
