<?php

namespace App\Exports\Tollhouse;

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;


        $provinces = \App\Models\Province::
        when($province, function($query, $province) {
            return $query->where('id', $province);  
        })
        ->get();
        $array = [];
        foreach ($provinces as $item) {
            $array[$item->id] = [
                'province_fa' => $item->name_fa,
                'EhdasCount' => 0,
                'AnbarShenCount' => 0,
                'NosaziCount' => 0,
                'MashinCount' => 0,
                'DivarkeshiCount' => 0,
                'countAll' => 0,
                'EhdasAvg' => 0,
                'AnbarShenAvg' => 0,
                'NosaziAvg' => 0,
                'MashinAvg' => 0,
                'DivarkeshiAvg' => 0,
                'SayerCount' => 0,
                'SayerAvg' => 0
            ];
        }


        $query = "SELECT
                COUNT(CASE WHEN operation_type_id1 = 501 OR 
                        operation_type_id2 = 501
                        OR 
                        operation_type_id3 = 501
                        OR 
                        operation_type_id4 = 501
                        THEN 1 END) AS EhdasCount,
                AVG(CASE WHEN operation_type_id1 = 501 THEN operation_type_progress1
                    WHEN operation_type_id2 = 501 THEN operation_type_progress2
                    WHEN operation_type_id3 = 501 THEN operation_type_progress3
                    WHEN operation_type_id4 = 501 THEN operation_type_progress4
                END) AS EhdasAvg,
                COUNT(CASE WHEN operation_type_id1 = 502 OR 
                        operation_type_id2 = 502
                        OR 
                        operation_type_id3 = 502
                        OR 
                        operation_type_id4 = 502
                        THEN 1 END) AS AnbarShenCount,
                AVG(CASE WHEN operation_type_id1 = 502 THEN operation_type_progress1
                    WHEN operation_type_id2 = 502 THEN operation_type_progress2
                    WHEN operation_type_id3 = 502 THEN operation_type_progress3
                    WHEN operation_type_id4 = 502 THEN operation_type_progress4
                END) AS AnbarShenAvg,
                COUNT(CASE WHEN operation_type_id1 = 503 OR 
                        operation_type_id2 = 503
                        OR 
                        operation_type_id3 = 503
                        OR 
                        operation_type_id4 = 503
                        THEN 1 END) AS NosaziCount,
                AVG(CASE WHEN operation_type_id1 = 503 THEN operation_type_progress1
                    WHEN operation_type_id2 = 503 THEN operation_type_progress2
                    WHEN operation_type_id3 = 503 THEN operation_type_progress3
                    WHEN operation_type_id4 = 503 THEN operation_type_progress4
                END) AS NosaziAvg,
                COUNT(CASE WHEN operation_type_id1 = 504 OR 
                        operation_type_id2 = 504
                        OR 
                        operation_type_id3 = 504
                        OR 
                        operation_type_id4 = 504
                        THEN 1 END) AS MashinCount,
                AVG(CASE WHEN operation_type_id1 = 504 THEN operation_type_progress1
                    WHEN operation_type_id2 = 504 THEN operation_type_progress2
                    WHEN operation_type_id3 = 504 THEN operation_type_progress3
                    WHEN operation_type_id4 = 504 THEN operation_type_progress4
                END) AS MashinAvg,
                COUNT(CASE WHEN operation_type_id1 = 505 OR 
                        operation_type_id2 = 505
                        OR 
                        operation_type_id3 = 505
                        OR 
                        operation_type_id4 = 505
                        THEN 1 END) AS DivarkeshiCount,
                AVG(CASE WHEN operation_type_id1 = 505 THEN operation_type_progress1
                    WHEN operation_type_id2 = 505 THEN operation_type_progress2
                    WHEN operation_type_id3 = 505 THEN operation_type_progress3
                    WHEN operation_type_id4 = 505 THEN operation_type_progress4
                END) AS DivarkeshiAvg,
                COUNT(CASE WHEN operation_type_id1 = 506 OR 
                    operation_type_id2 = 506
                    OR 
                    operation_type_id3 = 506
                    OR 
                    operation_type_id4 = 506
                    THEN 1 END) AS SayerCount,
                AVG(CASE WHEN operation_type_id1 = 506 THEN operation_type_progress1
                    WHEN operation_type_id2 = 506 THEN operation_type_progress2
                    WHEN operation_type_id3 = 506 THEN operation_type_progress3
                    WHEN operation_type_id4 = 506 THEN operation_type_progress4
                END) AS SayerAvg,
                COUNT(*) AS countAll,province_fa, province_id FROM contract_subitems
                WHERE project_type_id = 5
                ";
                
                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);

        $sum = [
            'EhdasCount' => 0,
            'AnbarShenCount' => 0,
            'NosaziCount' => 0,
            'MashinCount' => 0,
            'SayerCount' => 0,
            'DivarkeshiCount' => 0,
            'countAll' => 0,
            'EhdasAvg' => 0,
            'AnbarShenAvg' => 0,
            'NosaziAvg' => 0,
            'MashinAvg' => 0,
            'SayerAvg' => 0,
            'DivarkeshiAvg' => 0,
        ];

        foreach ($data as $value) {
            $array[$value->province_id] = [
                'province_fa' => $value->province_fa,
                'EhdasCount' => $value->EhdasCount,
                'AnbarShenCount' => $value->AnbarShenCount,
                'NosaziCount' => $value->NosaziCount,
                'MashinCount' => $value->MashinCount,
                'SayerCount' => $value->SayerCount,
                'DivarkeshiCount' => $value->DivarkeshiCount,
                'countAll' => $value->countAll,
                'EhdasAvg' => $value->EhdasAvg,
                'AnbarShenAvg' => $value->AnbarShenAvg,
                'NosaziAvg' => $value->NosaziAvg,
                'MashinAvg' => $value->MashinAvg,
                'SayerAvg' => $value->SayerAvg,
                'DivarkeshiAvg' => $value->DivarkeshiAvg,
            ];

            $sum['EhdasCount'] += $value->EhdasCount;
            $sum['AnbarShenCount'] += $value->AnbarShenCount;
            $sum['NosaziCount'] += $value->NosaziCount;
            $sum['MashinCount'] += $value->MashinCount;
            $sum['SayerCount'] += $value->SayerCount;
            $sum['DivarkeshiCount'] += $value->DivarkeshiCount;
            $sum['countAll'] += $value->countAll;
            $sum['EhdasAvg'] += $value->EhdasAvg;
            $sum['AnbarShenAvg'] += $value->AnbarShenAvg;
            $sum['NosaziAvg'] += $value->NosaziAvg;
            $sum['MashinAvg'] += $value->MashinAvg;
            $sum['SayerAvg'] += $value->SayerAvg;
            $sum['DivarkeshiAvg'] += $value->DivarkeshiAvg;
        }

        return view('excel.Tollhouse.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(5);
        $drawing2->setOffsetY(5);
        $drawing2->setCoordinates('B1');
        return [$drawing, $drawing2];
    }

    public function styles(Worksheet $sheet)
    {

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

    }
}