<?php

namespace App\Core\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;

class OfferAcceptMail extends Mailable
{

    use Queueable, SerializesModels;


    public $tries = 5;
    public $timeout = 30;

    protected $param;


    public function __construct($param)
    {
        $this->param = $param;
    }

    public function build()
    {
        try {
            $params = $this->param;

            $propertyService = App::make("App\Core\Service\OfferService");

            $requestParams = [
                'offer_id' => fillOnUndefined($params, 'offer_id'),
                'property_id' => fillOnUndefined($params, 'property_id'),
            ];


            $offerDetail = $propertyService->findOffer($requestParams);
            $offerDetail = fillOnUndefined($offerDetail, 'data');

            app('translator')->setLocale($offerDetail["language"]);
            $mailSenderAddress = Config::get('app.mailSenderAddress');

            $mailParams['to'] = [];
            $mailParams['to'] = $offerDetail['email'];

            $mailParams['cc'] = [];
            if (!empty($offerDetail['create_user'])) {
                $mailParams['cc'][] = $offerDetail['create_user']['email'];
            }

            $mailParams['bcc'] = [];
            $mailParams['bcc'][] = Config::get('app.logMailAddress');


            $mailParams['property_name'] = $offerDetail['property']['name'];
            $mailParams['url'] = Config::get('app.client_server').'/offer/'.$offerDetail['offer_code'];


            $mailParams['paymentUrl'] = null;
            if(isset($params['paymentUrl']) && !is_null($params['paymentUrl'])) {
                $mailParams['paymentUrl'] = $params['paymentUrl'];
            }

            $description = [];
            //$description[] = '<b>'.$offerDetail['property']['name'].'</b> tarafından oluşturulan <b>'.$offerDetail['ticket_code'].'</b> kodlu teklif onaylanmıştır.';
            $description[] = __('api-mailing-offer_accept_mail-description', ['propertyName' => $offerDetail['property']['name'], 'code' => $offerDetail['ticket_code']]);

            if(!is_null($offerDetail['payment_type_mapping_id']) && !empty($mailParams['paymentUrl'])) {
                //$description[] = 'Teklife ait ödemenizi aşağıdaki <b>Ödeme Linki</b> üzerinden güvenle gerçekleştirebilirsiniz.';
                $description[] = __('api-mailing-offer_accept_mail-payment');
            }

            $description = implode('<br><br>', $description);

            $mailParams['logo'] = Config::get('app.webUrl').'/assets/img/logo/mini-logo.png';
            if(isset($offerDetail['property_brand']['logoUrl'])) {
                $mailParams['logo'] = $offerDetail['property_brand']['logoUrl'];
            }


            /*echo view('emails.offerAcceptMail', compact('mailParams', 'description'));
            die();*/

            return $this->from($mailSenderAddress, 'Extranetwork - ' . $mailParams['property_name'])
                ->view('emails.offerAcceptMail', compact('mailParams', 'description'))
                ->to($mailParams['to'])
                ->cc($mailParams['cc'])
                ->bcc($mailParams['bcc'])
                ->subject(__('api-mailing-offer_accept_mail-title', ['code' => $offerDetail['ticket_code']]))
                ->with(['message' => $this]);


        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            return output(['status' => -1, 'message' => $e->getMessage()]);
        }
    }

}
