<commit_before>package InstructorFSMController;

use strict;
use warnings;

use DumperUtils;

sub new {
	my ($class, $user, $chat_id, $api, $instructors, $resources, $recordtimeparser) = @_;
	my $self = {
		user => $user,
		chat_id => $chat_id,

		api => $api,
		instructors => $instructors,
		resources => $resources,
		recordtimeparser => $recordtimeparser,

		dtf => DateTimeFactory->new,
		log => Log->new("instructorfsmcontroller"),
	};
	bless $self, $class;
}

sub _send_message {
	my ($self, $text) = @_;
	$self->{api}->send_message({
		chat_id => $self->{chat_id},
		text => $text
	});
}

sub _send_keyboard {
	my ($self, $text, $keyboard) = @_;
	$self->{api}->send_keyboard({
		chat_id => $self->{chat_id},
		text => $text,
		keyboard => $keyboard
	});
}

sub send_start_message {
	$self->_send_message(lz("instructor_start"));
}

sub send_cancel_message {
	$self->_send_message(lz("instructor_operation_cancelled"));
}

sub send_menu {
	my @keyboard = (
		lz("instructor_show_schedule"),
		lz("instructor_add_record"),
	);
	$self->_send_keyboard(lz("instructor_menu"), \@keyboard);
}

sub is_schedule_selected {
	my ($text) = @_;
	$text eq lz("instructor_show_schedule");
}

sub is_add_record_selected {
	my ($text) = @_;
	$text eq lz("instructor_add_record");
}

sub send_schedule {
	$self->_send_message(lz("instructor_schedule"));

	my $instructor = $self->{instructors}->name($api->{user}->{id});
	my $schedule = $self->{resources}->schedule($instructor);

	my $text = "";
	foreach my $resource (keys %$schedule) {
		$text .= $resource . ":\n";
		foreach my $event (@{$schedule->{$resource}}) {
			$text .= dt($event->{span}->start) . " - ";
			$text .= dt($event->{span}->end) . ": ";
			$text .= ($event->{busy} ? lz("busy") : lz("free")) . "\n";
		}
	}

	if ($text eq "") {
		$text = lz("instructor_schedule_is_empty");
	}

	$self->_send_message($text);
}

sub ask_record_time {
	my @keyboard = (
		lz("instructor_cancel_operation"),
	);
	$self->_send_keyboard(lz("instructor_record_time"), \@keyboard);
}

sub is_cancel_operation_selected {
	my ($text) = @_;
	$text eq lz("instructor_cancel_operation");
}

sub ask_record_time_failed {
	$self->_send_message(lz("invalid_record_time"));
}

sub parse_record_time {
	my ($text) = @_;
	$self->{recordtimeparser}->parse($text);
}

sub save_record {
	my ($record) = @_;<commit_msg>some bugs in InstructorFSMController fixed<commit_after>package InstructorFSMController;

use strict;
use warnings;

use DumperUtils;

sub new {
	my ($class, $user, $chat_id, $api, $instructors, $resources, $recordtimeparser) = @_;
	my $self = {
		user => $user,
		chat_id => $chat_id,

		api => $api,
		instructors => $instructors,
		resources => $resources,
		recordtimeparser => $recordtimeparser,

		dtf => DateTimeFactory->new,
		log => Log->new("instructorfsmcontroller"),
	};
	bless $self, $class;
}

sub _send_message {
	my ($self, $text) = @_;
	$self->{api}->send_message({
		chat_id => $self->{chat_id},
		text => $text
	});
}

sub _send_keyboard {
	my ($self, $text, $keyboard) = @_;
	$self->{api}->send_keyboard({
		chat_id => $self->{chat_id},
		text => $text,
		keyboard => $keyboard
	});
}

sub send_start_message {
	my ($self) = @_;
	$self->_send_message(lz("instructor_start"));
}

sub send_cancel_message {
	my ($self) = @_;
	$self->_send_message(lz("instructor_operation_cancelled"));
}

sub send_menu {
	my ($self) = @_;
	my @keyboard = (
		lz("instructor_show_schedule"),
		lz("instructor_add_record"),
	);
	$self->_send_keyboard(lz("instructor_menu"), \@keyboard);
}

sub is_schedule_selected {
	my ($self, $text) = @_;
	$text eq lz("instructor_show_schedule");
}

sub is_add_record_selected {
	my ($self, $text) = @_;
	$text eq lz("instructor_add_record");
}

sub send_schedule {
	my ($self) = @_;
	$self->_send_message(lz("instructor_schedule"));

	my $instructor = $self->{instructors}->name($self->{user}->{id});
	my $schedule = $self->{resources}->schedule($instructor);

	my $text = "";
	foreach my $resource (keys %$schedule) {
		$text .= $resource . ":\n";
		foreach my $event (@{$schedule->{$resource}}) {
			$text .= dt($event->{span}->start) . " - ";
			$text .= dt($event->{span}->end) . ": ";
			$text .= ($event->{busy} ? lz("busy") : lz("free")) . "\n";
		}
	}

	if ($text eq "") {
		$text = lz("instructor_schedule_is_empty");
	}

	$self->_send_message($text);
}

sub ask_record_time {
	my ($self) = @_;
	my @keyboard = (
		lz("instructor_cancel_operation"),
	);
	$self->_send_keyboard(lz("instructor_record_time"), \@keyboard);
}

sub is_cancel_operation_selected {
	my ($self, $text) = @_;
	$text eq lz("instructor_cancel_operation");
}

sub ask_record_time_failed {
	my ($self) = @_;
	$self->_send_message(lz("invalid_record_time"));
}

sub parse_record_time {
	my ($self, $text) = @_;
	$self->{recordtimeparser}->parse($text);
}

sub save_record {
	my ($self, $record) = @_;
{'fraction_non_alphanumeric': 0.2016875900391027, 'fraction_numerical': 0.0, 'mean_word_length': 4.827338129496403, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '337591', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1887, 'n_words': 329}