<?php

namespace App\Http\Controllers;

use App\Http\Requests\Api\PDFRequest;
use App\Http\Resources\ResidentialComplex as ResidentialComplexResource;
use App\Repository\ResidentialComplexRepository;
use App\Services\ResidentialComplexService;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use PDF;
//use Elibyy\TCPDF\Facades\TCPDF;
use App;
use Spipu\Html2Pdf\Html2Pdf;

class PDFController extends Controller
{
    const CACHE_KEY_FILTER_OPTIONS = 'residential_complex_filter_options';
    /** @var ResidentialComplexService */
    public $residentialComplexService;
    /** @var ResidentialComplexRepository */
    public $residentialComplexRepository;

    public function __construct(
        ResidentialComplexRepository $residentialComplexRepository,
        ResidentialComplexService $residentialComplexService
    ) {
        $this->residentialComplexRepository = $residentialComplexRepository;
        $this->residentialComplexService = $residentialComplexService;
    }

    public function generate(PDFRequest $request, $code)
    {
        $parameters = [
            'show_residential_complex' => true,
            'residential_complex_slug' => $code
        ];

        $residentialComplex = $this->residentialComplexRepository->listResidentialComplex($parameters);
        $resource = new ResidentialComplexResource($residentialComplex->first(), $parameters);
        $response = $resource->response();
        $responseData = $response->getContent();
        $data = json_decode($responseData, true, 512, JSON_UNESCAPED_UNICODE);
        $fullData = $data['data'];

//        $pdf = PDF::loadView('pdf.pdf_view', $data)
//            ->setPaper('a4')
//            ->setOrientation('landscape')
//            ->setOption('dpi', 300)
////            ->setOption('page-width', 3508)
////            ->setOption('page-height', 2480)
//        ;
//        return $pdf->stream('invoice.pdf');
////        return $pdf->download('invoice.pdf');
//
//        $html = view('pdf.pdf_view')->render();
//
//        $html2pdf = new Html2Pdf();
////        $html2pdf->writeHTML('<h1>HelloWorld</h1>This is my first test');
//        $html2pdf->writeHTML($html);
//        $html2pdf->
//        $html2pdf->output();

//        PDF::SetTitle('Hello World');
//        PDF::AddPage();
//        PDF::Write(0, 'Hello World');
//        PDF::Output('hello_world.pdf');

        $html = view('pdf.pdf_view')->render();

//        $pdf = new TCPDF();
        PDF::SetTitle("List of users");
        PDF::AddPage();
        PDF::SetMargins(5, 5, 5);
        PDF::writeHTML($html, true, false, true, false, '');
        // D is the change of these two functions. Including D parameter will avoid
        // loading PDF in browser and allows downloading directly
//        $pdf::Output('userlist.pdf', 'D');
        PDF::Output('userlist.pdf', 'I');
    }

//    public function generate(PDFRequest $request, $code)
//    {
//        $parameters = [
//            'show_residential_complex' => true,
//            'residential_complex_slug' => $code
//        ];
//
//        $residentialComplex = $this->residentialComplexRepository->listResidentialComplex($parameters);
//        $resource = new ResidentialComplexResource($residentialComplex->first(), $parameters);
//        $response = $resource->response();
//        $responseData = $response->getContent();
//        $data = json_decode($responseData, true, 512, JSON_UNESCAPED_UNICODE);
//        $fullData = $data['data'];
////        $pdf = new PDF();
////        $pdf::stream();
//        $pdf = PDF::loadView('pdf.pdf_view', $fullData)->setPaper('a4', 'landscape');
////        $pdf = PDF::loadView('pdf.pdf_view_copy', $fullData)->setPaper('a4', 'landscape');
//
////        return $pdf->download('medium.pdf');
//        return $pdf->stream();
//    }

    public function html(PDFRequest $request, $code)
    {
        $parameters = [
            'show_residential_complex' => true,
            'residential_complex_slug' => $code
        ];

        $residentialComplex = $this->residentialComplexRepository->listResidentialComplex($parameters);
        $resource = new ResidentialComplexResource($residentialComplex->first(), $parameters);
        $response = $resource->response();
        $responseData = $response->getContent();
        $data = json_decode($responseData, true, 512, JSON_UNESCAPED_UNICODE);
        $fullData = $data['data'];
//        $pdf = new PDF();
//        $pdf::stream();
        $pdf = PDF::loadView('pdf.pdf_view', $fullData)->setPaper('a4', 'landscape');
//        $pdf = PDF::loadView('pdf.pdf_view_copy', $fullData)->setPaper('a4', 'landscape');

//        return $pdf->download('medium.pdf');
        return view('pdf.pdf_view');
    }

    public function fileExtension($file)
    {
        $mime = Image::make($file)->mime();
        $extensionExplode = explode('/', $mime);
        if (count($extensionExplode) > 0 && in_array($mime, $this->extension)) {
            return $extensionExplode[1];
        }

        return false;
    }
}

//        if ($request->isMethod("POST")) {
//            $message = $request->get('message');
//
//            $pdf = PDF::loadHTML($message)
//                ->setPaper('a4', 'landscape');
////            $pdf->loadHTML("$message");
//            return $pdf->stream();
//
////            $fullData = [];
////            $pdf = PDF::loadView('pdf.pdf_view', $fullData);
//////                ->setPaper('a4', 'landscape');
//////        return $pdf->download('medium.pdf');
////            return $pdf->stream();
//        }
//
//        return view('pdf.index', ['requests' => $request]);
//        $data = $resourse->resolve();
//        // This  $data array will be passed to our PDF blade
//        $data = [
//            'title' => 'First PDF for Medium',
//            'heading' => 'Hello from 99Points.info',
//            'content' => "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
//            ];
//
//$pathPng = public_path(env('UPLOAD_PATH') . '/' . \App\Helpers\ImageSize::PLANNING);
//$pathPngFull = public_path(env('UPLOAD_PATH') . '/' . \App\Helpers\ImageSize::PLANNING_FULL);
//$pathPngFullWebp = public_path(env('UPLOAD_PATH') . '/' . \App\Helpers\ImageSize::PLANNING_FULL_WEBP);
//
//$plannings = \App\Planning::all();
//foreach ($plannings as $planning) {
//    $imageName = $planning->image;
//
//    $imageWithPath = $pathPngFull . '/' . $imageName;
//    $imageWebpWithPath = $pathPngFullWebp . '/' . $imageName. ".webp";
//
//    if (!file_exists($imageWithPath) || !file_exists($imageWebpWithPath)) {
//        $explodeName = explode("-", $imageName);
//        $time = count($explodeName) > 0 ? (int)$explodeName[0] : "";
//        if (!empty($time)) {
//            $time += 1;
//
//            $searchImage = str_replace($explodeName[0], (string)$time, $imageWithPath);
//            $searchImageWebp = str_replace($explodeName[0], (string)$time, $imageWebpWithPath);
//
//
//            if (file_exists($searchImage)) {
//                rename($searchImage, $imageWithPath);
//            } else {
//                dump($searchImage . " " . 'not exist');
//            }
//
//            if (file_exists($searchImageWebp)) {
//                rename($searchImageWebp, $imageWebpWithPath);
//            } else {
//                dump($searchImageWebp . " " . 'not exist');
//            }
//        }
//    }
//}