//
//  DateSelectPopupView.m
//  OneCable
//
//  Created by nComz on 2017. 4. 11..
//  Copyright © 2017년 ntels. All rights reserved.
//

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

#import "CustomDatePopupView.h"

@interface CustomDatePopupView () <CustomTextFieldDelegate>
{
    
}

@end

@implementation CustomDatePopupView

- (id)initWithTitle:(NSString *)title
{
    [self setTitle:title];
    
    return [self initFromNib];
}

- (id)initFromNib {
    for (UIView *view in [CommonUtil nibViews:@"CustomDatePopupView"]) {
        if ([view isKindOfClass:[CustomDatePopupView class]]) {
            self = (CustomDatePopupView *)view;
    
            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;
            
            NSArray *arrToday = [[CommonUtil currentDate] explode:@"."];
            
            _txtYear.keyboardType = UIKeyboardTypeNumberPad;
            _txtYear.returnKeyType = UIReturnKeyNext;

            _txtMonth.keyboardType = UIKeyboardTypeNumberPad;
            _txtMonth.returnKeyType = UIReturnKeyNext;

            _txtDay.keyboardType = UIKeyboardTypeNumberPad;
            _txtDay.returnKeyType = UIReturnKeyDone;
            
            [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)];
            
            [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
            
//            [self setTitle:@"정답 날짜 입력"];
        }

    }
    
    return self;
}

- (void)setTitle:(NSString *)title {
    self.lblTitle.text = title;
}

- (void)didMoveToSuperview {
    if (!self.superview) {
        return;
    }

}

- (IBAction)btnConfirmTouched:(id)sender {
    [super btnConfirmTouched:sender];
    [self.delegate dismissPopupView];
}
- (IBAction)btnCancelTouched:(id)sender {
    [super btnCancelTouched:nil];
    [self.delegate dismissPopupView];
}

#pragma mark - textfield delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([textField isEqual:_txtYear]) {
        if (textField.text.length >= 4 && range.length == 0) {
            return NO;
        }
    }
    else {
        if (textField.text.length >= 2 && range.length == 0) {
            return NO;
        }
    }
    return YES;
}

- (void)moveToPrevField:(id)sender {
    
    UITextField *tf = sender;
    NSInteger tag = tf.tag;
    
    if ( ![tf isEqual:_txtYear] ) {
        
        UITextField *prevTf = [self viewWithTag:tag - 1] ;
        [prevTf becomeFirstResponder] ;
        
    }
    
}

- (void)moveToNextField:(id)sender {
    
    UITextField *tf = sender;
    NSInteger tag = tf.tag;
    
    if ( ![tf isEqual:_txtDay] ) {
        
        UITextField *prevTf = [self viewWithTag:tag + 1] ;
        [prevTf becomeFirstResponder] ;
        
    }
    
}


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    [textField becomeFirstResponder];
}

@end
