//
//  KWMLineView.m
//  iOrangeBusiness
//
//  Created by Yaotian on 7/27/15.
//  Copyright (c) 2015 kwm. All rights reserved.
//

#import "KWMLineView.h"
#import "UIColor+SAMAdditions.h"

@interface KWMLineView ()

@property (nonatomic) CGFloat thickness;

@end

@implementation KWMLineView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (void)setup {
    self.backgroundColor = [UIColor clearColor];
}

+ (CGFloat)getLineHeight {
    return 1.0f/[UIScreen mainScreen].scale;
}

- (CGFloat)thickness {
    if (_thickness <= 0) {
        _thickness = 1.0f/[UIScreen mainScreen].scale;
    }
    return _thickness;
}

- (UIColor *)lineColor {
    if (_lineColor == nil) {
        _lineColor = LINE_COLOR;
    }
    return _lineColor;
}

- (void)drawRect:(CGRect)rect {
    if (self.frame.size.height <= 0) {
        CGRect f = self.frame;
        f.size.height = [KWMLineView getLineHeight];
        self.frame = f;
    }
    
    CGContextRef cx = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(cx, self.thickness);
    CGContextSetStrokeColorWithColor(cx, self.lineColor.CGColor);
    
    CGFloat offset = self.isBottomLine ? self.frame.size.height - self.thickness*0.5 : self.thickness*0.5;
    CGContextMoveToPoint(cx, 0, offset);
    CGContextAddLineToPoint(cx, self.bounds.size.width, offset);
    
    CGContextStrokePath(cx);
}


@end