//
//  YMLineScaleView.m
//  PCDBank
//
//  Created by Gusont on 2021/2/8.
//  Copyright © 2021 YMBank. All rights reserved.
//

#import "SLLineScaleView.h"

@interface SLLineScaleView()

@property (nonatomic, strong) SLLineScaleSliderView *sliderView;

@property (nonatomic, strong) CAGradientLayer *gradientLayer;
@end

@implementation SLLineScaleView

- (void)drawRect:(CGRect)rect {
    self.backgroundColor = UIColor.yellowColor;
    // Drawing code
    CGFloat height = rect.size.height;
    //设置所需的圆角位置以及大小
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(height / 2.0, height/ 2.0)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = rect;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    self.gradientLayer.frame = rect;
    // 渐变色颜色数组,可多个
    self.gradientLayer.colors = [NSArray arrayWithObjects:
                                 (id)[SLColor(@"#1677FF") CGColor],
                                 (id)[SLColor(@"#1677FF") CGColor],
                                 (id)[SLColor(@"#F5F5F5") CGColor],
                                 (id)[SLColor(@"#F5F5F5") CGColor], nil];
    // 渐变的开始点 (不同的起始点可以实现不同位置的渐变,如图)
    self.gradientLayer.startPoint = CGPointMake(0, 0); //(0, 0)
    // 渐变的结束点
    self.gradientLayer.endPoint = CGPointMake(1, 0); //(1, 1)
    self.gradientLayer.locations = @[@0,@(self.scale),@(self.scale),@1];
}

- (CAGradientLayer *)gradientLayer
{
    if (!_gradientLayer) {
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        _gradientLayer = gradientLayer;
        [self.layer insertSublayer:gradientLayer atIndex:0];
    }
    return _gradientLayer;
}

- (void)setScale:(CGFloat)scale
{
    _scale = scale;
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    CGFloat whScale = (height / 2.0) / width;
    if (0 <= _scale && _scale < whScale) {
        self.scale = whScale * 2;
    }
    if ((1 - whScale) < _scale && _scale <= 1) {
        _scale = (1 - whScale * 2);
    }
    [self setNeedsDisplay];
    self.sliderView.scale = _scale;
    [self.sliderView setNeedsDisplay];
}

- (SLLineScaleSliderView *)sliderView
{
    if (!_sliderView) {
        _sliderView = [[SLLineScaleSliderView alloc] initWithFrame:self.bounds];
        _sliderView.backgroundColor = UIColor.clearColor;
        [self addSubview:_sliderView];
    }
    return _sliderView;
}

@end

@interface SLLineScaleSliderView()

@property (nonatomic, strong) CAShapeLayer *sliderLayer;
@end

@implementation SLLineScaleSliderView

- (void)drawRect:(CGRect)rect
{

    CGFloat width = rect.size.height + 1;
    CGFloat left = (rect.size.width * self.scale);
    UIBezierPath *bezier = [[UIBezierPath alloc] init];
    // 起始点
    [bezier moveToPoint:CGPointMake(left, 0)];
    [bezier addLineToPoint:CGPointMake(left + width, 0)];
    [bezier addLineToPoint:CGPointMake(left, width)];
    [bezier addLineToPoint:CGPointMake(left - width, width)];
//    self.sliderLayer.path = bezier.CGPath;
//    self.sliderLayer.fillColor = UIColor.whiteColor.CGColor;
//    self.sliderLayer.strokeColor = UIColor.whiteColor.CGColor;
}

- (CAShapeLayer *)sliderLayer
{
    if (!_sliderLayer) {
        _sliderLayer = [[CAShapeLayer alloc] init];
        [self.layer addSublayer:_sliderLayer];
    }
    return _sliderLayer;
}

@end
