package App::Clifton::Server;

use aliased 'App::Clifton::ConsoleService';
use aliased 'App::Clifton::ConfigLoader';
use Moo;

extends 'App::Clifton::Service';

sub BUILD {
  my ($self, $args) = @_;
  $args->{loop}->add($self);
  $self->$_ for qw(console_service);
}

has config_file => (is => 'ro', required => 1);

has config_loader => (is => 'lazy');

has current_config => (is => 'rw');

sub _build_config_loader { ConfigLoader->new }

has console_service => (is => 'lazy');

sub _build_console_service {
  my ($self) = @_;
  $self->_new_child(ConsoleService, { server => $self });
}

sub reload_config { shift->_do(reload_config => @_) }
sub shutdown { shift->_do(shutdown => @_) }

sub _body_for_reload_config {
  my ($self, $args) = @_;
  my $config = $self->config_loader->config_from_file($self->config_file);
  $self->current_config($config);
  $args->{on_finished}->($config);
}

1;
