//
//  PopTableView.m
//  kneet
//
//  Created by Jason Lee on 4/27/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"
#import "CustomLabel.h"
#import "CustomRadioGroup.h"

#import "PopTableView.h"
#import "CustomImageView.h"
#import "ImageUtil.h"

#define kfPopTableViewCellHeight 100.0f

@interface PopRadioTableViewCell : UITableViewCell

@property (strong, nonatomic) CustomLabel *lblTitle;
@property (strong, nonatomic) CustomRadioButton *rdoSelect;

@end

@implementation PopRadioTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        CGFloat width = IPHONE_WIDTH - 20 - 20 - 25 - 10; //margin - margin - radiosize - padding
        self.lblTitle = [[CustomLabel alloc] initWithFrame:CGRectMake(20, 20, width, 60)];
        _lblTitle.numberOfLines = 0;
        _lblTitle.font = [UIFont systemFontOfSize:kUIFontSize02];
        _lblTitle.textColor = kUITextColor01;


        self.rdoSelect = [[CustomRadioButton alloc] initWithFrame:CGRectMake(width+10, 37.5, 25.0f, 25.0f) normalImage:[UIImage imageNamed:@"common_radiobox_default"] highlightImage:[UIImage imageNamed:@"common_radiobox_checked"]];

        [self addSubview:_lblTitle];
        [self addSubview:_rdoSelect];

        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}



@end

@interface PopTableView () <UITableViewDataSource, UITableViewDelegate> {
    UIImage *_bgCellImage;
    BOOL _isNotFirstLoading;
}

@end

@implementation PopTableView

- (id)initFromNib {

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

            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;

            self.lblTitle.text = NSLocalizedString(@"주소검색",nil);

            _tableView.dataSource = self;
            _tableView.delegate = self;

            _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            _tableView.backgroundColor = [UIColor clearColor];
            _tableView.scrollEnabled = YES;

            _tableView.tableFooterView = [[UIView alloc] init];

            _rgroup = [[CustomRadioReusableGroup alloc] init];
            _rgroup.tableView = _tableView;

            UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
            _bgCellImage = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
            
            //Localization
            [self.btnConfirm setTitle:NSLocalizedString(@"선택", @"선택") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
        }
    }

    return self;
}

- (void)didMoveToSuperview {

}

- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;

//    _tableViewHeightConstraint.constant = kfPopTableViewCellHeight * _dataArray.count;
    [_tableView reloadData];
}


#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";

    PopRadioTableViewCell *cell = (PopRadioTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[PopRadioTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *component = _dataArray[indexPath.row];

    NSString *addr = component[@"formatted_address"];
    addr = [addr stringByReplacingOccurrencesOfString:@"대한민국 " withString:ksEmptyString];

    cell.lblTitle.text = addr;
    cell.rdoSelect.value = component;

    if (indexPath.row == 0 && !_isNotFirstLoading) {
        _isNotFirstLoading = YES;
        cell.rdoSelect.checked = YES;
    } else {
        cell.rdoSelect.checked = [cell.rdoSelect getRadioStatusFromValue];
    }

    [_rgroup addRadioButton:cell.rdoSelect];

    cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    PopRadioTableViewCell *cell = (PopRadioTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    [_rgroup someRadioButtonTouched:cell.rdoSelect];

    [_tableView reloadData];
}


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
