#!/usr/bin/perl

use lib 'lib';
use strict;
use warnings;
use NGCP::API::TestFramework;
use threads;
use Thread::Queue;
use Data::Dumper;

my $server = $ARGV[0] || undef;
my $selected = $ARGV[1] || 'all';

if ( !$server ){
    print "Usage: \$ perl testrunner.pl [<testsystem>] [<testset>]\n";
    print "Usage example: \$ perl testrunner.pl 192.168.88.162\n";
    print "Usage example: \$ perl testrunner.pl 192.168.88.162 fast\n";
    print "Possible test set: all, stable, fast, t/api-rest2/tests-directory, t/api-rest2/Contracts.yaml\n";
    print "Default test set: all\n";
    exit(1);
}

my @test_files;

if ( $selected eq 'stable' ) {
    print "Test selection: stable\n";
}
elsif ( $selected eq 'fast' ) {
    print "Test selection: fast\n";
}
elsif ( $selected eq 'all' ) {
    print "Test selection: all\n";
    map { push @test_files, "./$_" } `ls ./t/api-rest2/*.yaml`;
}
elsif ( -d $selected ) {
    print "Test selection: all files in '$selected' directory\n";
    map { push @test_files, "./$_" } `ls ${selected}/*.yaml`;
}
else {
    print "Test selection: $selected\n";
    push @test_files, $selected;
}

print "################################################################################\n";
print "Finished main setup, now running tests ...\n";

local $ENV{CATALYST_SERVER_SUB}="https://$server:443";
local $ENV{CATALYST_SERVER}="https://$server:1443";
local $ENV{NGCP_SESSION_ID}=int(rand(1000)).time;

my @threads;
my $tests_queue = Thread::Queue->new();

for ( @test_files ) {
    $tests_queue->enqueue($_);
}

$tests_queue->end();

for (1..2) {
    push @threads, threads->create( {'context' => 'scalar'}, \&worker, $tests_queue, 0 );
}

my @exit_codes;
foreach ( @threads ){
  push @exit_codes, $_->join();
}

sub worker {
    my ( $tests_queue, $exit_code ) = @_;

    while ( my $test_file = $tests_queue->dequeue_nb() ) {
        chomp $test_file;
        my $start_time = time;
        print "Running tests from $test_file\n";
        my $test_framework = NGCP::API::TestFramework->new( {
            file_path => $test_file,
            unique_id => int(rand(100000)),
        } );

        my $result = $test_framework->run();

        my $total_time = time - $start_time;
        print "Finished test execution for $test_file\n";
        if ( !$result->{success} ) {
            $exit_code = 1;
            print $result->{error_count}." errors were found!\n";
        }
        if ( $result->{warnings} && scalar @{$result->{warnings}} > 0 ) {
            print "Warnings were found!\n";
        }
        print "Tests for $test_file took $total_time seconds.\n\n";
    }
    return $exit_code;
}

if ( grep { $_ == 1 } @exit_codes ) {
    exit 1;
}
exit 0;
