//
//  CustomPicker.m
//  JasonDevelop
//
//  Created by Jason Lee on 10/27/14.
//  Copyright (c) jasondevelop. All rights reserved.
//

#import "CommonUtil.h"
#import "UIViewController-ActionSheetSimulation.h"

#import "CustomPicker.h"

#define kfToolBarHeight 44.0f
#define kUIBtnTintColor [UIColor whiteColor]

@interface CustomPicker () <UIPickerViewDataSource, UIPickerViewDelegate> {

@protected
    UIToolbar *_actionToolbar;
    UIView *_simulatedActionSheetView;
    UIPickerView *_pickerView;
    NSInteger _selectedRow;
}

@end

@implementation CustomPicker


- (id)selectedTitle {
    NSInteger row = [_pickerView selectedRowInComponent:0];
    return [_dataDict keyAtIndex:(NSInteger)row];
}

- (id)selectedValue {
    NSInteger row = [_pickerView selectedRowInComponent:0];
    return [_dataDict objectAtIndex:(NSInteger)row];
}

//- (void)setSelectedValue:(id)selectedValue {
//
//    BOOL isValueStringType = [selectedValue isKindOfClass:[NSString class]]; //스트링이 아니면, NSNumber
//    NSString *selectedKey = nil;
//    NSInteger row = 0;
//    for (NSObject *object in _dataDict.allValues) {
//        if (isValueStringType) {
//            NSString *value = (NSString *)object;
//            if ([value isEqualToString:selectedValue]) {
//                break;
//            }
//        } else {
//            NSNumber *value = (NSNumber *)object;
//            if ([value isEqualToNumber:selectedValue]) {
//                break;
//            }
//        }
//        row++;
//    }
//    [_pickerView selectRow:row inComponent:0 animated:YES];
//    selectedKey = [_dataDict keyAtIndex:(NSInteger)row];
//    [self setTitle:selectedKey forState:UIControlStateNormal];
//}


#pragma mark - UIActionSheet Delegate

- (void)presentPicker {
    [self showPicker];
}


- (void)generateActionSheet {
    if (_pickerView)
        return;

    //피커뷰를 만듬.
    _pickerView = [[UIPickerView alloc] init];
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
    _pickerView.showsSelectionIndicator = YES;

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"취소" style:UIBarButtonItemStylePlain target:self action:@selector(pickerCancelPressed:)];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    fixedSpace.width = 10;
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"선택" style:UIBarButtonItemStylePlain target:self action:@selector(pickerDonePressed:)];

    if (IOS_VERSION >= 7.0f) {
        cancelButton.tintColor = kUIBtnTintColor;
        doneBtn.tintColor = kUIBtnTintColor;
    }

    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    _actionToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, width, kfToolBarHeight)];
    _actionToolbar.backgroundColor = [UIColor blackColor];
    _actionToolbar.barStyle = IOS_VERSION >= 7.0f ? UIBarStyleDefault : UIBarStyleBlackTranslucent;
    [_actionToolbar setItems:[NSArray arrayWithObjects:fixedSpace, cancelButton, flexSpace, doneBtn, fixedSpace, nil] animated:YES];


    _simulatedActionSheetView = [[CommonUtil currentViewController] actionSheetSimulationWithPickerView:_pickerView withToolbar:_actionToolbar];
}

- (void)showPicker {
    if (!_simulatedActionSheetView) {
        [self generateActionSheet];
    }
    [[CommonUtil currentViewController] showActionSheetSimulation:_simulatedActionSheetView pickerView:_pickerView withToolbar:_actionToolbar];
}

- (void)pickerDonePressed:(id)sender {

    [[CommonUtil currentViewController] dismissActionSheetSimulation:_simulatedActionSheetView];

    NSInteger row = [_pickerView selectedRowInComponent:0];
//    NSString *string = [_dataDict keyAtIndex:(NSInteger)row];
    if ([_delegate respondsToSelector:@selector(didChangedSelectRow:)]) {
        [_delegate didChangedSelectRow:row];
    }
}


- (void)pickerCancelPressed:(id)sender {
    [[CommonUtil currentViewController] dismissActionSheetSimulation:_simulatedActionSheetView];
}


#pragma mark - UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _dataDict.count;
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *string = [_dataDict keyAtIndex:(NSInteger)row];
    return string;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    //    NSString *string = [_dataDict keyAtIndex:(NSInteger)row];
    //    [self setTitle:string forState:UIControlStateNormal];
//    [self setTitleColor:kUITextColor01 forState:UIControlStateNormal];
}
@end
