<?php

namespace App\Core\Service;

use App\Core\Repository\Booking\BookingRepository;
use App\Core\Repository\Property\PropertyRepository;
use App\Exceptions\ApiErrorException;

use App\Notifications\PushNotificationPropertyUser;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Notification;
use Log;
use Exception;


class NotificationService
{

    public function __construct(
        BookingRepository $bookingRepository,
        PropertyRepository $propertyRepository
    )
    {
        $languageService = App::make("App\Core\Helper\LanguageService");

        $this->bookingRepository = $bookingRepository;
        $this->propertyRepository = $propertyRepository;
        $this->language = $languageService::getDefaultLang();
        $this->validLanguages = $languageService::getValidLanguages();
    }

    public function sendLogNotification($params = [])
    {

        $response = ['status' => false, 'message' => ''];
        try {


            $propertyDetailParam = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['property_id']]
                ],
                'firstRow' => true
            ];

            $propertyData = $this->propertyRepository->findByCriteria($propertyDetailParam);

            if (!$propertyData) {
                throw new Exception('api-unknown_error');
            }

            $param = [
                'property_id' => $params['property_id'],
                'subject' => 'Otel Bilgileri Güncelleme',
                'message' => $propertyData['name'] . ', Otel Bilgileri Güncellendi.'
            ];

            Notification::route('OneSignal', null)->notify(new PushNotificationPropertyUser($param));

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return $response;
    }

    public function sendNewBookingNotification($params = [])
    {

        $response = ['status' => false, 'message' => ''];
        try {

            $bookingDetailParam = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['booking_id']]
                ],
                'with' => ['bookingPayment', 'bookingProperty'],
                'firstRow' => true
            ];

            $bookingData = $this->bookingRepository->findByCriteria($bookingDetailParam);

            if (!$bookingData) {
                throw new Exception('api-unknown_error');
            }

            if (in_array(strtolower($bookingData['booking_property']['country']), $this->validLanguages)) {
                $this->language = strtolower($bookingData['booking_property']['country']);
            }

            $param = [
                'property_id' => $bookingData['booking_property']['id'],
                'subject' => '🛎️ '.__('notification-new_booking', [], $this->language),
                'message' => __('notification-new_booking_desc', ['hotel_name' => $bookingData['booking_property']['name'], 'amount' => $bookingData['total'], 'currency' => $bookingData['currency_code']], $this->language),
            ];

            Notification::route('OneSignal', null)->notify(new PushNotificationPropertyUser($param));

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return $response;
    }

    public function sendTicketNotification($params = [])
    {

        $response = ['status' => false, 'message' => ''];
        try {

            $bookingDetailParam = [
                'criteria' => [
                    ['field' => 'id', 'condition' => '=', 'value' => $params['booking_id']]
                ],
                'with' => ['bookingPayment', 'bookingProperty'],
                'firstRow' => true
            ];

            $bookingData = $this->bookingRepository->findByCriteria($bookingDetailParam);

            if (!$bookingData) {
                throw new Exception('api-unknown_error');
            }

            if (in_array(strtolower($bookingData['booking_property']['country']), $this->validLanguages)) {
                $this->language = strtolower($bookingData['booking_property']['country']);
            }

            $param = [
                'property_id' => $bookingData['booking_property']['id'],
                'subject' => '📥 '.__('notification-new_ticket', [], $this->language),
                'message' => __('notification-new_ticket_desc', [
                    'hotel' => $bookingData['booking_property']['name'],
                    'bookingCode' => $bookingData['booking_code']
                ], $this->language),
            ];

            Notification::route('OneSignal', null)->notify(new PushNotificationPropertyUser($param));

            $response['status'] = true;

        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return $response;
    }

}
