<?php

namespace App\Modules\Bookings;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Log;
use App\Models\User;
use App\Seraph\Services\GlobalMailerService;
use App\Modules\Notifications\Notification;
use App\Modules\Notifications\NotificationSetting;
use Exception;

class MessageNotificationsObserver
{
    /**
     * Executed after a message is created.
     *
     * @param  App\Modules\Bookings\Message $model
     *
     * @return void
     */
    public function created($model)
    {
        $this->sendAssignedNotifications($model);
    }

    /**
     * Return a list of posible templates for emails
     * and their subject.
     *
     * @return array
     */
    private function getTemplates()
    {
        return [
            'assigned' => [
                'message' => [
                    'template' => 'assigned_new_message',
                    'subject'  => 'A new message has been assigned to you',
                ],

            ],
        ];
    }

    /**
     * Retrieve a single template.
     *
     * @param  string $event
     * @param  string $type
     *
     * @return array
     */
    private function getTemplate($event, $type)
    {
        $templates = $this->getTemplates();

        if (! isset($templates[$event])) {
            return false;
        }

        $templates = $templates[$event];

        return (isset($templates[$type]))
                ? $templates[$type]
                : false;
    }

    /**
     * Checks to see weather a user has enabled his notifications,
     * and if it matches any of the rules it sends the notification.
     *
     * @param App\Modules\Bookings\Task $model
     * @param string $notification (template)
     * @param string $subject
     *
     * @return null
     */
    private function sendNotification($model, $notification, $subject)
    {
        $settings = NotificationSetting::notification($notification)->user($model->assigned_to)->first();

        // On page alert notification
        if ($settings && $settings->alert) {
            Notification::create([
                'user_id'      => $model->assigned_to,
                'related_type' => 'message',
                'related_id'   => $model->id,
                'message'      => $subject,
            ]);
        }

        // Email notification
        if ($settings && $settings->email) {
            try {
                GlobalMailerService::send('emails.notifications.'.$notification, ['Message' => $model], function ($message) use ($model, $subject) {
                    $message->to(User::find($model->assigned_to)->email)->subject($subject);
                });

                Log::info('Emailed user '.$model->assigned_to.' with template '.$notification.' for message #'.$model->id);
            } catch (Exception $e) {
                Log::error($e);
            }
        }
    }

    /**
     * Sends alert & email notifications to users if the rules applies to them.
     *
     * @return null
     */
    private function sendAssignedNotifications($model)
    {
        if ($template = $this->getTemplate('assigned', 'message')) {
            $this->sendNotification($model, $template['template'], $template['subject']);
        }
    }
}
