<?php
namespace App\Mail;

use App\Models\UserMessage;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class MailCustomMessage extends Mailable
{
    use Queueable, SerializesModels;

    protected $data;
    protected $user;
    protected $sender;
    public $subject;
    public $message;
    public $message_last;
    protected $save;


    public function __construct(User $user, $data, $sender, $save = false)
    {
        $this->save = $save;
        $this->data = $data;
        $this->user = $user;
        $this->sender = $sender;
        $this->subject = isset($data['subject']) ? $data['subject'] : __('email.email_subject');
        $this->message = isset($data['message']) ? $data['message'] : '';
        $this->message_last = isset($data['message_last']) ? $data['message_last'] : '';
    }


    public function build()
    {
        $salutation = __('email.salutation').",";
        if($this->user->account){
            if($this->user->account->salutation === "mr"){
                $salutation =  __('email.dear_sir')." ".$this->user->account->first_name.",";
            }else{
                $salutation = __('email.dear_mrs')." ".$this->user->account->first_name.",";
            }
        }
        if($this->save){
            UserMessage::create([
                'user_id' =>  $this->user->id,
                'send_user_id' => $this->sender->id,
                'email' => $this->user->email,
                'subject' => $this->subject,
                'message' => $this->message." ".$this->message_last,
                'send' => true,
                'sent_at' => now(),
            ]);
        }
        $url = "";
        $button = "";
        if(isset($this->data['confirmation_code'])){
            $url = route('register_verify', $this->data['confirmation_code']);
            $button =  __('email.button_account');

        }

        if(isset($this->data['url'])){
            $url =$this->data['url'];
            $button =  $this->data['button'];

        }
        return $this->view('emails.custom')->with([
            'url' => $url,
            'title' => $salutation,
            'button' => $button,
            'content' =>  $this->message,
            'content_last' =>  $this->message_last,
            'copy2line' => __('email.copy2line'),
            'copy3line' => __('email.copy3line'),
            'greetings' => __('email.greetings'),
            'sender' => __('email.sender'),

        ]);
    }
}
