//  ASIPithosObject.m
//  Based on ASICloudFilesObject.m
//
// Copyright 2011 GRNET S.A. All rights reserved.
//
// Redistribution and use in source and binary forms, with or
// without modification, are permitted provided that the following
// conditions are met:
// 
//   1. Redistributions of source code must retain the above
//      copyright notice, this list of conditions and the following
//      disclaimer.
// 
//   2. Redistributions in binary form must reproduce the above
//      copyright notice, this list of conditions and the following
//      disclaimer in the documentation and/or other materials
//      provided with the distribution.
// 
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// The views and conclusions contained in the software and
// documentation are those of the authors and should not be
// interpreted as representing official policies, either expressed
// or implied, of GRNET S.A.

#import "ASIPithosObject.h"
#import "ASIPithosSharingUser.h"

@implementation ASIPithosObject
@synthesize subdir;
@synthesize name, hash, objectHash, UUID, bytes, contentType, contentEncoding, contentDisposition, lastModified, 
version, versionTimestamp, modifiedBy, manifest, sharing, sharedBy, publicURI, metadata, allowedTo, 
data, permissions;

+ (id)object {
	ASIPithosObject *object = [[self alloc] init];
    object.subdir = NO;
	return object;
}

+ (id)subdirWithName:(NSString *)name {
	ASIPithosObject *object = [[self alloc] init];
    object.subdir = YES;
    object.name = name;
	return object;
}

- (NSString *)description {
    if (subdir)
        return [NSString stringWithFormat:@"subdir name: %@", name];
    return [NSString stringWithFormat:@"name: %@, hash: %@, objectHash: %@, UUID: %@, bytes: %lu, contentType: %@, contentEncoding: %@, contentDisposition: %@, lastModified: %@, version: %@, versionTimestamp: %@, modifiedBy: %@, manifest: %@, sharing: %@, sharedBy: %@, publicURI: %@, metadata: %@, allowedTo: %@", 
            name, hash, objectHash, UUID, bytes, contentType, contentEncoding, contentDisposition, lastModified, version, versionTimestamp, modifiedBy, manifest, sharing, sharedBy, publicURI, metadata, allowedTo];
}

- (void)setSharing:(NSString *)aSharing {
    if (!aSharing || ![aSharing isEqualToString:sharing]) {
        permissions = nil;
    }
    sharing = aSharing;
}

- (NSMutableArray *)permissions {
    if (!permissions) {
        NSMutableArray *newPermissions = [NSMutableArray array];
        if (self.sharing.length) {
            for (NSString *sharingList in [self.sharing componentsSeparatedByString:@";"]) {
                NSRange equalsRange = [sharingList rangeOfString:@"="];
                if (equalsRange.location != NSNotFound) {
                    NSString *sharingType = [sharingList substringToIndex:equalsRange.location];
                    if ([sharingType hasPrefix:@" "]) {
                        sharingType = [sharingType substringFromIndex:1];
                    }
                    for (NSString *sharingUser in [[sharingList substringFromIndex:(equalsRange.location + 1)] componentsSeparatedByString:@","]) {
                        [newPermissions addObject:[ASIPithosSharingUser sharingUserWithUser:sharingUser permission:sharingType]];
                    }
                }
            }
        }
        permissions = newPermissions;
    }
    return permissions;
}

- (void)setPermissions:(NSMutableArray *)aPermissions {
    if (!aPermissions || ![aPermissions isEqualToArray:permissions]) {
        if (aPermissions.count) {
            NSMutableDictionary *sharingListsDictionary = [NSMutableDictionary dictionary];
            for (ASIPithosSharingUser *sharingUser in aPermissions) {
                if (sharingUser.permission) {
                    NSMutableString *sharingList = [sharingListsDictionary objectForKey:sharingUser.permission];
                    if (!sharingList) {
                        sharingList = [NSMutableString stringWithFormat:@"%@=", sharingUser.permission];
                        [sharingListsDictionary setObject:sharingList forKey:sharingUser.permission];
                    }
                    if (sharingUser.name) {
                        if (sharingUser.group) {
                            [sharingList appendFormat:@"%@:%@,", sharingUser.name, sharingUser.group];
                        } else {
                            [sharingList appendFormat:@"%@,", sharingUser.name];
                        }
                    }
                }
            }
            NSMutableString *newSharing = [NSMutableString string];
            for (NSMutableString *sharingList in [sharingListsDictionary objectEnumerator]) {
                [newSharing appendFormat:@"%@;", [sharingList substringToIndex:(sharingList.length - 1)]];
            }
            self.sharing = [newSharing substringToIndex:(newSharing.length - 1)];
        } else {
            self.sharing = nil;
        }
    }
    permissions = aPermissions;
}

@end
