/*****************************************************************************
 * ConvertAndSave.h: MacOS X interface module
 *****************************************************************************
 * Copyright (C) 2012 Felix Paul Kühne
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
 *
 * 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.
 *****************************************************************************/

#import "ConvertAndSave.h"
#import "intf.h"
#import <vlc_common.h>
#import <vlc_url.h>

/* mini doc:
 * the used NSMatrix includes a bunch of cells referenced most easily by tags. There you go: */
#define MPEGTS 0
#define WEBM 1
#define OGG 2
#define MP4 3
#define MPEGPS 4
#define MJPEG 5
#define WAV 6
#define FLV 7
#define MPEG1 8
#define MKV 9
#define RAW 10
#define AVI 11
#define ASF 12
/* 13-15 are present, but not set */

@implementation VLCConvertAndSave

@synthesize MRL=_MRL, outputDestination=_outputDestination, profileNames=_profileNames, profileValueList=_profileValueList, currentProfile=_currentProfile;

static VLCConvertAndSave *_o_sharedInstance = nil;

#pragma mark -
#pragma mark Initialization

+ (void)initialize{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    /* We are using the same format as the Qt4 intf here:
     * Container(string), transcode video(bool), transcode audio(bool),
     * use subtitles(bool), video codec(string), video bitrate(integer),
     * scale(float), fps(float), width(integer, height(integer),
     * audio codec(string), audio bitrate(integer), channels(integer),
     * samplerate(integer), subtitle codec(string), subtitle overlay(bool) */
    NSArray * defaultProfiles = [[NSArray alloc] initWithObjects:
                                 @"mp4;1;1;0;h264;0;0;0;0;0;mpga;128;2;44100;0;1",
                                 @"webm;1;1;0;VP80;2000;0;0;0;0;vorb;128;2;44100;0;1",
                                 @"ts;1;1;0;h264;800;1;0;0;0;mpga;128;2;44100;0;0",
                                 @"ts;1;1;0;drac;800;1;0;0;0;mpga;128;2;44100;0;0",
                                 @"ogg;1;1;0;theo;800;1;0;0;0;vorb;128;2;44100;0;0",
                                 @"ogg;1;1;0;theo;800;1;0;0;0;flac;128;2;44100;0;0",
                                 @"ts;1;1;0;mp2v;800;1;0;0;0;mpga;128;2;44100;0;0",
                                 @"asf;1;1;0;WMV2;800;1;0;0;0;wma2;128;2;44100;0;0",
                                 @"asf;1;1;0;DIV3;800;1;0;0;0;mp3;128;2;44100;0;0",
                                 @"ogg;1;1;0;none;800;1;0;0;0;vorb;128;2;44100;none;0",
                                 @"raw;1;1;0;none;800;1;0;0;0;mp3;128;2;44100;none;0",
                                 @"mp4;1;1;0;none;800;1;0;0;0;mpga;128;2;44100;none;0",
                                 @"raw;1;1;0;none;800;1;0;0;0;flac;128;2;44100;none;0",
                                 @"wav;1;1;0;none;800;1;0;0;0;s16l;128;2;44100;none;0", nil];

    NSArray * defaultProfileNames = [[NSArray alloc] initWithObjects:
                                     @"Video - H.264 + MP3 (MP4)",
                                     @"Video - VP80 + Vorbis (Webm)",
                                     @"Video - H.264 + MP3 (TS)",
                                     @"Video - Dirac + MP3 (TS)",
                                     @"Video - Theora + Vorbis (OGG)",
                                     @"Video - Theora + Flac (OGG)",
                                     @"Video - MPEG-2 + MPGA (TS)",
                                     @"Video - WMV + WMA (ASF)",
                                     @"Video - DIV3 + MP3 (ASF)",
                                     @"Audio - Vorbis (OGG)",
                                     @"Audio - MP3",
                                     @"Audio - MP3 (MP4)",
                                     @"Audio - FLAC",
                                     @"Audio - CD",
                                     nil];

    NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:defaultProfiles, @"CASProfiles", defaultProfileNames, @"CASProfileNames", nil];

    [defaults registerDefaults:appDefaults];
    [defaultProfiles release];
}

+ (VLCConvertAndSave *)sharedInstance
{
    return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
}

- (id)init
{
    if (_o_sharedInstance) {
        [self dealloc];
    } else {
        _o_sharedInstance = [super init];
    }

    return _o_sharedInstance;
}

