//
//  CSAnimationItem.m
//  CocoaSplit
//
//  Created by Zakk on 3/20/15.
//  Copyright (c) 2015 Zakk. All rights reserved.
//

#import "CSAnimationItem.h"
#import "SourceLayout.h"

@implementation CSAnimationItem


-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [self removeDetachedInputs];
    
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.module_name forKey:@"module_name"];
    [aCoder encodeObject:self.inputs forKey:@"inputs"];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init])
    {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.module_name = [aDecoder decodeObjectForKey:@"module_name"];
        self.inputs = [aDecoder decodeObjectForKey:@"inputs"];
    }
    
    return self;
}


-(instancetype)copyWithZone:(NSZone *)zone
{
    CSAnimationItem *newItem = [[CSAnimationItem allocWithZone:zone] init];
    newItem.module_name = self.module_name;
    newItem.inputs = self.inputs;
    newItem.name = self.name;
    return newItem;
}


-(void)removeDetachedInputs
{
    for (NSMutableDictionary *inp in self.inputs)
    {
        NSString *inputType = inp[@"type"];
        if ([inputType isEqualToString:@"input"])
        {
            InputSource *inpsrc = inp[@"value"];
            if (!inpsrc || [inpsrc isEqual:[NSNull null]])
            {
                continue;
            }
            
            if (!inpsrc.sourceLayout || ![inpsrc.sourceLayout containsInput:inpsrc])
            {                
                inp[@"value"] = [NSNull null];
            }
        }
    }
}


-(void)purgeInputSource:(InputSource *)src
{
    for (NSMutableDictionary *inp in self.inputs)
    {
        NSString *inputType = inp[@"type"];
        if ([inputType isEqualToString:@"input"])
        {
            InputSource *inpsrc = inp[@"value"];
            if (inpsrc == src)
            {
                inp[@"value"] = [NSNull null];
            }
        }
    }
}


-(instancetype)initWithDictionary:(NSDictionary *)dict moduleName:(NSString *)moduleName
{
    if (self = [super init])
    {
        
        self.module_name = moduleName;
        
        self.name = [dict objectForKey:@"name"];
        NSArray *inputNames = [dict objectForKey:@"inputs"];
        NSArray *paramNames = [dict objectForKey:@"params"];
    
        self.inputs = [NSMutableArray array];
        
        for (NSString *iname in inputNames)
        {
            NSMutableDictionary *inputDict = [NSMutableDictionary dictionary];
            inputDict[@"type"] = @"input";
            inputDict[@"label"] = iname;
            inputDict[@"value"] = [NSNull null];
            
            [self.inputs addObject:inputDict];
        }
        
        for (NSString *iname in paramNames)
        {
            NSMutableDictionary *inputDict = [NSMutableDictionary dictionary];
            inputDict[@"type"] = @"param";
            inputDict[@"label"] = iname;
            inputDict[@"value"] = [NSNull null];
            [self.inputs addObject:inputDict];
        }
        
        NSMutableDictionary *liveDict = [NSMutableDictionary dictionary];
        liveDict[@"type"] = @"bool";
        liveDict[@"label"] = @"onLive";
        liveDict[@"value"] = @NO;
        
        [self.inputs addObject:liveDict];
    }
    
    return self;
}

-(bool)onLive
{
    bool ret = NO;
    for (NSDictionary *input in self.inputs)
    {
        if ([input[@"label"] isEqualToString:@"onLive"])
        {
            ret = [input[@"value"] boolValue];
            
            if (ret)
            {
                break;
            }
        }
    }
    return ret;
}




@end
