#!/usr/bin/perl

=encoding UTF-8
=cut

=head1 DESCRIPTION

=cut

# common modules
use strict;
use warnings FATAL => 'all';
use feature 'say';
use utf8;
use open qw(:std :utf8);

use Carp;
use Data::Dumper;
use List::Util qw(max);

use lib::abs qw(
    ../lib
);
use Utils;
use Countries;
use Branches;

# global vars

# subs
sub get_data {
    my $branches = Branches->new();
    my $countries = Countries->new();

    my @data;

    foreach my $file_name (glob '/data/branches/*') {
        my ($branch_id) = $file_name =~ m'.*/(.*)\.json\z';
        my $data = get_data_from_json_file $file_name;

        push @data, {
            branch_id => $branch_id,
            person_type => (
                $branches->working_with_balance($branch_id)
                    ? $branches->get_balance_person_type($branch_id)
                    : ''
            ),
            name => $branches->get_branch_name($branch_id, 'ru'),
            oferta => ($data->{oferta} ? 'x' : '-'),
            oferta_id => ($branches->is_oferta($branch_id) ? $branches->get_oferta_id($branch_id) : '-'),
            country_ids => ($data->{country_ids} ? (join ', ', map {$countries->get_country_name($_, 'ru') . " ($_)"} sort {$a <=> $b} @{$data->{country_ids}}) : ''),
            except_country_ids => ($data->{except_country_ids} ? (join ', ', map {$countries->get_country_name($_, 'ru') . " ($_)"} sort {$a <=> $b} @{$data->{except_country_ids}}) : ''),
        };
    }

    return \@data;
}

sub output_data {
    my ($data) = @_;

    my @fields = (
        branch_id => {},
        person_type => {},
        name => {},
        oferta => {},
        oferta_id => {},
        country_ids => {},
        except_country_ids => {},
    );

    my %id2length;

    foreach my $row (@{$data}) {
        foreach my $id (keys %{$row}) {
            $id2length{$id} = max($id2length{$id} // 0, length($row->{$id}), length($id));
        }
    }

    my $format = '';

    foreach my $f (grep { ref($_) eq '' } @fields) {
        $format .= '%-' . ($id2length{$f} + 3 ) . 's ';
    }

    my $header = sprintf $format, grep { ref($_) eq '' } @fields;
    say $header;
    say '-' x length($header);

    foreach my $row (@{$data}) {
        my @to_output;

        foreach my $f (grep { ref($_) eq '' } @fields) {
            push @to_output, $row->{$f};
        }

        say sprintf $format, @to_output;
    }

    return 1;
}

# main
sub main {

    my $data = get_data();

    output_data($data);

#    use DDP;
#    p $data;

#    my $branches = Branches->new();
#    my $countries = Countries->new();
#
#    my $format = '%15s  %-11s  %-32s  %-6s  %9s  %20s  %70s';
#
#    say sprintf $format,
#        ;
#
#    foreach my $file_name (glob '/data/branches/*') {
#        my ($branch_id) = $file_name =~ m'.*/(.*)\.json\z';
#        my $data = get_data_from_json_file $file_name;
#
#        say sprintf $format,
#            $branch_id,
#            (
#                $branches->working_with_balance($branch_id)
#                    ? $branches->get_balance_person_type($branch_id)
#                    : ''
#            ),
#            $branches->get_branch_name($branch_id, 'ru'),
#            '  ' . ($data->{oferta} ? 'x' : '-'),
#            ($branches->is_oferta($branch_id) ? $branches->get_oferta_id($branch_id) : '-'),
#            ($data->{country_ids} ? (join ', ', map {$countries->get_country_name($_, 'ru') . " ($_)"} sort {$a <=> $b} @{$data->{country_ids}}) : ''),
#            ($data->{except_country_ids} ? (join ', ', map {$countries->get_country_name($_, 'ru') . " ($_)"} sort {$a <=> $b} @{$data->{except_country_ids}}) : ''),
#            ;
#    }
#
}
main();
__END__
