<?php

namespace App\Console\Commands\Jobs;

use App\Core\Mail\PropertyCatalogMail;
use App\Core\Service\PropertyService;
use App\Exceptions\ApiErrorException;
use App\Models\Country;
use App\Models\Language;
use App\Models\vwBookingEngineSearch;
use Barryvdh\DomPDF\PDF;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class PropertyCatalogService extends Command
{
    protected $signature = 'cron:property-catalog-service {property_id} {language} {--email=}';

    protected $description = '';

    private $propertyService;

    public function __construct(
        PDF $pdf,
        Mailer $mailer,
        PropertyService $propertyService
    )
    {
        parent::__construct();

        $this->pdf = $pdf;
        $this->mailer = $mailer;
        $this->propertyService = $propertyService;
    }

    public function handle()
    {

        $this->info(date('Y-m-d H:i:s') . ' START');

        $pdfDataRequest = [
            'property_id' => $this->argument('property_id'),
            'locale' => $this->argument('language')
        ];

        $pdfContentService = App::make('App\Core\Service\PdfContentService');

        $factSheetDataResponse = $pdfContentService->factSheetData($pdfDataRequest);
        if ($factSheetDataResponse['status'] != 'success') {
            throw new Exception($factSheetDataResponse['message']);
        }

        $pageContent = $factSheetDataResponse['data'];
        $pdfLanguage = $pdfDataRequest['locale'];

        $this->info(date('Y-m-d H:i:s') . ' PDF');

        app('translator')->setLocale($pdfLanguage);
        $pdfService = $this->pdf->loadView('pdf.propertyCatalog', compact('pageContent'), [], 'UTF8');
        $pdfService->setOptions([
            'dpi' => 100,
            'isHtml5ParserEnabled' => true,
            'isRemoteEnabled' => true,
            'chroot', base_path(),
            'enable_html5_parser' => true,
            'enable_css_float' => true
            ]);


        $hotelName = Str::slug($pageContent['name'], '_', 'en') . '_' . $pdfLanguage;
        $pathStorage = Config::get('app.fileSystemDriver') . '/property-catalog/' . $hotelName . '.pdf';

        file_put_contents($pathStorage, $pdfService->output());

        if ($this->option('email')) {

            $this->info(date('Y-m-d H:i:s') . ' E-Mail');

            $languageDetail = Language::where('code', $pdfLanguage)->first();
            $languageDetail = $languageDetail ? $languageDetail->toArray() : null;

            $mailParams = [
                'email' => $this->option('email'),
                'catalogUrl' => Config::get('app.imageUrl') . '/property-catalog/' . $hotelName . '.pdf',
                'propertyName' => $pageContent['name'],
                'language' => $pdfLanguage,
                'languageText' => __($languageDetail['language_key'],[],'tr'),
                'pathStorage' => $pathStorage
            ];


            $this->mailer->send('emails.propertyCatalogMail', ['mailParams' => $mailParams], function ($message) use ($mailParams) {
                $message->to($mailParams['email'], 'Extranetwork Property Katalog')
                    ->bcc(Config::get('app.logMailAddress'))
                    ->subject($mailParams['propertyName'] . ' Catalog - ' . $mailParams['languageText'])
                    //->attach($mailParams['pathStorage'])
                    ->from(Config::get('app.mailSenderAddress'), 'Extranetwork');
            });

        }

        $this->info(date('Y-m-d H:i:s') . ' FINISHED');


    }
}
