//
//  CustomButton.m
//  JasonDevelop
//
//  Created by Jason Lee on 2013. 12. 16..
//  Copyright (c) jasondevelop. All rights reserved.
//
#import "CommonUtil.h"
#import "DatePickerButton.h"

#import "CustomDatePopupView.h"

#import "UIViewController-ActionSheetSimulation.h"

#define kfToolBarHeight 44.0f
#define ksEmptyString @""
#define kUIBtnTintColor RGBCOLOR(46, 141, 205)


@interface DatePickerButtonDatePicker : UIDatePicker {
    BOOL changed;
    

}

@end

@implementation DatePickerButtonDatePicker

- (instancetype)init {
    if (self = [super init]) {
        self.layer.backgroundColor = [UIColor whiteColor].CGColor;
    }
    return self;
}

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

- (void)addSubview:(UIView *)view {
    
    if (!changed) {
        changed = YES;
        [self setValue:kUIBtnTintColor forKey:@"textColor"]; //TODO : DatePickerButton change color
    }
    [super addSubview:view];
}

@end

@interface DatePickerButton () {
    DatePickerButtonDatePicker *_datePicker;
    UIToolbar *_actionToolbar;
    NSDateFormatter *_dateFormmater;
    UIView *_simulatedActionSheetView;

    CustomDatePopupView *_popDate;
}

@end

@implementation DatePickerButton

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        _datePicker = [[DatePickerButtonDatePicker alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 216)];
    }
    return self;
}

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


#pragma mark - UIActionSheet Delegate

- (void)didTouchButton {
    if (!_isHidePicker) {
        [self showPicker];
    }
    else {
        [self showPopUp];
    }
}

- (void)generateActionSheet {
    
    [_datePicker setDatePickerMode:UIDatePickerModeDate];
    
    _dateFormmater = [CommonUtil dateFormatter];
    
//    if (_pickerMode == DatePickerModeFull) {
//        [_dateFormmater setDateFormat:@"yyyy/MM/dd"];
//    } else if (_pickerMode == DatePickerModeYear) {
//        [_dateFormmater setDateFormat:@"yyyy"];
//    }
    
    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 = UIBarStyleDefault;
    [_actionToolbar setItems:[NSArray arrayWithObjects:fixedSpace, cancelButton, flexSpace, doneBtn, fixedSpace, nil] animated:YES];
    
    _simulatedActionSheetView = [[CommonUtil currentViewController] actionSheetSimulationWithPickerView:_datePicker withToolbar:_actionToolbar];
    
    
}

- (void)showPopUp {
    if (_popDate == nil) {
        _popDate = [[CustomDatePopupView alloc] initWithTitle:_poupTitle];
    }
    
    [_popDate showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {//OK
            NSLog(@"Input Date : %@.%@.%@", _popDate.txtYear.text, _popDate.txtMonth.text, _popDate.txtDay.text);
        }
    }];
}


- (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];
    
    if (_pickerMode == DatePickerModeFull) {
        [_dateFormmater setDateFormat:@"yyyy/MM/dd"];
    } else if (_pickerMode == DatePickerModeYear) {
        [_dateFormmater setDateFormat:@"yyyy"];
    }
    
    //request new category.
    NSString *stringDate = [NSString stringWithFormat:@"%@", [_dateFormmater stringFromDate:_datePicker.date]];
    
    [self setTitle:stringDate forState:UIControlStateNormal];
    
    
    if ([self.delegate respondsToSelector:@selector(didDoneClicked:)]) {
        [self.delegate didDoneClicked:self];
    }
}


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

- (void)setTitle:(NSString *)title forState:(UIControlState)state {
//    NSLog(@"%@", [self dateString]);
//    NSLog(@"%@", [self formatString]);
    if ([title hasPrefix:@"19"] || [title hasPrefix:@"20"]) {
        // Date형식 변경에 따라 수정
//        [super setTitle:[CommonUtil formattedDate:title] forState:state];
        [super setTitle:title forState:state];

        if (_datePicker)
        {
            NSDateComponents *ds = [CommonUtil dateComponentsFromString:title];
            NSDate *date = [[CommonUtil calendar] dateFromComponents:ds];
            [_datePicker setDate:date animated:NO];
        }
    } else {
        [super setTitle:title forState:state];
    }
}

- (void)setDate:(NSDate *)date {
    NSString *dateString = [CommonUtil stringFromDate:date];
    [self setTitle:dateString forState:UIControlStateNormal];
}

- (NSString *)dateString {
    if ([[self titleForState:UIControlStateNormal] isEqualToString:ksEmptyString])
        return ksEmptyString;
    
    return _datePicker ? [CommonUtil stringFromDate2:_datePicker.date] : ksEmptyString;
}

- (NSString *)formatString {
    return _datePicker ? [CommonUtil stringFromDate:_datePicker.date] : ksEmptyString;
}

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

@end
