// The MIT License
//
// Copyright (c) 2016 Dariusz Bukowski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "DBColorCheckbox.h"

@implementation DBColorCheckbox

#pragma mark - Drawing

- (void)layoutSubviews {
    [super layoutSubviews];
    self.layer.cornerRadius = self.frame.size.width / 2.0;
    self.clipsToBounds = YES;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    if (self.isChecked) {
        [self.checkMarkColor setStroke];
        // Based on code generated by PaintCode, mentioned here: https://stackoverflow.com/a/19332828
        CGRect checkMarkRect = CGRectInset(self.bounds, 6.0, 6.0);
        UIBezierPath *bezierPath = [UIBezierPath bezierPath];
        [bezierPath moveToPoint:CGPointMake(CGRectGetMinX(checkMarkRect) + 0.27083 * CGRectGetWidth(checkMarkRect),
                                            CGRectGetMinY(checkMarkRect) + 0.54167 * CGRectGetHeight(checkMarkRect))];
        [bezierPath addLineToPoint:CGPointMake(CGRectGetMinX(checkMarkRect) + 0.41667 * CGRectGetWidth(checkMarkRect),
                                               CGRectGetMinY(checkMarkRect) + 0.68750 * CGRectGetHeight(checkMarkRect))];
        [bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(checkMarkRect) + 0.75000 * CGRectGetWidth(checkMarkRect),
                                                CGRectGetMinY(checkMarkRect) + 0.35417 * CGRectGetHeight(checkMarkRect))];
        bezierPath.lineCapStyle = kCGLineCapSquare;
        bezierPath.lineWidth = 1.3;
        [bezierPath stroke];
    }
}

#pragma mark - Handling touches

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
    self.isChecked = true;
    return YES;
}

#pragma mark - Property setters

- (void)setColor:(UIColor *)color {
    _color = color;
    self.backgroundColor = color;
    [self setupBorderIfNeeded];
}

- (void)setCheckMarkColor:(UIColor *)checkMarkColor {
    _checkMarkColor = checkMarkColor;
    [self setupBorderIfNeeded];
}

- (void)setIsChecked:(BOOL)isChecked {
    if (isChecked != _isChecked) {
        _isChecked = isChecked;
        [self.delegate colorCheckbox:self didChangeValue:isChecked];
        [self setNeedsDisplay];
    }
}

#pragma mark - Private methods

- (void)setupBorderIfNeeded {
    if (self.color == [UIColor whiteColor]) {
        self.layer.borderWidth = 1.0;
        self.layer.borderColor = self.checkMarkColor.CGColor;
    }
}

@end
