<?php

namespace Lc\SovBundle\Notification;

use Lc\SovBundle\Component\StringComponent;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Twig\Environment;

class SmsFactorNotification
{
    const TO_USER = 'to-user';
    const CONTENT_MESSAGE = 'content-message';
    const CONTENT_TEMPLATE = 'content-template';
    const CONTENT_DATA = 'content-data';

    protected HttpClientInterface $client;
    protected ParameterBagInterface $parameterBag;
    protected MailMailjetNotification $mailMailjetNotification;
    protected StringComponent $stringComponent;
    protected Environment $templating;

    public function __construct(
        HttpClientInterface     $client,
        ParameterBagInterface   $parameterBag,
        MailMailjetNotification $mailMailjetNotification,
        StringComponent         $stringComponent,
        Environment             $templating
    )
    {
        $this->client = $client;
        $this->parameterBag = $parameterBag;
        $this->mailMailjetNotification = $mailMailjetNotification;
        $this->stringComponent = $stringComponent;
        $this->templating = $templating;
    }

    public function send($params = [])
    {
        $user = isset($params[self::TO_USER]) ? $params[self::TO_USER] : null;

        if ($user) {
            $phone = $this->stringComponent->formatPhoneNumber($user->getPhone());

            $message = '';
            if (isset($params[self::CONTENT_MESSAGE])) {
                $message = $params[self::CONTENT_MESSAGE];
            } elseif (isset($params[self::CONTENT_TEMPLATE])) {
                $template = $params[self::CONTENT_TEMPLATE];
                $paramsTemplate = [];
                if (isset($params[self::CONTENT_DATA]) && is_array($params[self::CONTENT_DATA])) {
                    $paramsTemplate = $params[self::CONTENT_DATA];
                }
                $message = $this->templating->render($template, $paramsTemplate);
            }

            if ($this->parameterBag->get('mailjet.dev.redirect.active') == 1) {
                $this->mailMailjetNotification->send([
                    MailMailjetNotification::SUBJECT => 'Notification par SMS à ' . $phone,
                    MailMailjetNotification::TO_EMAIL => $user->getEmail(),
                    MailMailjetNotification::CONTENT_TEMPLATE => 'mail/notification',
                    MailMailjetNotification::CONTENT_DATA => [
                        'message' => $message
                    ],
                ]);

                return true;
            } else {
                $token = $this->parameterBag->get('smsfactor.token');
                $from = $this->parameterBag->get('smsfactor.from');

                if ($token && strlen($token) > 0) {

                    $response = $this->client->request(
                        'GET',
                        'https://api.smsfactor.com/send',
                        [
                            'headers' => [
                                'Authorization' => 'Bearer ' . $token,
                                'Content-Type' => 'application/json; charset=utf-8',
                                'Accept' => 'application/json'
                            ],
                            'query' => [
                                'sender' => $from,
                                'to' => $phone,
                                'text' => $message,
                            ],
                        ]
                    );

                    return $response;
                } else {
                    throw new \ErrorException('Le token SMS SmsFactor n\'est pas défini.');
                }
            }
        }

        return false;
    }

}