//
//  DeleteAccountPopupView.m
//  kneet
//
//  Created by Jason Lee on 5/12/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"

#import "RequestHandler.h"
#import "JDJSONModel.h"

#import "CustomLabel.h"

#import "DeleteAccountPopupView.h"

@interface DeleteAccountPopupView () {

}

@end

@implementation DeleteAccountPopupView

- (id)initFromNib {

    for (UIView *view in [CommonUtil nibViews:@"DeleteAccountPopupView"]) {
        if ([view isKindOfClass:[DeleteAccountPopupView class]]) {
            self = (DeleteAccountPopupView *)view;

            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;
            self.lblTitle.text = NSLocalizedString(@"계정 삭제",nil);

            [_lblWarning setUnderLine:_lblWarning.text];
            
            //Localization
            _lblDesc1.text = NSLocalizedString(@"계정을 삭제한 즉시\n모든 장치와 규칙이 사라집니다", @"계정을 삭제한 즉시\n모든 장치와 규칙이 사라집니다");
            _lblDesc2.text = NSLocalizedString(@"삭제 후에는 복구할 수 없으며\n새로운 계정을 만들고\n장치를 등록해야 합니다", @"삭제 후에는 복구할 수 없으며\n새로운 계정을 만들고\n장치를 등록해야 합니다");
            _lblDesc3.text = NSLocalizedString(@"신중하게 결정해주세요", @"신중하게 결정해주세요");
            
            //Localization
            [self.btnConfirm setTitle:NSLocalizedString(@"계정삭제", @"계정삭제") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
        }
    }

    return self;
}

- (void)didMoveToSuperview {

}

//계정 삭제
- (void)requestDeleteAccount {

    NSString *path = [NSString stringWithFormat:API_DELETE_ACCOUNT, [JDFacade facade].loginUser.memberId];

    [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[JDJSONModel class] showLoadingView:YES completion:^(id responseObject) {

        //자동 로그인 설정 - 취소
        [[JDFacade facade] storeObjectToUserDefaults:@(NO) forKey:USDEF_APP_AUTO_LOGIN];
        [[JDFacade facade] removeObjectAtKeychain:USDEF_SESSION_AUTOTOKEN];
        
        [JDFacade facade].loginUser = nil;
        
        UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"GoodbyeViewController" storyboardName:@"Settings"];
        [[JDFacade facade] presentViewControllerByPush:vc];

        [super btnConfirmTouched:nil];
        
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}

- (void)btnConfirmTouched:(id)sender {

    [self requestDeleteAccount];
}

@end
