//
//  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);
            
            //Localization

            [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            
            [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            
            [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            //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];
    NSString *path = [[JDFacade facade] getUrlWithCustGroupIDAndMemberID:API_DELETE_ACCOUNT];
    
    [[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:@"LoginViewController" storyboardName:@"Main"];
        [[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
