//
//  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 InvitationModel

@end

@implementation InvitationListModel

@end

@implementation HomeMemberModel

@end

@implementation HomeMemberListModel

@end

@implementation MemberIconModel

@end

@implementation MemberIconListModel

@end

@implementation MobileDeviceListModel

@end

@implementation LoginModel

@synthesize memberId = _memberId, homegrpId = _homegrpId, newEmailId = _newEmailId, modeChange = _modeChange, noticeReadTime = _noticeReadTime, personalNoticeReadTime = _personalNoticeReadTime;


//+ (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:USDEF_APP_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:USDEF_APP_USERID_LIST];
}

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

//새 이메일 변경 시 - 체크를 위한 로컬 저장
- (NSString *)newEmailId {
    if (!_newEmailId) {
        _newEmailId = [self.localStorage objectForKey:USDEF_SESSION_NEW_EMAIL];
    }
    return _newEmailId ? _newEmailId : nil;
}

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

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

//모드 변경 저장
- (NSString *)modeChange {
    if (!_modeChange) {
        _modeChange = [self.localStorage objectForKey:USDEF_SESSION_MODE_CHANGE];
    }
    return _modeChange ? _modeChange : nil;
}

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

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

//세션 - 공지 읽음 날짜 확인
- (NSString *)noticeReadTime {
    if (!_noticeReadTime) {
        _noticeReadTime = [self.localStorage objectForKey:USDEF_SESSION_NOTICE_READ_DATE];
    }
    return _noticeReadTime ? _noticeReadTime : nil;
}

- (void)setNoticeReadTime:(NSString *)noticeReadTime {
    _noticeReadTime = noticeReadTime;
    
    //로컬 저장소에 저장
    [self.localStorage setObject:_noticeReadTime forKey:USDEF_SESSION_NOTICE_READ_DATE];
    [self synchronizeLocalStorage];
}


//세션 - 알림 읽음 날짜 확인
- (NSString *)personalNoticeReadTime {
    if (!_personalNoticeReadTime) {
        _personalNoticeReadTime = [self.localStorage objectForKey:USDEF_SESSION_PUSH_READ_DATE];
    }
    return _personalNoticeReadTime ? _personalNoticeReadTime : nil;
}

- (void)setPersonalNoticeReadTime:(NSString *)personalNoticeReadTime {
    _personalNoticeReadTime = personalNoticeReadTime;
    
    //로컬 저장소에 저장
    [self.localStorage setObject:_personalNoticeReadTime forKey:USDEF_SESSION_PUSH_READ_DATE];
    [self synchronizeLocalStorage];
}

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

- (BOOL)isHomehubOnline {
    return _homehubOnlineState && ![_homehubOnlineState isEmptyString] && [_homehubOnlineState isEqualToString:@"ON"];
}

- (BOOL)hasHomeHub {
    return _homehubDeviceId && ![_homehubDeviceId isEmptyString];
}


#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];
}



@end
