<?php

declare(strict_types=1);

namespace App\Scenario;

use App\Util\ParsedCommand;
use Pkirillw\MaxBotApi\Builder\Message as MessageBuilder;
use Pkirillw\MaxBotApi\Scheme\Enum\Format;
use Pkirillw\MaxBotApi\Scheme\Update\MessageCreatedUpdate;

final class AttachmentScenario extends AbstractScenario
{
    public function handle(MessageCreatedUpdate $update, ParsedCommand $command): void
    {
        switch ($command->name) {
            case 'attach_location':
                $this->attachLocation($update, $command);
                return;
            case 'attach_contact':
                $this->attachContact($update, $command);
                return;
            case 'attach_sticker':
                $this->attachSticker($update, $command);
                return;
        }
    }

    private function attachLocation(MessageCreatedUpdate $update, ParsedCommand $command): void
    {
        $lat = (float) ($command->arg(0) ?: '55.7558');
        $lng = (float) ($command->arg(1) ?: '37.6173');
        $this->api->messages->send(
            MessageBuilder::new()
                ->setChat($update->getChatId())
                ->setText(sprintf('Location: %.4f, %.4f (Moscow by default)', $lat, $lng))
                ->setFormat(Format::Markdown)
                ->addLocation($lat, $lng),
        );
    }

    private function attachContact(MessageCreatedUpdate $update, ParsedCommand $command): void
    {
        $name = $command->arg(0, 'Test Contact');
        $contactId = $command->argInt(1, $update->getUserId());
        $this->api->messages->send(
            MessageBuilder::new()
                ->setChat($update->getChatId())
                ->setText('Contact attachment:')
                ->addContact($name, $contactId),
        );
    }

    private function attachSticker(MessageCreatedUpdate $update, ParsedCommand $command): void
    {
        $code = $command->arg(0, '281');
        $this->api->messages->send(
            MessageBuilder::new()
                ->setChat($update->getChatId())
                ->addSticker($code),
        );
    }
}
