//
//  SortDictionary.m
//  JasonDevelop
//
//  Created by Hs Lee on 12. 3. 28..
//  Copyright (c) jasondevelop. All rights reserved.
//

#import "SortDictionary.h"

@implementation SortDictionary 


- (id)init {
    if (self = [super init]) {
        _keys = [[NSMutableArray alloc] init];
        _values = [[NSMutableArray alloc] init];        
    }
    
    return self;
}

- (id)initWithDictionary:(NSDictionary *)dict {
    
    if (self = [self init]) {
        NSArray *sortedArray = [dict.allKeys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            NSInteger num1 = [obj1 intValue];
            NSInteger num2 = [obj2 intValue];
            
            if (num1 < num2)
                return NSOrderedAscending;
            else if (num1 > num2)
                return NSOrderedDescending;
            else
                return NSOrderedSame;
        }];
                                
        for (int i = 0; i < sortedArray.count; i++) {
            [_keys addObject:[dict objectForKey:sortedArray[i]]];
            [_values addObject:sortedArray[i]];
        }
    }
    return self;
}


- (NSMutableArray *)allKeys {
    return _keys;
}

- (void)setAllKeys:(NSArray *)keys {
    _keys = [NSMutableArray arrayWithArray:keys];
}


- (NSMutableArray *)allValues {
    return _values;
}

- (void)setAllValues:(NSArray *)values {
    _values = [NSMutableArray arrayWithArray:values];
}

- (void)setObject:(id)object forKey:(id)key {
    
    NSInteger i = 0;
    for (id nkey in _keys) {
        
        if ([key isEqualToString:nkey]) {
            [_values replaceObjectAtIndex:i withObject:object];
            return;
        }
        i++;
    }
    
    [_keys addObject:key];
    [_values addObject:object];
}

- (id)keyAtIndex:(NSInteger)index {
    return [_keys objectAtIndex:index];
}

- (id)objectForKey:(id)key {
    
    NSInteger idx = -1;
    NSInteger i = 0;
    for (id nkey in _keys) {
        
        if ([key isEqualToString:nkey]) {
            idx = i; 
            break;
        }
        
        i++;
    }
    
    return idx > -1 ? [_values objectAtIndex:idx] : nil;
}

- (id)objectAtIndex:(NSInteger)index {
    return [self objectForKey:[self keyAtIndex:index]];
}

- (NSInteger)count {
    return (NSInteger)[_keys count];
}

- (NSInteger)indexForObject:(id)object {
    
    NSInteger idx = -1;
    NSInteger i = 0;
    for (id nObject in _values) {
        
        if ([object isEqual:nObject]) {
            idx = i;
            break;
        }
        
        i++;
    }
    
    return idx;
}

//해당 오브젝트를 해당 위치로 옮김.
- (void)setIndex:(NSInteger)index object:(id)object {
    id mKey = nil;
    NSInteger findex = [self indexForObject:object];
    if (findex > -1) {
         mKey = [self keyAtIndex:findex];
        
        [_keys removeObject:mKey];
        [_values removeObject:object];
    }
    
    [_keys insertObject:mKey atIndex:index];
    [_values insertObject:object atIndex:index];
}

- (void)removeAllObjects {
    [_keys removeAllObjects];
    [_values removeAllObjects];
}


@end