- (void)dealloc
{
    if (_MRL)
        [_MRL release];
    if (_outputDestination)
        [_outputDestination release];
    if (_currentProfile)
        [_currentProfile release];

    [_profileNames release];
    [_profileValueList release];
    [_videoCodecs release];
    [_audioCodecs release];
    [_subsCodecs release];

    [super dealloc];
}

- (void)awakeFromNib
{
    [_window setTitle: _NS("Convert & Save")];
    [_cancel_btn setTitle: _NS("Cancel")];
    [_ok_btn setTitle: _NS("Save")];
    [_drop_lbl setStringValue: _NS("Drop Media here")];
    [_drop_btn setTitle: _NS("Open Media...")];
    [_profile_lbl setStringValue: _NS("Choose Profile")];
    [_profile_btn setTitle: _NS("Customize")];
    [_destination_lbl setStringValue: _NS("Choose Destination")];
    [_destination_filename_stub_lbl setStringValue: _NS("Choose an output location")];
    [_destination_filename_lbl setHidden: YES];
    [_customize_ok_btn setTitle: _NS("Apply")];
    [_customize_cancel_btn setTitle: _NS("Cancel")];
    [[_customize_tabview tabViewItemAtIndex:0] setLabel: _NS("Encapsulation")];
    [[_customize_tabview tabViewItemAtIndex:1] setLabel: _NS("Video codec")];
    [[_customize_tabview tabViewItemAtIndex:2] setLabel: _NS("Audio codec")];
    [[_customize_tabview tabViewItemAtIndex:3] setLabel: _NS("Subtitles")];
    [_customize_tabview selectTabViewItemAtIndex: 0];
    [_customize_vid_ckb setTitle: _NS("Video")];
    [_customize_vid_keep_ckb setTitle: _NS("Keep original video track")];
    [_customize_vid_codec_lbl setStringValue: _NS("Codec")];
    [_customize_vid_bitrate_lbl setStringValue: _NS("Bitrate")];
    [_customize_vid_framerate_lbl setStringValue: _NS("Frame Rate")];
    [_customize_vid_res_box setTitle: _NS("Resolution")];
    [_customize_vid_res_lbl setStringValue: _NS("You just need to fill one of the three following parameters, VLC will autodetect the other using the original aspect ratio")];
    [_customize_vid_width_lbl setStringValue: _NS("Width")];
    [_customize_vid_height_lbl setStringValue: _NS("Height")];
    [_customize_vid_scale_lbl setStringValue: _NS("Scale")];
    [_customize_aud_ckb setTitle: _NS("Audio")];
    [_customize_aud_keep_ckb setTitle: _NS("Keep original audio track")];
    [_customize_aud_codec_lbl setStringValue: _NS("Codec")];
    [_customize_aud_bitrate_lbl setStringValue: _NS("Bitrate")];
    [_customize_aud_channels_lbl setStringValue: _NS("Channels")];
    [_customize_aud_samplerate_lbl setStringValue: _NS("Sample Rate")];
    [_customize_subs_ckb setTitle: _NS("Subtitles")];
    [_customize_subs_overlay_ckb setTitle: _NS("Overlay subtitles on the video")];

    /* there is no way to hide single cells, so replace the existing ones with empty cells.. */
    id blankCell = [[[NSCell alloc] init] autorelease];
    [blankCell setEnabled:NO];
    [_customize_encap_matrix putCell:blankCell atRow:3 column:1];
    [_customize_encap_matrix putCell:blankCell atRow:3 column:2];
    [_customize_encap_matrix putCell:blankCell atRow:3 column:3];

    /* fetch profiles from defaults */
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    _profileValueList = [[defaults arrayForKey:@"CASProfiles"] retain];
    _profileNames = [[defaults arrayForKey:@"CASProfileNames"] retain];

    [_profile_pop removeAllItems];
    [_profile_pop addItemsWithTitles: _profileNames];

    _videoCodecs = [[NSArray alloc] initWithObjects:
                    [NSArray arrayWithObjects:@"MPEG-1", @"MPEG-2", @"MPEG-4", @"DIVX 1", @"DIVX 2", @"DIVX 3", @"H.263", @"H.264", @"VP8", @"WMV1", @"WMV2", @"M-JPEG", @"Theora", @"Dirac", nil],
                    [NSArray arrayWithObjects:@"mpgv", @"mp2v", @"mp4v", @"DIV1", @"DIV2", @"DIV3", @"H263", @"h264", @"VP80", @"WMV1", @"WMV2", @"MJPG", @"theo", @"drac", nil],
                    nil];
    _audioCodecs = [[NSArray alloc] initWithObjects:
                    [NSArray arrayWithObjects:@"MPEG Audio", @"MP3", @"MPEG 4 Audio (AAC)", @"A52/AC-3", @"Vorbis", @"Flac", @"Speex", @"WAV", @"WMA2", nil],
                    [NSArray arrayWithObjects:@"mpga", @"mp3", @"mp4a", @"a52", @"vorb", @"flac", @"spx", @"s16l", @"wma2", nil],
                    nil];
    _subsCodecs = [[NSArray alloc] initWithObjects:
                   [NSArray arrayWithObjects:@"DVB subtitle", @"T.140", nil],
                   [NSArray arrayWithObjects:@"dvbs", @"t140", nil],
                   nil];

    [_customize_vid_codec_pop removeAllItems];
    [_customize_vid_scale_pop removeAllItems];
    [_customize_aud_codec_pop removeAllItems];
    [_customize_aud_samplerate_pop removeAllItems];
    [_customize_subs_pop removeAllItems];

    [_customize_vid_codec_pop addItemsWithTitles:[_videoCodecs objectAtIndex:0]];
    [_customize_aud_codec_pop addItemsWithTitles:[_audioCodecs objectAtIndex:0]];
    [_customize_subs_pop addItemsWithTitles:[_subsCodecs objectAtIndex:0]];

    [_customize_aud_samplerate_pop addItemWithTitle:@"8000"];
    [_customize_aud_samplerate_pop addItemWithTitle:@"11025"];
    [_customize_aud_samplerate_pop addItemWithTitle:@"22050"];
    [_customize_aud_samplerate_pop addItemWithTitle:@"44100"];
    [_customize_aud_samplerate_pop addItemWithTitle:@"48000"];

    [_customize_vid_scale_pop addItemWithTitle:@"1"];
    [_customize_vid_scale_pop addItemWithTitle:@"0.25"];
    [_customize_vid_scale_pop addItemWithTitle:@"0.5"];
    [_customize_vid_scale_pop addItemWithTitle:@"0.75"];
    [_customize_vid_scale_pop addItemWithTitle:@"1.25"];
    [_customize_vid_scale_pop addItemWithTitle:@"1.5"];
    [_customize_vid_scale_pop addItemWithTitle:@"1.75"];
    [_customize_vid_scale_pop addItemWithTitle:@"2"];
}

