package Maasha::KISS::Track;

# Copyright (C) 2009 Martin A. Hansen.

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# http://www.gnu.org/copyleft/gpl.html


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


# Routines for creating KISS tracks.


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


use warnings;
use strict;
use Data::Dumper;
use Maasha::Calc;
use vars qw( @ISA @EXPORT );

@ISA = qw( Exporter );


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


sub entries_sort
{
    my ( $entries,   # list of KISS entries
       ) = @_;

    # Returns nothing.

    @{ $entries } = sort { $a->{ 'S_BEG' } <=> $b->{ 'S_BEG' } or
                           $a->{ 'S_END' } <=> $b->{ 'S_END' } } @{ $entries };
}


sub track_feature
{
    my ( $width,     # draw window width
         $y_offset,  # y axis draw offset
         $beg,       # base window beg
         $end,       # base window end
         $entries,   # list of sorted KISS entries 
       ) = @_;

    # Returns a list.
    
    my ( $factor, $entry, $y_step, @ladder, $i, $x, $y, $w, @features );

    $factor = $width / ( $end - $beg );
    $y_step = 0;

    foreach $entry ( @{ $entries } )
    {
        $w = sprintf( "%.0f", ( $entry->{ 'S_END' } - $entry->{ 'S_BEG' } + 1 ) * $factor );

        if ( $w >= 1 )
        {
            $x = sprintf( "%.0f", ( $entry->{ 'S_BEG' } - $beg ) * $factor );

            for ( $y_step = 0; $y_step < @ladder; $y_step++ )
            {
                last if $x >= $ladder[ $y_step ] + 1; 
            }

            $y = $y_offset + ( 5 * $y_step );

            push @features, {
                id     => $entry->{ 'Q_ID' },
                x      => $x,
                y      => $y,
                height => 5,
                width  => $w,
            };

            $ladder[ $y_step ] = $x + $w;
        }
    }

    return wantarray ? @features : \@features;
}


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

1;


