<?php

namespace Yajny\EventsYj\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Yajny\EventsYj\Traits\DynamicTemplate;
use Yajny\EventsYj\Models\UserNotification;

class UserAlertTemplate extends Model
{
    //
    use DynamicTemplate;

    public $timestamps = false;

    public $changeTitle  = true;

    public function notifyUser($event)
    {



        // build template
        foreach ($event as $eveKey => $eveObj) {
            if ($eveObj)
                $this->templateVarReplace($eveKey, $eveObj);
        }

        if ($event->user) {
            $this->webNotify($event->user);
            $this->pushNotify($event->user);
        }
    }

    public function webNotify($user)
    {

        $userNotification  = new UserNotification(['user_id' => $user->user_id, 'message' => $this->template, 'title' => $this->title, 'url' => $this->url]);
        $userNotification->save();
    }

    public function smsNotify()
    {
    }

    public function pushNotify($user)
    {
        $message = json_decode($this->template, true)[$user->user_lang];
        $title = json_decode($this->title, true)[$user->user_lang];
        $url = url($user->user_lang . $this->url);
        $icon = config('website.cnf_appurl') . '/iconset/512x512.png';

        $body = [
            'to' => [
                'Identity' => [$user->user_id]
            ],
            'tag_group' => 'Backend Notifications',
            'respect_frequency_caps' => false,
            'content' => [
                'title' => $title,
                'body' => $message,
                'platform_specific' => [
                    'safari' => [
                        'deep_link' => $url,
                    ],
                    'chrome' => [
                        'icon' => $icon,
                        'deep_link' => $url,
                        'require_interaction' => true,
                    ],
                    'firefox' => [
                        'icon' => $icon,
                        'deep_link' => $url,
                    ],
                ],
            ],
        ];

        $headers = [
            'X-CleverTap-Account-Id: ' . config('services.clevertap.account_id'),
            'X-CleverTap-Passcode: ' . config('services.clevertap.passcode'),
            'Content-Type: application/json; charset=utf-8',
        ];


        //Send Web Push
        $ch = curl_init();

        curl_setopt_array($ch, [
            CURLOPT_URL => "https://api.clevertap.com/1/send/webpush.json",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HTTPHEADER => $headers,
        ]);

        $result = curl_exec($ch);

        curl_close($ch);

        $body['content']['platform_specific'] = [
            'ios' => [
                'deep_link' => $url,
            ],
            'android' => [
                'deep_link' => $url,
                'large_icon' => $icon,
                'default_sound' => true
            ]
        ];


        //Send Mobile Push
        $ch2 = curl_init();

        curl_setopt_array($ch2, [
            CURLOPT_URL => "https://api.clevertap.com/1/send/push.json",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HTTPHEADER => $headers,
        ]);

        curl_exec($ch2);

        curl_close($ch2);
    }
}
