//
//  TimePickerButton.m
//  JasonDevelop
//
//  Created by Jason Lee on 2014. 8. 27..
//  Copyright (c) jasondevelop. All rights reserved.
//

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

#define kfToolBarHeight 44.0f
#define kfRoundWidth 2.0f
#define kUIBtnTintColor [UIColor whiteColor]
#define ksEmptyString @""

@interface TimePickerButtonDatePicker : UIDatePicker {
    BOOL changed;
}

@end

@implementation TimePickerButtonDatePicker

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.layer.backgroundColor = [UIColor blackColor].CGColor;
    }
    return self;
}

- (void)awakeFromNib {
    self.layer.backgroundColor = [UIColor clearColor].CGColor;
}

- (void)addSubview:(UIView *)view {
    
    if (!changed) {
        changed = YES;
        [self setValue:[UIColor whiteColor] forKey:@"textColor"];
    }
    [super addSubview:view];
}

@end


@interface TimePickerButton () {
    UIActionSheet *_actionSheet;
    TimePickerButtonDatePicker *_datePicker;
    NSDateFormatter *_dateFormmater;
    UIToolbar *_actionToolbar;
    UIView *_simulatedActionSheetView;
}

@end

@implementation TimePickerButton

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        _datePicker = [[TimePickerButtonDatePicker alloc] init];
        [_datePicker setDatePickerMode:UIDatePickerModeTime];
        
        _dateFormmater = [[CommonUtil dateFormatter] copy];
        [_dateFormmater setTimeZone:[NSTimeZone systemTimeZone]];
        [_dateFormmater setDateFormat:@"HH시 mm분"];
        
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self addTarget:self action:@selector(didTouchButton) forControlEvents:UIControlEventTouchUpInside];
}



#pragma mark - UIActionSheet Delegate

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

- (void)generateActionSheet {
    
    //피커뷰를 만듬.
    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.barStyle = UIBarStyleBlack;
    [_actionToolbar setItems:[NSArray arrayWithObjects:fixedSpace, cancelButton, flexSpace, doneBtn, fixedSpace, nil] animated:YES];
    
    
    _simulatedActionSheetView = [[CommonUtil currentViewController] actionSheetSimulationWithPickerView:_datePicker withToolbar:_actionToolbar];
}

- (void)showPicker {
    [[CommonUtil currentViewController].view endEditing:YES];
    
    if (!_simulatedActionSheetView) {
        [self generateActionSheet];
    }
    [[CommonUtil currentViewController] showActionSheetSimulation:_simulatedActionSheetView pickerView:_datePicker withToolbar:_actionToolbar];
}

- (void)pickerDonePressed:(id)sender {
    [[CommonUtil currentViewController] dismissActionSheetSimulation:_simulatedActionSheetView];
    //request new category.


    NSDateComponents *pickerComps = [CommonUtil dateComponents:_datePicker.date];
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *pickerDate = [cal dateFromComponents:pickerComps];
    
    
    NSString *stringDate = [NSString stringWithFormat:@"%@", [_dateFormmater stringFromDate:pickerDate]];
    [self setTitle:stringDate forState:UIControlStateNormal];
}


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

- (void)setHour:(NSInteger)hour minute:(NSInteger)minute {
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    
    NSDateComponents *ds = [CommonUtil dateComponents:[NSDate date]];
    [ds setHour:hour];
    [ds setMinute:minute];
    [ds setSecond:0];
    

    NSDate *date = [cal dateFromComponents:ds];
    [self setTitle:[CommonUtil stringFromTime:date] forState:UIControlStateNormal];
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state {
    if ([title isEqualToString:ksEmptyString]) {
        [super setTitle:ksEmptyString forState:state];
        return;
    }
    
    [super setTitle:[CommonUtil formattedTime:title] forState:state];
    
    if (_datePicker) {
        NSDateComponents *ds = [CommonUtil timeComponentsFromString:title];
        [ds setTimeZone:[NSTimeZone systemTimeZone]];
        
        NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:ds];
        [_datePicker setDate:date animated:NO];
    }
}

//- (void)setTime:(NSDate *)time {
//    NSString *dateString = [CommonUtil stringFromTime:time];
//    [self setTitle:dateString forState:UIControlStateNormal];
//}

- (NSString *)timeString {
    if ([[self titleForState:UIControlStateNormal] isEqualToString:ksEmptyString])
        return ksEmptyString;
    
//    return _datePicker ? [CommonUtil stringFromTime:_datePicker.date] : ksEmptyString;
    
    NSString *timeString = [NSString stringWithFormat:@"%@", [_dateFormmater stringFromDate:_datePicker.date]];
    return _datePicker ? timeString : ksEmptyString;
}

- (NSString *)hourString {
    if (!self.dateComponents) {
        return nil;
    }
    NSString *hour = self.dateComponents.hour < 10 ? [NSString stringWithFormat:@"0%zd", self.dateComponents.hour] : [NSString stringWithFormat:@"%zd", self.dateComponents.hour];
    return hour;
}

- (NSString *)minuteString {
    if (!self.dateComponents) {
        return nil;
    }
    
    NSString *minute = self.dateComponents.minute < 10 ? [NSString stringWithFormat:@"0%zd", self.dateComponents.minute] : [NSString stringWithFormat:@"%zd", self.dateComponents.minute];
    return minute;
}

- (NSDate *)date {
    return _datePicker.date;
}


- (NSDateComponents *)dateComponents {
    return _datePicker ? [CommonUtil dateComponents:_datePicker.date] : nil;
}

@end
