//
//  MIKMIDIOutputPort.m
//  MIDI Testbed
//
//  Created by Andrew Madsen on 3/8/13.
//  Copyright (c) 2013 Mixed In Key. All rights reserved.
//

#import "MIKMIDIPort_SubclassMethods.h"
#import "MIKMIDIOutputPort.h"
#import "MIKMIDIDestinationEndpoint.h"
#import "MIKMIDICommand.h"

#if !__has_feature(objc_arc)
#error MIKMIDIOutputPort.m must be compiled with ARC. Either turn on ARC for the project or set the -fobjc-arc flag for MIKMIDIOutputPort.m in the Build Phases for this target
#endif

@implementation MIKMIDIOutputPort

- (id)initWithClient:(MIDIClientRef)clientRef name:(NSString *)name
{
	self = [super initWithClient:clientRef name:name];
	if (self) {
		name = [name length] ? name : @"Input port";
		MIDIPortRef port;
		OSStatus error = MIDIOutputPortCreate(clientRef,
											  (__bridge CFStringRef)name,
											  &port);
		if (error != noErr) { self = nil; return nil; }
		self.portRef = port; // MIKMIDIPort will take care of disposing of the port when needed
	}
	return self;
}

- (BOOL)sendCommands:(NSArray *)commands toDestination:(MIKMIDIDestinationEndpoint *)destination error:(NSError **)error;
{
	if (![commands count] || !destination) return NO;
	
	error = error ? error : &(NSError *__autoreleasing){ nil };

	MIDIPacketList *packetList;
	if (!MIKCreateMIDIPacketListFromCommands(&packetList, commands)) return NO;
	
	OSStatus err = MIDISend(self.portRef, destination.objectRef, packetList);
	free(packetList);
	if (err != noErr) {
		*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
		return NO;
	}
	
	return YES;
}

@end
