// Copyright (c) 2002, Peter Bentley
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// 
//   Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 
//   Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 
//   The name Peter Bentley may not be used to endorse or promote
// products derived from this software without specific prior
// written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//


#import "OmdTransport.h"
#import "OmdToc.h"
#import "OmdUnit.h"
#import "OmdTime.h"
#import "OmdTrackInfo.h"
#import "OmdPosition.h"
#import "omd.h"

CVSID( "$Id: OmdTransport.m,v 1.9 2002/11/24 20:48:11 pete Exp $" );

@implementation OmdTransport

- (void) dealloc
{
    [toc release];
    [lastpos release];
    [super dealloc];
}
    


- (IBAction) play: (id) sender
{
    slog( LOG_INFO, "Start playback" );
    [omdUnit play];
}

- (IBAction) stop: (id) sender
{
    slog( LOG_INFO, "Stop" );
    [omdUnit stop];
}

- (IBAction) pause: (id) sender
{
    slog( LOG_INFO, "Pause" );
    [omdUnit pause];
}

- (IBAction) fforward: (id) sender
{
    slog( LOG_INFO, "Fast Forward" );
    [omdUnit fforward];
}

- (IBAction) rewind: (id) sender
{
    slog( LOG_INFO, "Rewind" );
    [omdUnit rewind];
}

- (IBAction) nextTrack: (id) sender
{
    slog( LOG_INFO, "Next track" );
    [omdUnit nextTrack];
}

- (IBAction) previousTrack: (id) sender
{
    slog( LOG_INFO, "Previous track" );
    [omdUnit previousTrack];
}

- (IBAction) setPosition: (NSSlider *) sender
{
    OmdTime	*t;
    double	value = [sender doubleValue];
    OmdPosition *pos = [omdUnit getPlaybackPosition];

    t = [[[OmdTime alloc] init] autorelease];
    [t setDoubleValue: value];
    [pos setTime: t];

    slog( LOG_INFO, "Set Position: track %d/%s", [pos track],
        [[t stringValue] lossyCString] );

    [omdUnit setPlaybackPosition: pos];
}

- (void) refresh
{
    [toc release];
    toc = [[omdUnit toc] retain];
}

- (void) updateView
{

    if( [omdUnit discInserted: YES] )
    {
        OmdPosition *pos = [omdUnit getPlaybackPosition];
        if( ! pos )
        {
            // XXX Do something
            return;
        }
        OmdTrackInfo *track = [toc trackInfo: [pos track]];
        OmdTime *tracktime = [track length];
        OmdTime *currtime = [pos time];
        
        if( ( lastpos == nil ) || ! [currtime isSameTimeAs: [lastpos time]] )
        {
            [trackPositionStr setStringValue: [currtime stringValue]];
        }
        
        if( ( lastpos == nil ) || ( [lastpos track] != [pos track] ) )
        {
            [trackTitle setStringValue: [[NSString alloc] 
                        initWithFormat: @"%d: %@", [track id], [track titleString]]];
        }
        
        // Set slider position
        [trackPosition setMinValue: 0.0];
        [trackPosition setMaxValue: [tracktime doubleValue]];
        [trackPosition setDoubleValue: [currtime doubleValue]];
        
        // Save the position for next time round
        [lastpos release];
        lastpos = [pos retain];
    }
    else
    {
        [trackPosition setMinValue: 0.0];
        [trackPosition setMaxValue: 1.0];
        [trackPosition setDoubleValue: 0.0];
        [trackPosition setEnabled: NO];
        
        [trackTitle setStringValue: @"No disc"];
        [trackPositionStr setStringValue: @""];
        
    }
}

@end
