//
//  WeatherLocationPopupView.m
//  kneet2
//
//  Created by Jason Lee on 11/3/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"

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

#import "CustomLabel.h"
#import "CustomTableView.h"
#import "CustomButton.h"
#import "CustomRadioGroup.h"
#import "ImageUtil.h"

#import "WeatherLocationPopupView.h"

@implementation WeatherLocationPopupTableViewCell

@end

@interface WeatherLocationPopupView () <UITableViewDataSource, UITableViewDelegate> {
    
    CustomRadioReusableGroup *_rgroup;
    BOOL _isNotFirstLoading;
    UIImage *_bgCellImage1, *_bgCellImage2;
    
    NSMutableArray<ItemModel> *_checkedDevices;
}

@end

@implementation WeatherLocationPopupView

- (id)initFromNib {

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

            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;
            
            [self initTableViewAsDefaultStyle:_tableView];
            
            UINib *nib = [UINib nibWithNibName:@"WeatherLocationPopupTableViewCell" bundle:nil];
            [_tableView registerNib:nib forCellReuseIdentifier:@"LocationCellIdentifier"];
            
            //set radio group
            _rgroup = [[CustomRadioReusableGroup alloc] init];
            _rgroup.tableView = _tableView;
            
            //Localization
            [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
            
            UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 2);
            _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_01"]];
            _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"img_list_bg_02"]];
        }
    }

    return self;
}

- (void)initTableViewAsDefaultStyle:(CustomTableView *)tableView {
    tableView.dataSource = self;
    tableView.delegate = self;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor clearColor];
    
    tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
}

- (void)didMoveToSuperview {

}

- (CommonCode *)weatherLocation {
    return _rgroup.valueForChecked;
}

- (void)setLocations:(NSArray<CommonCode> *)locations {
    _locations = locations;
    
    _isNotFirstLoading = [_locations matchedObjectName:ksCustomRadioButtonStatus condition:YES] != nil;
}

#pragma mark - Main Logic


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

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 78.0f;
    
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CommonCode *code = _locations[indexPath.row];
    
    WeatherLocationPopupTableViewCell *tcell = (WeatherLocationPopupTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"LocationCellIdentifier"];
    tcell.lblCity.text = code.commonCodeName;
    tcell.rdoSelect.value = code;
    
    if (indexPath.row == 0 && !_isNotFirstLoading) {
        _isNotFirstLoading = YES;
        tcell.rdoSelect.checked = YES;
    } else {
        tcell.rdoSelect.checked = [tcell.rdoSelect getRadioStatusFromValue];
    }
    
    [_rgroup addRadioButton:tcell.rdoSelect];
    
    //set background image
    if (indexPath.row % 2 == 0) {
        tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
    } else {
        tcell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
    }
    
    return tcell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WeatherLocationPopupTableViewCell *tcell = (WeatherLocationPopupTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    [_rgroup someRadioButtonTouched:tcell.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];
    }
}


#pragma mark - UI Events

@end
