//
//  PodEditController.m
//  CocoaPodUI
//
//  Created by Евгений Кратько on 19.03.14.
//  Copyright (c) 2014 akki. All rights reserved.
//
//Copyright (c) 2014 Yevgeniy Branitsky (Kratko)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

#import "PodEditController.h"
#import "PodItem.h"

@interface PodEditController ()

@end

@implementation PodEditController

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

- (void)setItem:(PodItem *)item
{
    if (_item) {
        @try {
            [_item removeObserver:self forKeyPath:@"version"];
        }
        @catch (NSException *exception) {
            DLog(@"%@", [exception reason]);
        }
    }
    _item = item;
    [self setNeedGit:(_item.gitURL != nil)];
    [self setNeedPath:(_item.path != nil)];
    [self setNeedPodspec:(_item.podspecURL != nil)];
    [self setNeedCommit:(_item.commit != nil)];
    [_item addObserver:self forKeyPath:@"version" options:NSKeyValueObservingOptionNew context:NULL];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"version"]) {
//        DLog(@"POD CHANGE VERSION %s", __PRETTY_FUNCTION__);
        if ([self.delegate respondsToSelector:@selector(didCompletePodEdition:)]) {
            [self.delegate didCompletePodEdition:self.item];
        }
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (IBAction)closeAction:(id)sender
{
    [[self window] close];
    [NSApp endSheet:[self window]];
}
@end
