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

#import "CustomLabel.h"
#import "CustomButton.h"

#import "TemperaturePopupView.h"

@implementation TemperaturePickerView

- (void)awakeFromNib {
    self.layer.backgroundColor = [UIColor clearColor].CGColor;
    [self setValue:kUITextColor02 forKey:@"textColor"];
}

- (void)didMoveToSuperview {
//    if (!_changed) {
//        _changed = YES;
//    }
    
    self.showsSelectionIndicator = YES;
    self.subviews[1].backgroundColor = kUITextColor02;
    self.subviews[2].backgroundColor = kUITextColor02;
}

@end

@interface TemperaturePopupView () <UIPickerViewDataSource, UIPickerViewDelegate> {
    
    NSMutableArray<ItemModel> *_checkedItems;
    NSMutableArray *_temperatureArray;
}

@end

@implementation TemperaturePopupView
@synthesize temperature = _temperature;

- (id)initFromNib {

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

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

            NSInteger zindex = 0, i = -50;
            BOOL isZeroSet = NO;
            while (i < 51) {
                if (i != 0 && !isZeroSet) {
                    zindex++;
                } else if (i == 0) {
                    isZeroSet = YES;
                }
                
                NSString *temperature = [NSString stringWithFormat:@"%zd", i++];
                [_temperatureArray addObject:temperature];                
            }
            
            [_temperaturePicker selectRow:zindex inComponent:0 animated:NO];
            
            [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)setTemperature:(NSString *)temperature {
    _temperature = temperature;
//    _temperaturePicker selectRow:zindex inComponent:0 animated:NO];
    if (_temperature) {
        NSInteger row = 50 + [temperature integerValue];
        [_temperaturePicker selectRow:row inComponent:0 animated:YES];
    }

}

#pragma mark - Main Logic
- (NSString *)temperature {
    return [_temperatureArray objectAtIndex:[_temperaturePicker selectedRowInComponent:0]];
}

#pragma mark - Picker Delegate
// 필수 사용메소드 2개 : 이 작업을 하면 피커에 데이터가 들어간다.
// 피커를 사용하기 위해 반드시 사용되어야 할 필수 메소드이다.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// 피커를 사용하기 위해 반드시 사용되어야 할 필수 메소드이다.
- (NSInteger)pickerView:(UIPickerView *) pickerView numberOfRowsInComponent : (NSInteger)component{
    return _temperatureArray.count;
}

// 피커를 사용하기 위해 반드시 사용되어야 할 필수 델리게이트이다.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent: (NSInteger)component{
    return _temperatureArray[row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40.0f;
}

#pragma mark - UI Events

- (IBAction)btnConfirmTouched:(id)sender {
    //validate
    
    [super btnConfirmTouched:sender];
}

@end
