//
// IMStepper.m
// MyProject
//
// Created by haibara on 4/23/15.
//
//

#import "IMStepper.h"
#import <FontAwesomeKit/FontAwesomeKit.h>
@interface PKYStepper ()
- (void)commonInit;
- (BOOL)isMinimum;
- (BOOL)isMaximum;
@end

@interface IMStepper ()<UITextFieldDelegate>
@property(nonatomic, strong) UITextField *countTextField;
@end

@implementation IMStepper

- (instancetype)init {
	if (self = [super init]) {
		[self commonInit];
	}
	return self;
}

- (CGSize)sizeThatFits:(CGSize)size {
	return CGSizeMake(MAX([self.countLabel sizeThatFits:CGSizeZero].width, self.buttonWidth) + self.buttonWidth * 2, self.buttonWidth);
}

- (CGSize)intrinsicContentSize {
	return CGSizeMake(MAX(self.countLabel.intrinsicContentSize.width, self.buttonWidth) + self.buttonWidth * 2, self.buttonWidth);
}

- (void)tintColorDidChange {
	[super tintColorDidChange];

	[self setLabelTextColor:self.tintColor];
	self.countTextField.textColor = self.tintColor;
	self.incrementButton.titleLabel.textColor = self.tintColor;
	self.decrementButton.titleLabel.textColor = self.tintColor;
}

#pragma mark - Protected

- (void)commonInit {
	[super commonInit];

	self.clipsToBounds = NO;
	self.buttonWidth = 29;

	[self.decrementButton setAttributedTitle:[FAKIonIcons iosMinusIconWithSize:self.buttonWidth].attributedString forState:UIControlStateNormal];
	[self.decrementButton setAttributedTitle:[FAKIonIcons iosMinusOutlineIconWithSize:self.buttonWidth].attributedString forState:UIControlStateDisabled];
	[self.incrementButton setAttributedTitle:[FAKIonIcons iosPlusIconWithSize:self.buttonWidth].attributedString forState:UIControlStateNormal];
	[self.incrementButton setAttributedTitle:[FAKIonIcons iosPlusOutlineIconWithSize:self.buttonWidth].attributedString forState:UIControlStateDisabled];

	[self setBorderColor:[UIColor clearColor]];
	[self tintColorDidChange];
}

-(void)layoutSubviews{
	[super layoutSubviews];

	CGFloat width = self.bounds.size.width;
	CGFloat height = self.bounds.size.height;
	self.countTextField.frame = CGRectMake(self.buttonWidth, 0, width - (self.buttonWidth * 2), height);

}

- (void)setStyle:(StepperStyle)style{
	_style = style;
	if (style == StepperStyletextField) {
		self.countTextField = [[UITextField alloc] init];
		self.countTextField.keyboardType = UIKeyboardTypeNumberPad;
		self.countTextField.textAlignment = NSTextAlignmentCenter;
		[self addSubview:self.countTextField];
		self.countLabel.hidden = YES;
		self.countTextField.delegate = self;
		self.countTextField.text = [NSString stringWithFormat:@"%g",self.value];
	}
}

- (void)setValue:(float)value {
	[super setValue:value];
	
	if (self.style == StepperStyletextField) {
		if (!self.countTextField.isFirstResponder) {
			self.countTextField.text = [NSString stringWithFormat:@"%g",value];
		}else{
			return;
		}
	}

	self.countLabel.text = [NSString stringWithFormat:@"%g", value];
	self.incrementButton.enabled = !self.isMaximum;
	self.decrementButton.enabled = !self.isMinimum;
}

#pragma mark - Public

- (CGFloat)stepValue {
	return self.stepInterval;
}

- (void)setStepValue:(CGFloat)stepValue {
	self.stepInterval = stepValue;
}

- (CGFloat)minimumValue {
	return self.minimum;
}

- (void)setMinimumValue:(CGFloat)minimumValue {
	self.minimum = minimumValue;
}

- (CGFloat)maximumValue {
	return self.maximum;
}

- (void)setMaximumValue:(CGFloat)maximumValue {
	self.maximum = maximumValue;
}

- (void)setValueChangedBlock:(void (^) (IMStepper *stepper))block {
	if (self.countTextField) {
		[self.countTextField resignFirstResponder];
	}
	if (block) {
		self.incrementCallback = ^(PKYStepper *stepper, float newValue) {
			block(stepper);
		};
		self.decrementCallback = ^(PKYStepper *stepper, float newValue) {
			block(stepper);
		};
	}
}

-(void)editValueChangeBlock:(void (^)(IMStepper *))block{
	if (block) {
		self.valueChangedCallback = ^(PKYStepper *stepper, float newValue){
			block(stepper);
		};
	}
}

#pragma mark - UITextFieldDelegate
-(void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason{
	if ([textField.text floatValue] > self.maximumValue) {
		self.value = self.maximumValue;
		return;
	}
	if ([textField.text floatValue] < self.minimumValue) {
		self.value = self.minimumValue;
		return;
	}
	self.value = [textField.text floatValue];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
	if ([textField.text isEqualToString:@""]) {
		self.countTextField.text = [NSString stringWithFormat:@"%g",self.value];
	}
	return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
	NSMutableString *valueString = [textField.text stringByAppendingString:string];
	self.decrementButton.enabled = YES;
	self.incrementButton.enabled = YES;
	if ([valueString floatValue] > self.maximum) {
		self.value = self.maximumValue;
		self.incrementButton.enabled = NO;
		self.countTextField.text = [NSString stringWithFormat:@"%g",self.value];
		return NO;
	}
	if ([valueString floatValue] < self.minimum) {
		self.value = self.minimumValue;
		self.decrementButton.enabled = NO;
		self.countTextField.text = [NSString stringWithFormat:@"%g",self.value];
		return NO;
	}
	return YES;
}
@end
