//
//  HomeMemberDetailViewController.m
//  kneet
//
//  Created by Jason Lee on 6/16/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"
#import "RequestHandler.h"
#import "LoginModel.h"

#import "CustomButton.h"
#import "CustomLabel.h"
#import "CustomRadioGroup.h"
#import "UIButton+WebCache.h"
#import "MemberRolePopupView.h"

#import "HomeMemberDetailViewController.h"
#import "HomeMemberViewController.h"



@interface HomeMemberDetailViewController () {

}
@end


#pragma mark - Class Definition

@implementation HomeMemberDetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {
    
    
}


- (void)setContents {
    _lblMemberRole.text = _member.gradeCodeName;
    
    if ([_member.gradeCode isEqualToString:KNEET_MEMBER_MASTER]) {
        _lblMemberRoleDesc.text = NSLocalizedString(@"홈을 관리하고 멤버를 초대할 수 있습니다", @"홈을 관리하고 멤버를 초대할 수 있습니다");
    } else if ([_member.gradeCode isEqualToString:KNEET_MEMBER_POWER]) {
        _lblMemberRoleDesc.text = NSLocalizedString(@"홈 셋팅을 제외한 모든 것을 할 수 있습니다", @"홈 셋팅을 제외한 모든 것을 할 수 있습니다");
    } else if ([_member.gradeCode isEqualToString:KNEET_MEMBER_SIMPLE]) {
        _lblMemberRoleDesc.text = NSLocalizedString(@"단순 사용만 가능합니다", @"단순 사용만 가능합니다");
    }
}

- (void)prepareViewDidLoad {
    
    [_btnMemberIcon sd_setImageWithURL:[NSURL URLWithString:_member.imageFileName] forState:UIControlStateNormal];
    
    [self setContents];
    
    if ([_member.invitation isEqualToString:@"11"]) {//초대완료
        _lblMessage.hidden = YES;
        _btnEdit.hidden = [_member.gradeCode isEqualToString:KNEET_MEMBER_MASTER];
        _btnAction.hidden = [_member.gradeCode isEqualToString:KNEET_MEMBER_MASTER];
        
        _constraintLblMessageHeight.constant = 0;
        _constraintLblMessageBottom.constant = 0;
        
        [_btnAction setTitle:NSLocalizedString(@"멤버 삭제", @"멤버 삭제") forState:UIControlStateNormal];
        
    } else {
        _btnEdit.hidden = YES;
        if ([_member.invitation isEqualToString:@"01"]) {//초대중
            _lblMessage.text = NSLocalizedString(@"참여 대기 중 입니다", @"참여 대기 중 입니다");
        
        } else if ([_member.invitation isEqualToString:@"23"]) {//초대거절
            _lblMessage.text = NSLocalizedString(@"상대방이 거절했습니다", @"상대방이 거절했습니다");
        }
        [_btnAction setTitle:NSLocalizedString(@"초대 취소", @"초대 취소") forState:UIControlStateNormal];
    }
}

#pragma mark - Main Logic

- (void)requestUpateMemberRole:(NSString *)newRole updateType:(NSString *)updateType {
    //parameters
//    NSDictionary *parameter = @{@"target_email": _member.targetEmail,
//                                @"grade_code": newRole};
//    
//    NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_MEMBER_UPDATE, updateType];
//    
//    [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
//       
//        _member.gradeCode = newRole;
//        _member.gradeCodeName = [_member.gradeCode isEqualToString:KNEET_MEMBER_POWER] ? NSLocalizedString(@"파워멤버", @"파워멤버") : NSLocalizedString(@"심플멤버", @"심플멤버");
//        
//        [self setContents];
//        
//        HomeMemberViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[HomeMemberViewController class]];
//        if (vc) {
//            if ([updateType isEqualToString:@"grade"]) {
////                [vc.tableView reloadData];
//            } else {
//                if ([updateType isEqualToString:@"ban"]) {
//                    [[JDFacade facade] toast:NSLocalizedString(@"멤버가 삭제되었습니다", @"멤버가 삭제되었습니다")];
//                } else if ([updateType isEqualToString:@"invite_cancel"]) {
//                    [[JDFacade facade] toast:NSLocalizedString(@"초대를 취소합니다", @"초대를 취소합니다")];
//                }
//                [vc refreshHomeMemberList];
//                [self.navigationController popViewControllerAnimated:YES];
//            }
//        }
//    
//    } failure:^(id errorObject) {
//        JDErrorModel *error = (JDErrorModel *)errorObject;
//        [[JDFacade facade] alert:error.errorMessage];
//    }];
}

#pragma mark - UI Events
- (IBAction)btnModifyTouched:(id)sender {
    
    MemberRolePopupView *popup = [[MemberRolePopupView alloc] initFromNib];
    popup.member = _member;
    [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {//OK
            if (![popup.rgroup.valueForChecked isEqualToString:_member.gradeCode]) {
                [self requestUpateMemberRole:popup.rgroup.valueForChecked updateType:@"grade"];
            }
        }
    }];
}

- (IBAction)btnActionTouched:(id)sender {
	if ([_member.invitation isEqualToString:@"11"]) {//멤버 삭제
        
        
        
        NSString *msg = [NSString stringWithFormat:NSLocalizedString(@"[%@]님을 현재 홈의 멤버에서\n삭제하시겠습니까?\n삭제된 멤버는 현재홈의 모든 기능을\n이용할 수 없게 됩니다", @"[%@]님을 현재 홈의 멤버에서\n삭제하시겠습니까?\n삭제된 멤버는 현재홈의 모든 기능을\n이용할 수 없게 됩니다"), _member.nickname];
        CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:msg delegate:nil OKButtonTitle:NSLocalizedString(@"확인", @"확인") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
        [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 0) {//OK
                [self requestUpateMemberRole:_member.gradeCode updateType:@"ban"];
            }
        }];
        
    } else {//초대 취소
        
        
        
        CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:NSLocalizedString(@"초대를 취소하시겠습니까?\n취소 시 목록에서 삭제됩니다", @"초대를 취소하시겠습니까?\n취소 시 목록에서 삭제됩니다") delegate:nil OKButtonTitle:NSLocalizedString(@"확인", @"확인") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
        [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 0) {//OK
                [self requestUpateMemberRole:_member.gradeCode updateType:@"invite_cancel"];
            }
        }];
    }
}



#pragma mark - MemoryWarning
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