# pragma mark -
# pragma mark Code to Communicate with other objects

- (void)toggleWindow
{
    [_window makeKeyAndOrderFront: nil];
}

# pragma mark -
# pragma mark User Interaction

- (IBAction)windowButtonAction:(id)sender
{
}

- (IBAction)openMedia:(id)sender
{
}

- (IBAction)profileSelection:(id)sender
{
    NSInteger index = [sender indexOfSelectedItem];
    if (index != ([sender numberOfItems] - 1))
    {
        if (_currentProfile)
            [_currentProfile release];
        _currentProfile = [[NSMutableArray alloc] initWithArray: [[_profileValueList objectAtIndex:index] componentsSeparatedByString:@";"]];
    }
}

- (IBAction)customizeProfile:(id)sender
{
    [self resetCustomizationSheetBasedOnProfile:[_profileValueList objectAtIndex:[_profile_pop indexOfSelectedItem]]];
    [NSApp beginSheet:_customize_panel modalForWindow:_window modalDelegate:self didEndSelector:NULL contextInfo:nil];
}

- (IBAction)closeCustomizationSheet:(id)sender
{
    // sender == _customize_ok_btn ?
    [_customize_panel orderOut:sender];
    [NSApp endSheet: _customize_panel];
}

- (IBAction)chooseDestination:(id)sender
{
    NSSavePanel * saveFilePanel = [[NSSavePanel alloc] init];
    [saveFilePanel setCanSelectHiddenExtension: YES];
    [saveFilePanel setCanCreateDirectories: YES];
    [saveFilePanel beginSheetForDirectory:nil file:nil modalForWindow:_window modalDelegate:self didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:nil];
}

