<?php
/*
 *  Copyright 2022.  Baks.dev <admin@baks.dev>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

namespace BaksDev\Users\Profile\UserProfile\Message\SendProfileTelegramm;

use BaksDev\Users\Profile\UserProfile\Repository\ProfileTelegramm\ProfileTelegrammInterface;
use BaksDev\Users\Profile\UserProfile\Message\SendProfileTelegramm\SendProfileTelegrammDTO;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final class SendProfileTelegrammHandler implements MessageHandlerInterface
{
    private ChatterInterface $chatter;
    private ProfileTelegrammInterface $profileTelegramm;
    
    public function __construct(
      ChatterInterface $chatter,
      ProfileTelegrammInterface $profileTelegramm
    )
    {
        $this->chatter = $chatter;
        $this->profileTelegramm = $profileTelegramm;
    }
    
    public function __invoke(SendProfileTelegrammDTO $command)
    {
        /* Получаем идентификатор чата профиля */
        $chatId = $this->profileTelegramm->get($command->getProfile());
        
        if($chatId)
        {
            $chatMessage = new ChatMessage($command->getMessage());
            
            $telegramOptions = new TelegramOptions();
            $telegramOptions
              ->chatId($chatId)
              ->parseMode('MarkdownV2')
              ->disableWebPagePreview(true)
              ->disableNotification(true);
            
            if($command->getHref())
            {
                /*$url = $this->urlGenerator->generate(
                  'users-profile-user:admin.newedit.edit',
                  ['id' => $command->getEvent()],
                  UrlGeneratorInterface::ABSOLUTE_URL);*/
                
                $telegramOptions->replyMarkup(
                  (new InlineKeyboardMarkup())
                    ->inlineKeyboard
                    ([
                        (new InlineKeyboardButton('Перейти в раздел'))
                          ->url($command->getHref()),
                      ])
                );
            }
            
            $chatMessage->options($telegramOptions);
            $this->chatter->send($chatMessage);
        }
        
        return true;
    }
}

