#!/usr/bin/env perl
use strict;
use warnings;
use Mojo::IOLoop;
use Bot::ChatBots::Telegram::LongPoll;

my $bcb = Bot::ChatBots::Telegram::LongPoll->new(
   token     => $ENV{TOKEN},
   processor => \&process_record,
   start     => 0,   # DO NOT START RIGHT AWAY!
);

setup_recurring();

# now it's time to hand operations over to Mojo::IOLoop
$bcb->start;

# track nagging...
{
   my %nagged;
   sub setup_recurring {
      Mojo::IOLoop->recurring(
         10 => sub {
            my $sender = $bcb->sender;
            for my $chat_id (keys %nagged) {
               $sender->send_message(
                  {
                     text => 'whooops!',
                     chat_id => $chat_id,
                  }
               );
            }
         }
      );
   }
   sub nag_on  { $nagged{$_[0]} = 1 }
   sub nag_off { delete $nagged{$_[0]} }
}

sub process_record {
   my $record = shift;

   my $type    = $record->{data_type};
   my $payload = $record->{payload};
   if ($type eq 'Message' && exists($payload->{from}) ) {
      my $text      = $payload->{text} || '';
      my $peer_name = $payload->{from}{first_name} || 'U. N. Known';
      my $chat_id   = $record->{channel}{id};
      print {*STDERR} "$peer_name says: $text\n";
      if (($text eq '/start') || ($text eq '/help')) {
         $record->{send_response} = <<'END';
Very simple:
* for help type /help
* for greeting type /hello
* for being annoyed every 10 s type /nag on
* to stop annoyance type /nag off
* to be reminded type /remind <seconds> <message>
END
      }
      elsif ($text eq '/hello') {
         $record->{send_response} = "Hello to you, $peer_name";
      }
      elsif ($text eq '/nag on') {
         nag_on($chat_id);
         $record->{send_response} = 'OK, to deactivate /nag off';
      }
      elsif ($text eq '/nag off') {
         nag_off($chat_id);
         $record->{send_response} = 'OK, to reactivate /nag on';
      }
      elsif (my (          $delay,        $msg) = $text =~ m{
            \A /remind \s+ ([1-9]\d*) \s+ (.*)
         }mxs)
      {
         Mojo::IOLoop->timer($delay => sub {
            $bcb->sender->send_message(
               {
                  text => "$peer_name: remember $msg",
                  chat_id => $chat_id,
               }
            );
         });
         $record->{send_response} = "I'll try my best!";
      }
   }

   return $record; # follow on..
}
