//
//  JDJSONModel.h
//  kneet
//
//  Created by Jason Lee on 2/9/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"
#import "LoginModel.h"
#import "CypherUtil.h"

@implementation HomeGroupModel

- (CLLocationCoordinate2D)coordinate {
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(0, 0);
    if (_locationCoordinate && ![_locationCoordinate isEmptyString]) {

        NSArray *geos = [_locationCoordinate componentsSeparatedByString:@","];
        coordinate.latitude = [geos[0] doubleValue];
        coordinate.longitude = [geos[1] doubleValue];
    }
    return coordinate;
}

- (NSInteger)level {

    _level = 0;
    if ([_gradeCode isEqualToString:KNEET_MEMBER_MASTER]) {
        _level = 90;
    } else if ([_gradeCode isEqualToString:KNEET_MEMBER_POWER]) {
        _level = 80;
    } else if ([_gradeCode isEqualToString:KNEET_MEMBER_SIMPLE]) {
        _level = 10;
    }
    return _level;
}

@end

@implementation HomeGroupListModel

@end

@implementation InvitationModel

@end

@implementation HomeMemberModel

@end

@implementation HomeMemberListModel

@end

@implementation MemberIconModel

@end

@implementation MemberIconListModel

@end

@implementation MobileDeviceListModel

@end

@implementation LoginModel

@synthesize memberId = _memberId, mobileDeviceId = _mobileDeviceId, mobileDeviceAuthorization = _mobileDeviceAuthorization, homegrpId = _homegrpId, newEmailId = _newEmailId, modeChange = _modeChange;


//+ (JSONKeyMapper *)keyMapper {
//
//    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
//
//    //Self
//    [dictionary setValue:@"lang" forKey:@"lang"];
//    [dictionary setValue:@"emailId" forKey:@"email_id"];
//    [dictionary setValue:@"authToken" forKey:@"auth_token"];
//
//    [dictionary setValue:@"lang" forKey:@"lang"];
//    [dictionary setValue:@"emailId" forKey:@"email_id"];
//    [dictionary setValue:@"authToken" forKey:@"auth_token"];
//    [dictionary setValue:@"member" forKey:@"member"];
//    [dictionary setValue:@"timezoneId" forKey:@"timezone_id"];
//    [dictionary setValue:@"timezone" forKey:@"timezone"];
//    [dictionary setValue:@"countryCode" forKey:@"country_code"];
//    [dictionary setValue:@"memberId" forKey:@"member_id"];
//    [dictionary setValue:@"serviceId" forKey:@"service_id"];
//    [dictionary setValue:@"imageFileName" forKey:@"image_file_name"];
//    [dictionary setValue:@"nickname" forKey:@"nickname"];
//    [dictionary setValue:@"key" forKey:@"key"];
//    [dictionary setValue:@"homegrpId" forKey:@"homegrp_id"];
//    [dictionary setValue:@"homegrpList" forKey:@"homegrp_list"];
//
//    return [[JSONKeyMapper alloc] initWithDictionary:dictionary];
//}

- (void)setMemberId:(NSString *)memberId {
    _memberId = memberId;

    //사용자 아이디 리스트
    NSMutableArray *userList = [[JDFacade facade] objectForKeyFromUserDefaults:USER_DEF_USERID_LIST];

    BOOL isExist = NO;
    if (!userList) {
        userList = [[NSMutableArray alloc] init];

    } else {
        for (NSString *memberId in userList) {
            isExist = [_memberId isEqualToString:memberId];
            if (isExist) {
                break;
            }
        }
    }

    if (!isExist) {//기존에 없으면 추가함.
        [userList addObject:_memberId];
    }

    [[JDFacade facade] storeObjectToUserDefaults:userList forKey:USER_DEF_USERID_LIST];
}

- (NSString *)emailId {
    if (!_emailId || [_emailId isEmptyString]) {
        _emailId = [JDFacade facade].tmpEmailId;
    }
    return _emailId;
}

#pragma mark - HomeGrp
- (NSString *)homegrpId {
    if (!_homegrpId) {
        _homegrpId = [self.localStorage objectForKey:USER_DEF_HOMEGRP_ID];
    }
    return _homegrpId ? _homegrpId : nil;
}

- (void)setHomegrpId:(NSString *)homegrpId {
    _homegrpId = homegrpId;

    //로컬 저장소에 저장
    [self.localStorage setObject:_homegrpId forKey:USER_DEF_HOMEGRP_ID];
    [self synchronizeLocalStorage];
}


- (NSString *)newEmailId {
    if (!_newEmailId) {
        _newEmailId = [self.localStorage objectForKey:USER_DEF_NEW_EMAIL];
    }
    return _newEmailId ? _newEmailId : nil;
}

- (void)setNewEmailId:(NSString *)newEmailId {
    _newEmailId = newEmailId;

    //로컬 저장소에 저장
    [self.localStorage setObject:_newEmailId forKey:USER_DEF_NEW_EMAIL];
    [self synchronizeLocalStorage];
}

- (NSString *)modeChange {
    if (!_modeChange) {
        _modeChange = [self.localStorage objectForKey:USER_DEF_MODE_CHANGE];
    }
    return _modeChange ? _modeChange : nil;
}

- (void)setModeChange:(NSString *)modeChange {
    _modeChange = modeChange;

    //로컬 저장소에 저장
    [self.localStorage setObject:_modeChange forKey:USER_DEF_MODE_CHANGE];
    [self synchronizeLocalStorage];
}



#pragma mark - about Location Settings
//로컬 저장소
- (NSMutableDictionary *)localStorage {
    if (!self.memberId)
        return nil;

    if (!_localStorage) {
        NSString *encryptedKey = [CypherUtil AES128Encrypt:self.memberId WithKey:self.memberId];
        _localStorage = [[JDFacade facade] objectForKeyFromUserDefaults:encryptedKey];

        if (!_localStorage) {//저장된 정보가 없을 경우,
            _localStorage = [[NSMutableDictionary alloc] init];
        }
    }
    return _localStorage;
}

- (void)synchronizeLocalStorage {

    if (!self.memberId)
        return;

    NSString *encryptedKey = [CypherUtil AES128Encrypt:self.memberId WithKey:self.memberId];
    [[JDFacade facade] storeObjectToUserDefaults:self.localStorage forKey:encryptedKey];
}


- (void)addHomegrp:(HomeGroupModel *)homegrp {
    if (!self.homegrpList) {
        self.homegrpList = (NSMutableArray<HomeGroupModel> *)[[NSMutableArray alloc] init];
    }

    for (HomeGroupModel *lhomegrp in self.homegrpList) {
        [[JDFacade facade] setRadioButtonStatus:@NO object:lhomegrp];
    }

    [self.homegrpList insertObject:homegrp atIndex:0];

    [JDFacade facade].loginHomeGroup = homegrp;
}
@end
