package SVGManager;

use strict;
use warnings;


sub new {
   my ($class) = @_;
   my $self = {};
   bless $self, $class;
   return $self;
}

# colored bars
sub get_alignment_bar{
    my ($self, $ref_hash_align_data, $start, $end, $metric) = @_;
    my %align_data = %{$ref_hash_align_data};
    my $align_data_count = $end - $start + 1;
    my $step = 1;
    if ($align_data_count <= 100){
        $step = 5;
    } elsif ($align_data_count <= 200){
        $step = 4;
    } elsif ($align_data_count <= 250){
        $step = 3;
    } elsif ($align_data_count <= 400){
        $step = 2;
    } else {
        $step = 1;
    }
    if($align_data_count == 0) { return; }
    my $color_white     = '#ffffff';
    my $color_black     = '#000000';
    my $color_blue      = '#00b8ff';
    my $color_darkblue  = '#0000c8';
    my $color_sky       = '#00fffa';
    my $color_yellow    = '#ffff00';
    my $color_green     = '#00ff00';
    my $color_orange    = '#ff9900';
    my $color_red       = '#ff6900';
    my $color_darkred   = '#ff0000';
    my $color_grey      = '#f0f0f0';
    my $color_magenta   = '#cc33cc';
    my $height = 10;
    my $width = $align_data_count*$step;
    my $result = sprintf "<svg width=\"%spt\" height=\"%spt\">\n", $width, $height;
    for(my $i = 0; $i < $align_data_count; $i++){
        my $color = $color_white;
	my $res = $i + $start;
	if (exists $align_data{$res} ){
		if ($metric ne 'gdt_ts'){#'lddt' || $metric eq 'proq' || $metric eq 'qmean' || $metric eq 'qacons' || $metric eq 'tempy' || $metric eq 'atomincl'){
			if ($align_data{$res} >= 0.8) {
	                   $color = $color_sky;
	                } elsif ($align_data{$res} >= 0.6) {
        	           $color = $color_green;
	                } elsif ($align_data{$res} >= 0.4) {
        	           $color = $color_yellow;
	                } elsif ($align_data{$res} >= 0.2) {
                	   $color = $color_orange;
        	        } else {
	                   $color = $color_darkred;
                	}
		} elsif ($metric eq 'gdt_ts'){
                	if ($align_data{$res} <= 2.0) {
	                   $color = $color_sky;
	                } elsif ($align_data{$res} <= 4.0) {
	                   $color = $color_green;
	                } elsif ($align_data{$res} <= 6.0) {
	                   $color = $color_yellow;
	                } elsif ($align_data{$res} <= 8.0) {
	                   $color = $color_orange;
	                } else {
	                   $color = $color_darkred;
               		}
        	}
        }
	my $x = ($res-$start)*$step;
        $result .= sprintf "<g><title>%s:%s</title>\n", $res, (exists $align_data{$res} ?  sprintf("%.2f", $align_data{$res}) : '');
        $result .= sprintf "<rect x=\"%spt\" y=\"0pt\" width=\"%spt\" height=\"%spt\" fill=\"%s\"/>\n", $x, $step, $height, $color;
        $result .= sprintf "</g>\n";
    }
    $result .= "</svg>";
    return $result;
}

sub get_scale_bar {
    my ($self, $start, $end) = @_;
    my $target_size = $end - $start + 1;
    my $step = 1;
    my $incr = 50;
    if ($target_size <= 100) {
            $step = 5;
            $incr = 10;
    } elsif ($target_size <= 200){
            $step = 4;
            $incr = 20;
    } elsif ($target_size <= 250){
            $step = 3;
            $incr = 20;
    } elsif ($target_size <= 400){
            $step = 2;
            $incr = 40;
    } else {
            $step = 1;
            $incr = 50;
    }
    my $height = 10;
    my $width = $target_size*$step;
    my $result = sprintf "<svg width=\"%spt\" height=\"%spt\" fill=\"white\">\n", $width, $height;
    for (my $i = $start; $i <= $end; $i++){
        if ($i % 10 == 0) {
                $result .= sprintf "<line x1=\"%dpt\" y1=\"%dpt\" x2=\"%dpt\" y2=\"%dpt\" stroke=\"gray\"/>\n", ($i-$start)*$step + 0.5*$step, $height, ($i-$start)*$step + 0.5*$step, 0.6*$height;
        }
        if ($i % $incr == 0) {
                $result .= sprintf "<text x=\"%dpt\" y=\"%dpt\" fill=\"gray\" font-family=\"Arial,Verdana,Helvetica,Sans-serif\" font-size=\"10\">%s</text>\n", ($i-$start)*$step + 4, 0.8*$height, $i;
        }
    }
    $result .= sprintf "</svg>\n";
    return $result;
}

1;
