package App::MPD::Feeder::Command;

use strict;
use warnings;
use utf8;

use Log::Any qw($log);
use Object::Pad;

class App::MPD::Feeder::Command
isa App::MPD::Feeder {
    method run(@args) {
        my $cmd = shift @args;

        if ( $cmd eq 'dump-config' ) {
            die "dump-config command accepts no arguments\n" if @args;

            $self->opt->dump;

            return 0;
        }

        if ( $cmd eq 'add-unwanted-artist' ) {
            die "Missing command arguments\n" unless @args;
            $self->set_db_needs_update(0);
            for my $artist (@args) {
                if ( $self->db_add_unwanted_artist($artist) ) {
                    $log->info(
                        "Artist '$artist' added to the unwanted list\n");
                }
                else {
                    $log->warn(
                        "Artist '$artist' already in the unwanted list\n");
                }
            }

            return 0;
        }

        if ( $cmd eq 'del-unwanted-artist' ) {
            die "Missing command arguments\n" unless @args;
            $self->set_db_needs_update(0);
            for my $artist (@args) {
                if ( $self->db_del_unwanted_artist($artist) ) {
                    $log->info(
                        "Artist '$artist' deleted from the unwanted list\n");
                }
                else {
                    $log->warn(
                        "Artist '$artist' is not in the unwanted list\n");
                }
            }

            return 0;
        }

        if ( $cmd eq 'add-unwanted-album' ) {
            die "NOT IMPLEMENTED\n";
        }

        if ( $cmd eq 'one-shot' ) {
            die "one-shot command accepts no arguments\n" if @args;

            $self->queue_songs( undef, sub {$self->mpd->loop->stop} );
            $self->mpd->loop->run;
            return 0;
        }
        elsif ( $cmd eq 'single' ) {
            die "single command accepts no arguments\n" if @args;

            $self->queue_songs( 1, sub {$self->mpd->loop->stop} );
            $self->mpd->loop->run;
            return 0;
        }
        else {
            die "Unknown command '$cmd'";
        }
    }
}
