#!/usr/bin/perl -w
use strict;
use warnings;
use OrkutStats;
use InternetStats;

my $orkutstats= GetOrkutStats();
my $inetstats= GetInetStats();

for my $c (sort { $orkutstats->{$b}{total}<=>$orkutstats->{$a}{total} }keys %$orkutstats) {
    printf("%s%s%s %s %s\n",
        fmt($orkutstats->{$c}{total}, 6),
        fmt($orkutstats->{$c}{female}, 6),
        fmt($orkutstats->{$c}{male}, 6),
        fmt(percent($orkutstats->{$c}{female}, $orkutstats->{$c}{total}), 2),
        #fmt($orkutstats->{$c}{total}/$inetstats->{xlatc($c)}{internetusers}),
        #fmt($orkutstats->{$c}{total}/$inetstats->{xlatc($c)}{population}),
        $c);
}
my %xlatc= (
'Afganistan'=>'Afghanistan',
'Bosnia'=>'Bosnia and Herzegovina',
'Brunei Darussalem'=>'Brunei Darussalam',
'Central African Rep.'=>'Central African Republic',
'Cote d\'Ivoire'=>'Cote D Ivoire',
'Gibraltar'=>'Gibralter',
'Guinea-Bissau'=>'Guinea Bissau',
'North Korea'=>'Korea (Peoples Republic of)',
'South Korea'=>'Korea (Republic of)',
'Kyrgystan'=>'Kyrgyzstan',
'Macao'=>'Macau',
'Micronesia'=>'Federated States of Micronesia',
'Monserrat'=>'Montserrat',
'Norfolk'=>'Norfolk Island',
'Northern Marianas'=>'Northern Mariana Islands',
'Papau New Guinea'=>'Papua New Guinea',
'Pitcairn Islands'=>'Pitcairn',
'Russia'=>'Russian Federation',
'Serbia and Montenegro'=>'Serbia',
'Syria'=>'Syrian Arab Republic',
'Gambia'=>'The Gambia',
'Vietnam'=>'Viet Nam',
);
sub xlati2o {
    my $c= shift;
    $c =~ s/\s*\(.*//;
    $c =~ s/(.*),\s*(.*)/$2 $1/;
    $c =~ s/&/and/;

    if (exists $xlatc{$c}) {
        return $xlatc{$c};
    }
    return $c;

}

sub fmt {
    my ($val, $len)= @_;
    if ($len-length("$val")) {
        return "[silver]".("_" x ($len-length("$val")) )."[/silver]$val";
    }
    else {
        return "$val";
    }
}
sub percent {
    my ($part, $total)= @_;
    if ($total) {
        return int(100*$part/$total);
    }
    else {
        return "-"
    }
}
