//
//  XCTextFieldDelegateBlockSurrogate.m
//  XCFramework
//
//  Created by Daniel Prows on 11/6/14.
//  Copyright (c) 2014 xactware. All rights reserved.
//

#import "BlockDelegate.h"

@implementation XCBlockTextFieldSurrogate

-(BOOL)respondsToSelector:(SEL)aSelector
{
	BOOL retval = NO;
	if(aSelector == @selector(textFieldShouldBeginEditing:))
	{
		retval = self.textFieldShouldBeginEditingBlock != nil;
	}
	else if(aSelector == @selector(textFieldDidBeginEditing:))
	{
		retval = self.textFieldDidBeginEditingBlock != nil;
	}
	else if(aSelector == @selector(textFieldShouldEndEditing:))
	{
		retval = self.textFieldShouldEndEditingBlock != nil;
	}
	else if(aSelector == @selector(textFieldDidEndEditing:))
	{
		retval = self.textFieldDidEndEditingBlock != nil;
	}
	else if(aSelector == @selector(textField:shouldChangeCharactersInRange:replacementString:))
	{
		retval = self.textFieldShouldChangeCharactersInRangeBlock != nil;
	}
	else if(aSelector == @selector(textFieldShouldClear:))
	{
		retval = self.textFieldShouldClearBlock != nil;
	}
	else if(aSelector == @selector(textFieldShouldReturn:))
	{
		retval = self.textFieldShouldReturnBlock != nil;
	}
	return retval || [self.delegate respondsToSelector:aSelector];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	BOOL returnValue = YES;
	if(self.textFieldShouldBeginEditingBlock)
	{
		returnValue = self.textFieldShouldBeginEditingBlock(textField);
	}
	if([self.delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)])
	{
		returnValue = returnValue && [self.delegate textFieldShouldBeginEditing:textField];
	}
	return returnValue;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
	if(self.textFieldDidBeginEditingBlock)
	{
		self.textFieldDidBeginEditingBlock(textField);
	}
	if([self.delegate respondsToSelector:@selector(textFieldDidBeginEditing:)])
	{
		[self.delegate textFieldDidBeginEditing:textField];
	}
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
	BOOL returnValue = YES;
	if(self.textFieldShouldEndEditingBlock)
	{
		returnValue = self.textFieldShouldEndEditingBlock(textField);
	}
	if([self.delegate respondsToSelector:@selector(textFieldShouldEndEditing:)])
	{
		returnValue = returnValue && [self.delegate textFieldShouldEndEditing:textField];
	}
	return returnValue;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
	if(self.textFieldDidEndEditingBlock)
	{
		self.textFieldDidEndEditingBlock(textField);
	}
	if([self.delegate respondsToSelector:@selector(textFieldDidEndEditing:)])
	{
		[self.delegate textFieldDidEndEditing:textField];
	}
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
	BOOL returnValue = YES;
	if(self.textFieldShouldChangeCharactersInRangeBlock)
	{
		returnValue = self.textFieldShouldChangeCharactersInRangeBlock(textField, range, string);
	}
	if([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)])
	{
		returnValue = returnValue && [self.delegate textField:textField
								shouldChangeCharactersInRange:range
											replacementString:string];
	}
	return returnValue;
}

-(BOOL)textFieldShouldClear:(UITextField *)textField
{
	BOOL returnValue = YES;
	if(self.textFieldShouldClearBlock)
	{
		returnValue = self.textFieldShouldClearBlock(textField);
	}
	if([self.delegate respondsToSelector:@selector(textFieldShouldClear:)])
	{
		returnValue = returnValue && [self.delegate textFieldShouldClear:textField];
	}
	return returnValue;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
	BOOL returnValue = YES;
	if(self.textFieldShouldReturnBlock)
	{
		returnValue = self.textFieldShouldReturnBlock(textField);
	}
	if([self.delegate respondsToSelector:@selector(textFieldShouldReturn:)])
	{
		returnValue = returnValue && [self.delegate textFieldShouldReturn:textField];
	}
	return returnValue;
}

@end
