<?php
namespace App;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use setasign\Fpdi\Fpdi;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ImagePDFSignature{

    /**
     * @param $file
     * @return BinaryFileResponse
     *
     */
    public function process($file, $app)
    {
        Storage::disk('local')->put('test.pdf', $file);

        $outputFile = Storage::disk('local')->path('output.pdf');

        $this->fillPDF(Storage::disk('local')->path('test.pdf'), $outputFile, $app);

        return file_get_contents($outputFile);
        //return response()->file($outputFile);
    }

    public function fillPDF($file, $outputFile, $app)
    {
        $fpdi = new Fpdi();

        $date = Carbon::now()->toDateTimeString();
        $count = $fpdi->setSourceFile($file);
        $text = "Employee Name: ".ucwords(Auth::user()->name).", Employee Id: CF".Auth::user()->id;
        $lender = Lenders::where('id', 2)->first(['name']);
        for ($i=1; $i<=$count; $i++) {
            $template   = $fpdi->importPage($i);
            $size       = $fpdi->getTemplateSize($template);
            $fpdi->AddPage($size['orientation'], array($size['width'], $size['height']));
            $fpdi->useTemplate($template);
            $left = 10;
            $top =280;
            $fpdi->SetFont("helvetica", "", 8);
            $fpdi->SetTextColor(153,0,153);
            if($app->lender_id == 3){
                $fpdi->Text($left,$top,'Original seen & verified');
                $fpdi->Text($left,284,$text);
                $fpdi->Text($left,288, $date);
                $fpdi->Text($left,292, $lender->name);
            }else{
                $fpdi->Text($left,$top,$text);
                $fpdi->Text($left,284, $date);
                $fpdi->Text($left,288, $lender->name);
            }
        }
        return $fpdi->Output($outputFile, 'F');
    }

    public function signPDF($type, $path, $app){
        $localPath = $this->getFile($type, $path);
        return ['path' => "uploads/".$localPath['path'], 'file' => $this->process($localPath['s3']->get($path), $app)];
        //return $this->process(file_get_contents(storage_path('CREDIT FAIR EMI PROCESS.pdf')));
    }

    public function getFile($type, $path){
        $paths = explode('/', $path);
        $localPath = time().'_'.$paths[count($paths) -1];
//        if(strpos($type, 'aadhar_card') !== false) {
//            $s3 = Storage::disk('s3_temp');
//        }else{
            $s3 = Storage::disk('s3');
//        }
        Storage::disk('public_file')->put($localPath, $s3->get($path));
        return ['path' => $localPath, 's3' => $s3];
    }

    public function signImage($pathExt, $type, $path, $app){
        $localPath = $this->getFile($type, $path);
        $pdf = App::make('snappy.pdf.wrapper');
        $fullPath = "uploads/".$localPath['path'];
        $base64 = base64_encode(file_get_contents($fullPath));
        $html = "<img src='data:image/".$pathExt.";base64,".$base64."'/>";
        $pdf->loadHTML($html);

        return ['path' => $fullPath, 'file' => $this->process($pdf->output(), $app)];
    }
}
