//
//  Responder+Keyboard.m
//  UIKitSupport
//
//  Created by RICHWARE SYSTEMS.
//  Copyright © 2016년 RICHWARE SYSTEMS. All rights reserved.
//
#import <objc/runtime.h>
#import "Responder+Keyboard.h"

static BOOL hasAlreadyCachedKeyboard;

@interface UIResponder (Keyboard_Private)

+(void) __cacheKeyboard;

//@property (strong, nonatomic) id keyboardFrameChangedDelegate ;

@end

@implementation UIResponder(Keyboard)

+(void) cacheKeyboard {
    [[self class] cacheKeyboard:NO];
}

+(void) cacheKeyboard:(BOOL)onNextRunloop {
    if (! hasAlreadyCachedKeyboard)
    {
        hasAlreadyCachedKeyboard = YES;
        if (onNextRunloop)
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.0), dispatch_get_main_queue(), ^(void){ [[self class] __cacheKeyboard]; });
        else
            [[self class] __cacheKeyboard];
    }
}

+(void) __cacheKeyboard {
    UITextField *field = [UITextField new];
    [[[[UIApplication sharedApplication] windows] lastObject] addSubview:field];
    [field becomeFirstResponder];
    [field resignFirstResponder];
    [field removeFromSuperview];
}

//Delegate
//- (id)keyboardFrameChangedDelegate {
//    
//    return objc_getAssociatedObject(self, @selector(keyboardFrameChangedDelegate));
//    
//}
//
//- (void)setKeyboardFrameChangedDelegate:(id)keyboardFrameChangedDelegate {
//    
//    objc_setAssociatedObject(self, @selector(keyboardFrameChangedDelegate), keyboardFrameChangedDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//    
//}

//Keyboard
- (void)addObserverKeyboardDisplay:(KeyboardDisplayBlock)keyboardDisplayBlock {
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification * _Nonnull noti) {
                                                      
                                                      [self __keyboardWillChange:noti
                                                                         display:YES
                                                            keyboardDisplayBlock:keyboardDisplayBlock] ;
                                                      
                                                  }] ;
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification * _Nonnull noti) {
                                                      
                                                      [self __keyboardWillChange:noti
                                                                         display:NO
                                                            keyboardDisplayBlock:keyboardDisplayBlock] ;
                                                      
                                                  }] ;
    
}


- (void)__keyboardWillChange:(NSNotification*)noti display:(BOOL)display keyboardDisplayBlock:(KeyboardDisplayBlock)keyboardDisplayBlock {
    
    NSDictionary* info = [noti userInfo];
    
    NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue] ;
    CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    if( keyboardDisplayBlock != nil ) {
        keyboardDisplayBlock(keyboardFrame, duration, display) ;
    }
    
}

//Keyboard by delegate
- (void)addObserverKeyboardFrameChanged:(id)delegate {
    
    //self.keyboardFrameChangedDelegate = delegate ;
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification * _Nonnull noti) {
                                                      
                                                      [self __keyboardWillChangeByDelegate:delegate
                                                                                      noti:noti
                                                                                   display:YES] ;
                                                      
                                                  }] ;
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification * _Nonnull noti) {
                                                      
                                                      [self __keyboardWillChangeByDelegate:delegate
                                                                                      noti:noti
                                                                                   display:NO] ;
                                                      
                                                  }] ;
    
}


- (void)__keyboardWillChangeByDelegate:(id)delegate noti:(NSNotification*)noti display:(BOOL)display {
    
    NSDictionary* info = [noti userInfo];
    
    NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue] ;
    CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    if( delegate && [delegate respondsToSelector:@selector(keyboardFrameChanged:duration:display:)] ) {
        
        [delegate keyboardFrameChanged:keyboardFrame
                              duration:duration
                               display:display] ;
        
    }
    
}


- (void)removeObserverKeyboard {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil] ;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil] ;
    
}



@end
