#!/usr/bin/env perl

use 5.024;

# Using Test2, important to specify which version of Test2
# since later versions may break things.
use Test2::V0;
use Test2::Bundle::More;
use Test::Exception;
# use JSON::MaybeXS qw/encode_json/;
# use YAML::XS;
use feature qw /postderef signatures/;
no warnings 'experimental';
# use Path::Tiny;
use Vote::Count::Method::Cascade;
# use Vote::Count::Charge::Utility 'FullCascadeCharge', 'NthApproval';
use Vote::Count::ReadBallots 'read_ballots';
use Test2::Tools::Exception qw/dies lives/;
use Test2::Tools::Warnings qw/warns warning warnings no_warnings/;
#use Vote::Count::Charge::TestBalance 'balance_ok';
use Data::Printer;
# use Storable 3.15 'dclone';
use Data::Dumper;
# use Carp::Always;

my $DMB1 =
  Vote::Count::Method::Cascade->new(
    Seats     => 4,
    BallotSet => read_ballots('t/data/Scotland2017/Dumbarton.txt'),
    VoteValue => 100000,
    IterationLog => '/tmp/votecount_cascademethod_dmb1',
    LogTo     => '/tmp/votecount_cascademethod_dmb1',
    FloorThresshold => 1,
  );

$DMB1->StartElection;
my @elected = $DMB1->Conduct();
# note( $DMB1->logv );
# note Dumper @elected;
my $expectwin = [ 'David_MCBRIDE_Lab',  'Elizabeth_RUINE_Lab', 'Iain_MCLAREN_SNP', 'Karen_CONAGHAN_SNP'];
is_deeply( \@elected, $expectwin, 'first run winners ***' );

my $DMB2 =
  Vote::Count::Method::Cascade->new(
    Seats     => 4,
    BallotSet => read_ballots('t/data/Scotland2017/Dumbarton.txt'),
    VoteValue => 100000,
    IterationLog => '/tmp/votecount_cascademethod_dmb2',
    LogTo     => '/tmp/votecount_cascademethod_dmb2',
    FloorThresshold => 1,
    FloorRule => 'TopCount',
    DropRule => 'bottomrunoff',
    FinalPhase => 0,
  );
$DMB2->StartElection;
@elected = $DMB2->Conduct();
# note( $DMB2->logv );
# note Dumper @elected;
$expectwin = [ 'Brian_WALKER_Con', 'David_MCBRIDE_Lab', 'George_BLACK_WDCP', 'Karen_CONAGHAN_SNP'];
is_deeply( \@elected, $expectwin, 'second run winners' );


my $DMB3 =
  Vote::Count::Method::Cascade->new(
    Seats     => 4,
    BallotSet => read_ballots('t/data/Scotland2017/Dumbarton.txt'),
    VoteValue => 100,
    IterationLog => '/tmp/votecount_cascademethod_dmb3',
    LogTo     => '/tmp/votecount_cascademethod_dmb3',
    FloorThresshold => 1,
    FloorRule => 'TopCount',
    DropRule => 'topcount',
    AutomaticDefeat => 'none',
    FinalPhase => 1,
  );
$DMB3->StartElection;
@elected = $DMB3->Conduct();
note( $DMB3->logv );

my $BIG1 =
  Vote::Count::Method::Cascade->new(
    Seats     => 4,
    BallotSet => read_ballots('t/data/biggerset1.txt'),
    VoteValue => 100,
    IterationLog => '/tmp/votecount_cascademethod_big1',
    LogTo     => '/tmp/votecount_cascademethod_big1',
    FloorThresshold => 1,
    FloorRule => 'TopCount',
    DropRule => 'topcount',
    AutomaticDefeat => 'none',
    FinalPhase => 1,
  );
$BIG1->StartElection;
@elected = $BIG1->Conduct();
note( $BIG1->logv );


# note Dumper @elected;
# 100000 82 10000 57 1000 36 100 16

done_testing();