<?php

namespace App\Exports\Technical;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use App\Models\ContractSubItems;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithEvents;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class CompareProgramAndFunction implements FromView, ShouldAutoSize, WithEvents, WithDrawings, WithStyles
{
    public function __construct($fromDate = null, $toDate = null, $province = null, $last_status_id = null)
    {
        $this->fromDate = $fromDate ?? null;
        $this->toDate = $toDate ?? null;
        $this->province = $province ?? null;
        $this->last_status_id = $last_status_id ?? null;
    }

    public function view(): View
    {
        $fromDate = $this->fromDate;
        $toDate = $this->toDate;
        $province = $this->province;
        $last_status_id = $this->last_status_id;

        $query = "SELECT
                province_id,
                COUNT(CASE WHEN operation_type_id1 = '401' OR 
                        operation_type_id2 = '401'
                        OR 
                        operation_type_id3 = '401'
                        OR 
                        operation_type_id4 = '401'
                        THEN 1 END) AS TarizCount,
                AVG(CASE WHEN operation_type_id1 = '401' THEN operation_type_progress1
                    WHEN operation_type_id2 = '401' THEN operation_type_progress2
                    WHEN operation_type_id3 = '401' THEN operation_type_progress3
                    WHEN operation_type_id4 = '401' THEN operation_type_progress4
                END) AS TarizAvg,
                COUNT(CASE WHEN operation_type_id1 = '402' OR 
                        operation_type_id2 = '402'
                        OR 
                        operation_type_id3 = '402'
                        OR 
                        operation_type_id4 = '402'
                        THEN 1 END) AS TamirPolCount,
                AVG(CASE WHEN operation_type_id1 = '402' THEN operation_type_progress1
                        WHEN operation_type_id2 = '402' THEN operation_type_progress2
                        WHEN operation_type_id3 = '402' THEN operation_type_progress3
                        WHEN operation_type_id4 = '402' THEN operation_type_progress4
                END) AS TamirPolAvg,
                COUNT(CASE WHEN operation_type_id1 = '403' OR 
                        operation_type_id2 = '403'
                        OR 
                        operation_type_id3 = '403'
                        OR 
                        operation_type_id4 = '403'
                        THEN 1 END) AS ToonelCount,
                AVG(CASE WHEN operation_type_id1 = '403' THEN operation_type_progress1
                    WHEN operation_type_id2 = '403' THEN operation_type_progress2
                    WHEN operation_type_id3 = '403' THEN operation_type_progress3
                    WHEN operation_type_id4 = '403' THEN operation_type_progress4
                END) AS ToonelAvg,
                COUNT(CASE WHEN operation_type_id1 = '404' OR 
                        operation_type_id2 = '404'
                        OR 
                        operation_type_id3 = '404'
                        OR 
                        operation_type_id4 = '404'
                        THEN 1 END) AS DivarCount,
                AVG(CASE WHEN operation_type_id1 = '404' THEN operation_type_progress1
                    WHEN operation_type_id2 = '404' THEN operation_type_progress2
                    WHEN operation_type_id3 = '404' THEN operation_type_progress3
                    WHEN operation_type_id4 = '404' THEN operation_type_progress4
                END) AS DivarAvg,
                COUNT(CASE WHEN operation_type_id1 = '405' OR 
                        operation_type_id2 = '405'
                        OR 
                        operation_type_id3 = '405'
                        OR 
                        operation_type_id4 = '405'
                        THEN 1 END) AS NewPolCount,
                AVG(CASE WHEN operation_type_id1 = '405' THEN operation_type_progress1
                    WHEN operation_type_id2 = '405' THEN operation_type_progress2
                    WHEN operation_type_id3 = '405' THEN operation_type_progress3
                    WHEN operation_type_id4 = '405' THEN operation_type_progress4
                END) AS NewPolAvg,
                COUNT(CASE WHEN operation_type_id1 = '406' OR 
                        operation_type_id2 = '406'
                        OR 
                        operation_type_id3 = '406'
                        OR 
                        operation_type_id4 = '406'
                        THEN 1 END) AS KenargozarCount,
                AVG(CASE WHEN operation_type_id1 = '406' THEN operation_type_progress1
                    WHEN operation_type_id2 = '406' THEN operation_type_progress2
                    WHEN operation_type_id3 = '406' THEN operation_type_progress3
                    WHEN operation_type_id4 = '406' THEN operation_type_progress4
                END) AS KenargozarAvg,
                COUNT(CASE WHEN operation_type_id1 = '407' OR 
                        operation_type_id2 = '407'
                        OR 
                        operation_type_id3 = '407'
                        OR 
                        operation_type_id4 = '407'
                        THEN 1 END) AS SayerCount,
                AVG(CASE WHEN operation_type_id1 = '407' THEN operation_type_progress1
                    WHEN operation_type_id2 = '407' THEN operation_type_progress2
                    WHEN operation_type_id3 = '407' THEN operation_type_progress3
                    WHEN operation_type_id4 = '407' THEN operation_type_progress4
                END) AS SayerAvg,
                COUNT(*) AS countAll,province_fa FROM contract_subitems
                WHERE project_type_id = '4'
                ";

                if ($this->fromDate && $this->toDate) {
                    $query .= " AND contract_date_from_parent_contract BETWEEN '{$fromDate} 00:00:00' and '{$toDate} 23:59:59'\n";
                }

                if ($this->province) {
                    $query .= " AND province_id = {$province}\n";
                }

                if ($this->last_status_id) {
                    $query .= " AND last_status_id = {$last_status_id}\n";
                }

                $query .= " GROUP BY province_id
                ORDER BY province_id ASC;";
        
        $data = DB::select($query);

        $provinces = \App\Models\Province::select('id', 'name_fa')
                                        ->when($province, function($query, $province) {
                                            return $query->where('id', $province);  
                                        })
                                        ->get();
                                        
        foreach ($provinces as $key => $value) {
            $array[$value->id] = [ 
                'province_id' => $value->id,
                'province_fa' => $value->name_fa,
                'TarizCount' => '-',
                'TarizAvg' => '-',
                'TamirPolCount' => '-',
                'TamirPolAvg' => '-',
                'ToonelCount' => '-',
                'ToonelAvg' => '-',
                'DivarCount' => '-',
                'DivarAvg' => '-',
                'NewPolCount' => '-',
                'NewPolAvg' => '-',
                'KenargozarCount' => '-',
                'KenargozarAvg' => '-',
                'SayerCount' => '-',
                'SayerAvg' => '-',
                'countAll' => '-',
            ];
        }
            
        $sum = 
        [
            'TarizCount' => 0,
            'TarizAvg' => 0,
            'TamirPolCount' => 0,
            'TamirPolAvg' => 0,
            'ToonelCount' => 0,
            'ToonelAvg' => 0,
            'DivarCount' => 0,
            'DivarAvg' => 0,
            'NewPolCount' => 0,
            'NewPolAvg' => 0,
            'KenargozarCount' => 0,
            'KenargozarAvg' => 0,
            'SayerCount' => 0,
            'SayerAvg' => 0,
            'countAll' => 0,
        ];
        foreach ($data as $key => $value) {
            $array[$value->province_id] = [
                'province_id' => $value->province_id,
                'province_fa' => $value->province_fa,
                'TarizCount' => $value->TarizCount,
                'TarizAvg' => $value->TarizAvg,
                'TamirPolCount' => $value->TamirPolCount,
                'TamirPolAvg' => $value->TamirPolAvg,
                'ToonelCount' => $value->ToonelCount,
                'ToonelAvg' => $value->ToonelAvg,
                'DivarCount' => $value->DivarCount,
                'DivarAvg' => $value->DivarAvg,
                'NewPolCount' => $value->NewPolCount,
                'NewPolAvg' => $value->NewPolAvg,
                'KenargozarCount' => $value->KenargozarCount,
                'KenargozarAvg' => $value->KenargozarAvg,
                'SayerCount' => $value->SayerCount,
                'SayerAvg' => $value->SayerAvg,
                'countAll' => $value->countAll,
            ];
            $sum['TarizCount'] += $value->TarizCount;
            $sum['TarizAvg'] += $value->TarizAvg;
            $sum['TamirPolCount'] += $value->TamirPolCount;
            $sum['TamirPolAvg'] += $value->TamirPolAvg;
            $sum['ToonelCount'] += $value->ToonelCount;
            $sum['ToonelAvg'] += $value->ToonelAvg;
            $sum['DivarCount'] += $value->DivarCount;
            $sum['DivarAvg'] += $value->DivarAvg;
            $sum['NewPolCount'] += $value->NewPolCount;
            $sum['NewPolAvg'] += $value->NewPolAvg;
            $sum['KenargozarCount'] += $value->KenargozarCount;
            $sum['KenargozarAvg'] += $value->KenargozarAvg;
            $sum['SayerCount'] += $value->SayerCount;
            $sum['SayerAvg'] += $value->SayerAvg;
            $sum['countAll'] += $value->countAll;

        }

        return view('excel.Technical.CompareProgramAndFunction', [
            'roads' => $array,
            'fromFa' => $fromDate ?? null,
            'sum' => $sum,
            'province_count' => $provinces->count(),
            'toFa' => $toDate ?? null
        ]);
    }

    /**
     * @return array
    */
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true);
            },
        ];
    }

    public function drawings()
    {
        $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(55);
        $drawing2->setOffsetY(5);
        $drawing2->setCoordinates('A1');
        return [$drawing, $drawing2];
    }

    public function styles(Worksheet $sheet)
    {

        return [
            // Style the first row as bold text.
            'A:B' =>    [
                'font' => [
                    'name' => 'B Nazanin'
                ]
            ],
        ];

    }
}