//
//  JKLockButton.m
//  Lock
//
//  Created by Jack on 2016/10/13.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "JKLockButton.h"

@interface JKLockButton ()

@property (nonatomic,strong) CAShapeLayer *inscribedCircleLayer;///<内切圆

@property (nonatomic,strong) CAShapeLayer *borderLayer;///<边框


@end

@implementation JKLockButton

- (void)setNumber:(NSInteger)number {
    _number = number;
    
    if (number >= 0 && number < 10) {
        [self setTitle:[NSString stringWithFormat:@"%d",number] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    } else {
        [self setTitle:@"delete" forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    }
    [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
-(CAShapeLayer *)inscribedCircleLayer{
    if (!_inscribedCircleLayer) {
        _inscribedCircleLayer = [CAShapeLayer layer];
        _inscribedCircleLayer.strokeColor = self.strokColor.CGColor;
        _inscribedCircleLayer.fillColor = [UIColor clearColor].CGColor;
        _inscribedCircleLayer.lineWidth = 1.0f;
    }
    return _inscribedCircleLayer;
}

-(CAShapeLayer *)borderLayer{
    if (!_borderLayer) {
        _borderLayer = [CAShapeLayer layer];
        _borderLayer.fillColor = [UIColor clearColor].CGColor;
        _borderLayer.strokeColor = [UIColor clearColor].CGColor;
        _borderLayer.lineWidth = 1.0f;
    }
    return _borderLayer;
}


- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    
    self.inscribedCircleLayer.path = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath;

    self.borderLayer.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    
    [self.layer addSublayer:self.inscribedCircleLayer];
    [self.layer addSublayer:self.borderLayer];
    
}

- (instancetype)initWithFrame:(CGRect)frame strokColor:(UIColor *)strokColor highlightColor:(UIColor *)highlightColor {
    if (self = [super initWithFrame:frame]) {
        self.strokColor = strokColor;
        self.highlightColor = highlightColor;
        [self.layer setNeedsDisplay];
        [self.inscribedCircleLayer addSublayer:self.titleLabel.layer];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self.layer setNeedsDisplay];
        [self.inscribedCircleLayer addSublayer:self.titleLabel.layer];
    }
    return self;
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state {
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.inscribedCircleLayer.strokeColor = self.highlightColor.CGColor;
    self.inscribedCircleLayer.fillColor = self.highlightColor.CGColor;
    
    NSSet *allTargets = self.allTargets;
    
    id target = [allTargets.allObjects firstObject];
    
    if (target) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.inscribedCircleLayer.strokeColor = self.strokColor.CGColor;
    self.inscribedCircleLayer.fillColor = [UIColor clearColor].CGColor;
}

- (UIColor *)highlightColor {
    return _highlightColor ? _highlightColor : [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
}

- (UIColor *)strokColor {
    return _strokColor ? _strokColor : [UIColor lightGrayColor];
}

- (UIColor *)fillColor {
    return _fillColor ? _fillColor : self.highlightColor;
}


@end