- (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
    if (returnCode == NSOKButton) {
        _outputDestination = [[sheet URL] path];
        [_destination_filename_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath:_outputDestination]];
        [[_destination_filename_stub_lbl animator] setHidden: YES];
        [[_destination_filename_lbl animator] setHidden: NO];
    } else {
        _outputDestination = @"";
        [[_destination_filename_lbl animator] setHidden: YES];
        [[_destination_filename_stub_lbl animator] setHidden: NO];
    }
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *paste = [sender draggingPasteboard];
    NSArray *types = [NSArray arrayWithObject: NSFilenamesPboardType];
    NSString *desired_type = [paste availableTypeFromArray: types];
    NSData *carried_data = [paste dataForType: desired_type];

    if( carried_data ) {
        if( [desired_type isEqualToString:NSFilenamesPboardType] ) {
            NSArray *values = [[paste propertyListForType: NSFilenamesPboardType] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

            if ([values count] > 0) {
                [self setMRL: [NSString stringWithUTF8String:make_URI([[values objectAtIndex:0] UTF8String], NULL)]];
                [self updateDropView];
                return YES;
            }
        }
    }
    return NO;
}

# pragma mark -
# pragma mark Private Functionality
- (void)updateDropView
{
    if ([_MRL length] > 0) {
        NSString * path = [[NSURL URLWithString:_MRL] path];
        [_dropin_media_lbl setStringValue: [[NSFileManager defaultManager] displayNameAtPath: path]];
        NSImage * image = [[NSWorkspace sharedWorkspace] iconForFile: path];
        [image setSize:NSMakeSize(64,64)];
        [_dropin_icon_view setImage: image];

        if (![_dropin_view superview]) {
            NSRect boxFrame = [_drop_box frame];
            NSRect subViewFrame = [_dropin_view frame];
            subViewFrame.origin.x = (boxFrame.size.width - subViewFrame.size.width) / 2;
            subViewFrame.origin.y = (boxFrame.size.height - subViewFrame.size.height) / 2;
            [_dropin_view setFrame: subViewFrame];
            [[_drop_image_view animator] setHidden: YES];
            [_drop_box performSelector:@selector(addSubview:) withObject:_dropin_view afterDelay:0.4];
        }
    } else {
        [_dropin_view removeFromSuperview];
        [[_drop_image_view animator] setHidden: NO];
    }
}

- (void)resetCustomizationSheetBasedOnProfile:(NSString *)profileString
{
    /* Container(string), transcode video(bool), transcode audio(bool),
    * use subtitles(bool), video codec(string), video bitrate(integer),
    * scale(float), fps(float), width(integer, height(integer),
    * audio codec(string), audio bitrate(integer), channels(integer),
    * samplerate(integer), subtitle codec(string), subtitle overlay(bool) */

    NSArray * components = [profileString componentsSeparatedByString:@";"];
    if ([components count] != 16) {
        msg_Err(VLCIntf, "CAS: the requested profile '%s' is invalid", [profileString UTF8String]);
        return;
    }

    [self selectCellByEncapsulationFormat:[components objectAtIndex:0]];
    [_customize_vid_ckb setState:[[components objectAtIndex:1] intValue]];
    [_customize_aud_ckb setState:[[components objectAtIndex:2] intValue]];
    [_customize_subs_ckb setState:[[components objectAtIndex:3] intValue]];
    [_customize_vid_bitrate_fld setStringValue:[components objectAtIndex:5]];
    [_customize_vid_scale_pop selectItemWithTitle:[components objectAtIndex:6]];
    [_customize_vid_framerate_fld setStringValue:[components objectAtIndex:7]];
    [_customize_vid_width_fld setStringValue:[components objectAtIndex:8]];
    [_customize_vid_height_fld setStringValue:[components objectAtIndex:9]];
    [_customize_aud_bitrate_fld setStringValue:[components objectAtIndex:11]];
    [_customize_aud_channels_fld setStringValue:[components objectAtIndex:12]];
    [_customize_aud_samplerate_pop selectItemWithTitle:[components objectAtIndex:13]];
    [_customize_subs_overlay_ckb setState:[[components objectAtIndex:15] intValue]];

    /* since there is no proper lookup mechanism in arrays, we need to implement a string specific one ourselves */
    NSArray * tempArray = [_videoCodecs objectAtIndex:1];
    NSUInteger count = [tempArray count];
    NSString * searchString = [components objectAtIndex:4];
    if ([searchString isEqualToString:@"none"] || [searchString isEqualToString:@"0"]) {
        [_customize_vid_codec_pop selectItemAtIndex:-1];
    } else {
        for (NSUInteger x = 0; x < count; x++) {
            if ([[tempArray objectAtIndex:x] isEqualToString: searchString]) {
                [_customize_vid_codec_pop selectItemAtIndex:x];
                break;
            }
        }
    }

    tempArray = [_audioCodecs objectAtIndex:1];
    count = [tempArray count];
    searchString = [components objectAtIndex:10];
    if ([searchString isEqualToString:@"none"] || [searchString isEqualToString:@"0"]) {
        [_customize_aud_codec_pop selectItemAtIndex:-1];
    } else {
        for (NSUInteger x = 0; x < count; x++) {
            if ([[tempArray objectAtIndex:x] isEqualToString: searchString]) {
                [_customize_aud_codec_pop selectItemAtIndex:x];
                break;
            }
        }
    }

    tempArray = [_subsCodecs objectAtIndex:1];
    count = [tempArray count];
    searchString = [components objectAtIndex:14];
    if ([searchString isEqualToString:@"none"] || [searchString isEqualToString:@"0"]) {
        [_customize_subs_pop selectItemAtIndex:-1];
    } else {
        for (NSUInteger x = 0; x < count; x++) {
            if ([[tempArray objectAtIndex:x] isEqualToString: searchString]) {
                [_customize_subs_pop selectItemAtIndex:x];
                break;
            }
        }
    }
}

