/*****************************************************************************
 * PLItem.m: MacOS X interface module
 *****************************************************************************
 * Copyright (C) 2014 VLC authors and VideoLAN
 * $Id: f5c0f5f9b892ee6fe22da8dac9fb10d26f86766f $
 *
 * 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 "PLItem.h"

#include <vlc_playlist.h>
#include <vlc_input_item.h>

#pragma mark -
;
@implementation PLItem

@synthesize children=_children;
@synthesize plItemId=_playlistId;
@synthesize input=p_input;
@synthesize parent=_parent;

- (id)initWithPlaylistItem:(playlist_item_t *)p_item;
{
    self = [super init];
    if(self) {
        _playlistId = p_item->i_id;

        p_input = p_item->p_input;
        input_item_Hold(p_input);
        _children = [[NSMutableArray alloc] init];
    }

    return self;
}

- (void)dealloc
{
    input_item_Release(p_input);
    [_children release];
    [_parent release];

    [super dealloc];
}

// own hash and isEqual methods are important to retain expandable state
// when items are moved / recreated
- (NSUInteger)hash
{
    return (NSUInteger)[self plItemId];
}

- (BOOL)isEqual:(id)other
{
    if (other == self)
        return YES;
    if (!other || ![other isKindOfClass:[self class]])
        return NO;
    return [self plItemId] == [other plItemId];
}

- (BOOL)isLeaf
{
    return [_children count] == 0;
}

- (void)clear
{
    [_children removeAllObjects];
}

- (void)addChild:(PLItem *)item atPos:(int)pos
{
    [_children insertObject:item atIndex:pos];
    [item setParent: self];

}

- (void)deleteChild:(PLItem *)child
{
    [child setParent:nil];
    [_children removeObject:child];
}

@end
