//
// DrawUtils.m
// GotoBus
//
// Created by haibara on 2/12/15.
//
//

#import "DrawUtils.h"
#import <QuartzCore/QuartzCore.h>

@implementation DrawUtils

+ (UIView *)tableViewCellAccessoryDisclosureIndicatorRightWithColor:(UIColor *)color {
	UILabel *label = [[UILabel alloc] init];
	label.attributedText = [self attributedStringWithFAKIcon:[FAKFontAwesome angleRightIconWithSize:24] fillColor:color];
	[label sizeToFit];
	return label;
}

+ (UIView *)tableViewCellAccessoryDisclosureIndicatorDownWithColor:(UIColor *)color {
	UILabel *label = [[UILabel alloc] init];
	label.attributedText = [self attributedStringWithFAKIcon:[FAKFontAwesome angleDownIconWithSize:24] fillColor:color];
	[label sizeToFit];
	return label;
}

+ (void)addBorderToRect:(UIView *)view byEdges:(UIRectEdge)edges strokeColor:(UIColor *)strokeColor {
	return [self addBorderToRect:view byEdges:edges strokeColor:strokeColor borderWidth:1 / [UIScreen mainScreen].scale];
}

+ (void)addBorderToRect:(UIView *)view byEdges:(UIRectEdge)edges strokeColor:(UIColor *)strokeColor borderWidth:(CGFloat)borderWidth {
	if (edges == UIRectEdgeNone) {
		return;
	}
	UIBezierPath *path = [UIBezierPath bezierPath];
	if (edges & UIRectEdgeTop) {
		[path moveToPoint:CGPointZero];
		[path addLineToPoint:CGPointMake(CGRectGetWidth(view.frame), 0)];
	}
	if (edges & UIRectEdgeLeft) {
		[path moveToPoint:CGPointZero];
		[path addLineToPoint:CGPointMake(0, CGRectGetHeight(view.frame))];
	}
	if (edges & UIRectEdgeBottom) {
		[path moveToPoint:CGPointMake(0, CGRectGetHeight(view.frame) - borderWidth)];
		[path addLineToPoint:CGPointMake(CGRectGetWidth(view.frame), CGRectGetHeight(view.frame) - borderWidth)];
	}
	if (edges & UIRectEdgeRight) {
		[path moveToPoint:CGPointMake(CGRectGetWidth(view.frame), 0)];
		[path addLineToPoint:CGPointMake(CGRectGetWidth(view.frame), CGRectGetHeight(view.frame))];
	}
	CAShapeLayer *layer = [CAShapeLayer layer];
	layer.lineWidth = borderWidth;
	layer.strokeColor = strokeColor.CGColor;
	layer.fillColor = [UIColor clearColor].CGColor;
	layer.path = path.CGPath;
	[view.layer addSublayer:layer];
}

+ (void)addSolidBorderToRect:(UIView *)view strokeColor:(UIColor *)strokeColor {
	[self addSolidBorderToRect:view strokeColor:strokeColor borderWidth:1 / [UIScreen mainScreen].scale];
}

+ (void)addSolidBorderToRect:(UIView *)view strokeColor:(UIColor *)strokeColor borderWidth:(CGFloat)borderWidth {
	view.layer.borderWidth = borderWidth;
	view.layer.borderColor = strokeColor.CGColor;
}

+ (void)addRoundedBorderToRect:(UIView *)view cornerRadius:(CGFloat)cornerRadius {
	view.layer.cornerRadius = cornerRadius;
}

+ (void)addRoundedBorderToRect:(UIView *)view byRoundingCorners:(UIRectCorner)corners cornerRadius:(CGFloat)cornerRadius {
	UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:view.layer.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
	CAShapeLayer *layer = [CAShapeLayer layer];
	layer.path = path.CGPath;
	view.layer.mask = layer;
}

+ (void)addRoundedBorderToRect:(UIView *)view cornerRadius:(CGFloat)cornerRadius strokeColor:(UIColor *)strokeColor {
	[self addRoundedBorderToRect:view cornerRadius:cornerRadius strokeColor:strokeColor borderWidth:1 / [UIScreen mainScreen].scale];
}

+ (void)addRoundedBorderToRect:(UIView *)view cornerRadius:(CGFloat)cornerRadius strokeColor:(UIColor *)strokeColor borderWidth:(CGFloat)borderWidth {
	[self addSolidBorderToRect:view strokeColor:strokeColor borderWidth:borderWidth];
	[self addRoundedBorderToRect:view cornerRadius:cornerRadius];
}

+ (void)inscribeOvalToRect:(UIView *)view {
	[self addRoundedBorderToRect:view cornerRadius:MIN(CGRectGetWidth(view.frame), CGRectGetHeight(view.frame)) / 2];
}

+ (void)inscribeOvalToRect:(UIView *)view strokeColor:(UIColor *)strokeColor {
	[self inscribeOvalToRect:view strokeColor:strokeColor borderWidth:1 / [UIScreen mainScreen].scale];
}

+ (void)inscribeOvalToRect:(UIView *)view strokeColor:(UIColor *)strokeColor borderWidth:(CGFloat)borderWidth {
	if (borderWidth >= 0) {
		[self addSolidBorderToRect:view strokeColor:strokeColor borderWidth:borderWidth];
		[self inscribeOvalToRect:view];
	} else {
		[self inscribeOvalToRect:view];
		UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(view.layer.bounds, borderWidth / 2, borderWidth / 2) cornerRadius:view.layer.cornerRadius - borderWidth / 2];
		CAShapeLayer *layer = [CAShapeLayer layer];
		layer.lineWidth = -borderWidth;
		layer.strokeColor = strokeColor.CGColor;
		layer.fillColor = [UIColor clearColor].CGColor;
		layer.path = path.CGPath;
		[view.layer addSublayer:layer];
	}
}

+ (void)addShadowToRect:(UIView *)view {
	view.layer.shadowColor = [UIColor blackColor].CGColor;
	view.layer.shadowOffset = CGSizeZero;
	view.layer.shadowOpacity = 0.5;
	view.layer.shadowRadius = view.layer.cornerRadius;
}

+ (NSAttributedString *)attributedStringWithFAKIcon:(FAKIcon *)icon fillColor:(UIColor *)fillColor {
	[icon addAttribute:NSForegroundColorAttributeName value:fillColor];
	return icon.attributedString;
}

+ (UIImage *)imageWithFAKIcon:(FAKIcon *)icon fillColor:(UIColor *)fillColor {
	[icon addAttribute:NSForegroundColorAttributeName value:fillColor];
	return [icon imageWithSize:CGSizeMake(icon.iconFontSize, icon.iconFontSize)];
}

+ (UIImage *)tableViewCellImageWithFAKIcon:(FAKIcon *)icon fillColor:(UIColor *)fillColor {
	[icon addAttribute:NSForegroundColorAttributeName value:fillColor];
	return [icon imageWithSize:CGSizeMake(24, 24)];
}

@end