- (void)selectCellByEncapsulationFormat:(NSString *)format
{
    if ([format isEqualToString:@"ts"])
        [_customize_encap_matrix selectCellWithTag:MPEGTS];
    else if([format isEqualToString:@"webm"])
        [_customize_encap_matrix selectCellWithTag:WEBM];
    else if([format isEqualToString:@"ogg"])
        [_customize_encap_matrix selectCellWithTag:OGG];
    else if([format isEqualToString:@"ogm"])
        [_customize_encap_matrix selectCellWithTag:OGG];
    else if([format isEqualToString:@"mp4"])
        [_customize_encap_matrix selectCellWithTag:MP4];
    else if([format isEqualToString:@"mov"])
        [_customize_encap_matrix selectCellWithTag:MP4];
    else if([format isEqualToString:@"ps"])
        [_customize_encap_matrix selectCellWithTag:MPEGPS];
    else if([format isEqualToString:@"mjpeg"])
        [_customize_encap_matrix selectCellWithTag:MJPEG];
    else if([format isEqualToString:@"wav"])
        [_customize_encap_matrix selectCellWithTag:WAV];
    else if([format isEqualToString:@"flv"])
        [_customize_encap_matrix selectCellWithTag:FLV];
    else if([format isEqualToString:@"mpg"])
        [_customize_encap_matrix selectCellWithTag:MPEG1];
    else if([format isEqualToString:@"mkv"])
        [_customize_encap_matrix selectCellWithTag:MKV];
    else if([format isEqualToString:@"raw"])
        [_customize_encap_matrix selectCellWithTag:RAW];
    else if([format isEqualToString:@"avi"])
        [_customize_encap_matrix selectCellWithTag:AVI];
    else if([format isEqualToString:@"asf"])
        [_customize_encap_matrix selectCellWithTag:ASF];
    else if([format isEqualToString:@"wmv"])
        [_customize_encap_matrix selectCellWithTag:ASF];
    else
        msg_Err(VLCIntf, "CAS: unknown encap format requested for customization");
}

@end


@implementation VLCDropEnabledBox

- (void)awakeFromNib
{
    [self registerForDraggedTypes:[NSArray arrayWithObject: NSFilenamesPboardType]];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
        return NSDragOperationGeneric;

    return NSDragOperationNone;
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    return [[VLCConvertAndSave sharedInstance] performDragOperation: sender];
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
    [self setNeedsDisplay:YES];
}

@end

@implementation VLCDropEnabledImageView

- (void)awakeFromNib
{
    [self registerForDraggedTypes:[NSArray arrayWithObject: NSFilenamesPboardType]];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
        return NSDragOperationGeneric;

    return NSDragOperationNone;
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    return [[VLCConvertAndSave sharedInstance] performDragOperation: sender];
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
    [self setNeedsDisplay:YES];
}

@end

@implementation VLCDropEnabledButton

- (void)awakeFromNib
{
    [self registerForDraggedTypes:[NSArray arrayWithObject: NSFilenamesPboardType]];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric)
        return NSDragOperationGeneric;

    return NSDragOperationNone;
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    return YES;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    return [[VLCConvertAndSave sharedInstance] performDragOperation: sender];
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
    [self setNeedsDisplay:YES];
}

@end
