package RRResultsUploadManager;

use strict;
use warnings;

use DBI;
use lib qw(Core);

use Database;
use Configuration;
#use Statistics;

use RRResultsManager;

my $resultsupload_manager = undef;

1;

sub new {
    my ($class) = @_;

    return $resultsupload_manager if(defined($resultsupload_manager));

    my $self = {
        _id => undef,
        _database => Database->new($CONFIG->{HOSTNAME}, $CONFIG->{PORT}, $CONFIG->{DATABASE}, $CONFIG->{USERNAME}, $CONFIG->{PASSWORD})
    };

    $resultsupload_manager = bless $self, $class;
    return $resultsupload_manager;

}

sub parse_file{
    my ($self,$file,$refmodel1,$refmodel2,$refmodel3,$refmodel4) = @_;
    my %model1 = %{$refmodel1};my %model2 = %{$refmodel2};my %model3 = %{$refmodel3};my %model4 = %{$refmodel4};
    open (FILE,"< $file") || die "Couldn't open file $file\n";
    my $line=<FILE> ; # 1-st line
    chomp $line;
    if($line =~ m/([TR][0-9]{4,4}(s\d){0,1})RR([0-9]{3,3})[-_]([1-5])/){
	$model1{TARGET_NAME} = $1;$model2{TARGET_NAME} = $1;$model3{TARGET_NAME} = $1;$model4{TARGET_NAME} = $1;
	$model1{GROUP_CODE} = $3;$model2{GROUP_CODE} = $3;$model3{GROUP_CODE} = $3;$model4{GROUP_CODE} = $3;
	$model1{model_index} = $4;$model2{model_index} = $4;$model3{model_index} = $4;$model4{model_index} = $4;
    }

    $line=<FILE>; chomp $line; # 2-nd line
    if($line =~ m/^TARGET:\s+([TR].*)[-_]D([1-9]+)/){
	$model1{domain_index} = $2;$model2{domain_index} = $2;$model3{domain_index} = $2;$model4{domain_index} = $2;
    }else{
	$model1{domain_index} = 0;$model2{domain_index} = 0;$model3{domain_index} = 0;$model4{domain_index} = 0;
    }

    $line = <FILE>; # 3-rd line
    $line = <FILE>; chomp $line; # 4-th line
    if($line =~ m/^CheckSize_Coefficient:\s+(\S+)/){
	$model1{checksize} = $1;$model2{checksize} = $1;$model3{checksize} = $1;$model4{checksize} = $1;
    }
    $line = <FILE>; # 5-th line

    $line = <FILE>; chomp $line; # 6-th line
    if($line =~ m/^([LSM]{1,2}):\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/){
	$model1{range}=$1;$model1{acc}=$2;$model1{cov}=$3;$model1{imp}=$4;$model1{xd}=$5;$model1{number_targ_conts}=$6;
    }

    $line = <FILE>; chomp $line; # 7-th line
    if($line =~ m/^([LSM]{1,2}):\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/){
	$model2{range}=$1;$model2{acc}=$2;$model2{cov}=$3;$model2{imp}=$4;$model2{xd}=$5;$model2{number_targ_conts}=$6;
    }

    $line = <FILE>; chomp $line; # 8-th line
    if($line =~ m/^([LSM]{1,2}):\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/){
	$model3{range}=$1;$model3{acc}=$2;$model3{cov}=$3;$model3{imp}=$4;$model3{xd}=$5;$model3{number_targ_conts}=$6;
    }

    $line = <FILE>; chomp $line; # 9-th line
    if($line =~ m/^([LSM]{1,2}):\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/){
	$model4{range}=$1;$model4{acc}=$2;$model4{cov}=$3;$model4{imp}=$4;$model4{xd}=$5;$model4{number_targ_conts}=$6;
    }


    close(FILE);
    return (\%model1, \%model2, \%model3, \%model4);
}

sub upload{
    my ($self, $file_name, $file_dir) = @_;
    my $rr_manager1 = new RRResultsManager();
    my %rr_model1 = %{$rr_manager1->get_new_model()};
    my $rr_manager2 = new RRResultsManager();
    my %rr_model2 = %{$rr_manager2->get_new_model()};
    my $rr_manager3 = new RRResultsManager();
    my %rr_model3 = %{$rr_manager3->get_new_model()};
    my $rr_manager4 = new RRResultsManager();
    my %rr_model4 = %{$rr_manager4->get_new_model()};

    my $file = $file_dir."/".$file_name;
    
    my ($ref1, $ref2, $ref3, $ref4) = $self->parse_file($file,\%rr_model1,\%rr_model2,\%rr_model3,\%rr_model4);

    %rr_model1 = %{$ref1}; %rr_model2 = %{$ref2}; %rr_model3 = %{$ref3}; %rr_model4 = %{$ref4};
    
    my $id = $rr_manager1->exist_by_parameters(\%rr_model1);
    if( $id > 0){
	$rr_manager1->delete($id);
    }
    if($rr_model1{acc} < 0 || $rr_model1{cov} < 0 || $rr_model1{number_targ_conts} == 0){
    } else {
    	$rr_manager1->add(\%rr_model1);
    }

    $id = $rr_manager2->exist_by_parameters(\%rr_model2);
    if( $id > 0){
        $rr_manager2->delete($id);
    }
    if($rr_model2{acc} < 0 || $rr_model2{cov} < 0 || $rr_model2{number_targ_conts} == 0){
    } else {
        $rr_manager2->add(\%rr_model2);
    }

    $id = $rr_manager3->exist_by_parameters(\%rr_model3);
    if( $id > 0){
        $rr_manager3->delete($id);
    }
    if($rr_model3{acc} < 0 || $rr_model3{cov} < 0 || $rr_model3{number_targ_conts} == 0){
    } else {
        $rr_manager3->add(\%rr_model3);
    }


    $id = $rr_manager4->exist_by_parameters(\%rr_model4);
    if( $id > 0){
        $rr_manager4->delete($id);
    }
    if($rr_model4{acc} < 0 || $rr_model4{cov} < 0 || $rr_model4{number_targ_conts} == 0){
    } else {
        $rr_manager4->add(\%rr_model4);
    }

}

sub upload_zscore_for_target_domain{
    my ($self,$targ_name, $dom_index) = @_;
    my $rrresult_manager = new RRResultsManager();
    $rrresult_manager->calc_zscore_for_target_domain($targ_name,$dom_index);
}


