//
//  SYPaneView.m
//  SYPanView
//
//  Created by Yunis on 2017/8/18.
//  Copyright © 2017年 Yunis. All rights reserved.
//

#import "SYPaneView.h"
#import <QuartzCore/QuartzCore.h>
@interface SYPaneView()
@property (nonatomic,strong) NSMutableArray <CAShapeLayer *>*layerViewsArray;/**<记录刻度视图*/
@property (nonatomic,strong) NSMutableArray <CAShapeLayer *>*layerHeightViewsArray;/**<记录高亮刻度视图*/
@property (nonatomic,strong) UILabel      *toTaskNumLabel;
@property (nonatomic,strong) UILabel      *leftDesLabel;
@property (nonatomic,strong) UILabel      *rightDesLabel;
@property (nonatomic,strong) NSTimer      *animationTimer;
@property (nonatomic       ) int          index;
@end
@implementation SYPaneView

#pragma mark - Life Cycle

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

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self assignDate];
        [self loadSubViews];
    }
    return self;
}

- (void)dealloc {
    
}
#pragma mark - Intial Methods
//初始化数据
- (void)assignDate
{
    self.radius         = 120;
    self.index          = 0;
    self.startAngle     = -5*M_PI/4;
    self.endAngle       = M_PI/4;
    self.scaleLineWidth = 30;
    self.scaleLineCount = 55;
    self.normalColor    = [UIColor colorWithHex:0xE5E5E5];
    self.hightColor     = kMassColor;
}

- (void)loadSubViews
{
    [self drawBaseView];
    [self addSubview:self.toTaskNumLabel];
    [self addSubview:self.leftDesLabel];
    [self addSubview:self.rightDesLabel];
}

#pragma mark - Public Method
//外部方法

#pragma mark - Private Method
- (void)drawBaseView
{
    [self drawBaseScaleWithStartAngle:self.startAngle
                             endAngle:self.endAngle
                       scaleLineWidth:self.scaleLineWidth
                       scaleLineCount:self.scaleLineCount];
}

- (void)drawBaseScaleWithStartAngle:(CGFloat)startAngle
                           endAngle:(CGFloat)endAngle
                     scaleLineWidth:(CGFloat)scaleLineWidth
                     scaleLineCount:(CGFloat)scaleLineCount
{
    CGPoint cneter = self.center;
    CGFloat radius = self.radius;
    CGFloat perAngle = (endAngle - startAngle)/scaleLineCount;
    UIColor *strokeColor = self.normalColor;
    //绘制最外围刻度列表
    for (NSInteger i = 0; i<= scaleLineCount; i++) {
        CGFloat startAngel = (startAngle+ perAngle * i);
        CGFloat endAngel   = startAngel + perAngle/3;
        UIBezierPath *tickPath = [UIBezierPath bezierPathWithArcCenter:cneter radius:radius startAngle:startAngel endAngle:endAngel clockwise:YES];
        CAShapeLayer *perLayer = [CAShapeLayer layer];
        perLayer.strokeColor = strokeColor.CGColor;;
        perLayer.allowsEdgeAntialiasing=YES;
        perLayer.contentsScale = [UIScreen mainScreen].scale;
        perLayer.lineWidth   = scaleLineWidth;
        perLayer.path = tickPath.CGPath;
        [self.layer addSublayer:perLayer];
        [self.layerViewsArray addObject:perLayer];
    }
    //绘制内圈原
    UIBezierPath *tickPath = [UIBezierPath bezierPathWithArcCenter:cneter radius:radius - 25 startAngle:startAngle endAngle:endAngle clockwise:YES];
    CAShapeLayer *perLayer = [CAShapeLayer layer];
    perLayer.strokeColor = strokeColor.CGColor;;
    perLayer.fillColor = [UIColor clearColor].CGColor;
    perLayer.allowsEdgeAntialiasing=YES;
    perLayer.contentsScale = [UIScreen mainScreen].scale;
    perLayer.lineWidth   = 2;
    perLayer.path = tickPath.CGPath;
    [self.layer addSublayer:perLayer];
}

-(void)showHightColor:(NSTimer *)time{
    if (self.todoTask == 0 || self.allTaskCount == 0) {
        [self resetDate];
        return;
    }
    float percentage = (self.todoTask * 1.0)/self.allTaskCount;
    if (percentage > 1) {
        percentage = 1;
    }
    int count =(int)(self.layerViewsArray.count * percentage) - 1;
    if (self.index >= count) {
        [self resetDate];
        return;
    }
    if (self.index < self.layerViewsArray.count)
    {
        CAShapeLayer *perLayer = self.layerViewsArray[self.index];
        perLayer.strokeColor = self.hightColor.CGColor;;
        [self.layerHeightViewsArray addObject:perLayer];
    }
    self.index++;
}

- (void)resetDate {
    self.index = 0;
    [self.animationTimer invalidate];
    self.animationTimer = nil;
}
#pragma mark - Delegate
//代理方法
- (void)showNormalColor{
    [self.layerHeightViewsArray enumerateObjectsUsingBlock:^(CAShapeLayer * _Nonnull perLayer, NSUInteger idx, BOOL * _Nonnull stop) {
        perLayer.strokeColor = self.normalColor.CGColor;;
    }];
    [self.layerHeightViewsArray removeAllObjects];
}

#pragma mark - Lazy Loads
//懒加载 Getter方法
- (NSMutableArray <CAShapeLayer *>*)layerViewsArray{
    if (_layerViewsArray == nil) {
        _layerViewsArray = ({
            NSMutableArray <CAShapeLayer *>*marray = [NSMutableArray new];
            marray;
        });
    }
    return _layerViewsArray;
}

- (NSMutableArray <CAShapeLayer *>*)layerHeightViewsArray{
    if (_layerHeightViewsArray == nil) {
        _layerHeightViewsArray = ({
            NSMutableArray <CAShapeLayer *>*marray = [NSMutableArray new];
            marray;
        });
    }
    return _layerHeightViewsArray;
}

- (UILabel *)toTaskNumLabel{
    if (_toTaskNumLabel == nil) {
        _toTaskNumLabel = ({
            UILabel *speedLabel      = [[UILabel alloc] initWithFrame: self.bounds];
            speedLabel.center        = self.center;
            speedLabel.font          = [UIFont systemFontOfSize:40.0f weight: UIFontWeightMedium];
            speedLabel.textAlignment = NSTextAlignmentCenter;
            speedLabel.textColor     = [UIColor colorWithHex:0x22293D];
            speedLabel.text          = @"0DB";
            speedLabel;
        });
    }
    
    return _toTaskNumLabel;
}

- (UILabel *)leftDesLabel{
    if (_leftDesLabel == nil) {
        _leftDesLabel = ({
            UILabel * label                 = [[UILabel alloc]initWithFrame:CGRectMake(30, self.center.y + 50, 100, 30)];
            label.text                      = @"0db";
            label.font                      = [UIFont systemFontOfSize:12];
            label.textAlignment             = NSTextAlignmentCenter;
            label.textColor                 = [UIColor colorWithHex:0xCCCCCC];
            label.adjustsFontSizeToFitWidth = YES;
            label;
        });
    }
    
    return _leftDesLabel;
}


- (UILabel *)rightDesLabel{
    if (_rightDesLabel == nil) {
        _rightDesLabel = ({
            UILabel * label                 = [[UILabel alloc]initWithFrame:CGRectMake(self.center.x, self.center.y + 50, 90, 30)];
            label.text                      = @"110db";
            label.font                      = [UIFont systemFontOfSize:12];
            label.textAlignment             = NSTextAlignmentCenter;
            label.textColor                 = [UIColor colorWithHex:0xCCCCCC];
            label.adjustsFontSizeToFitWidth = YES;
            label;
        });
    }
    
    return _rightDesLabel;
}

- (NSTimer *)animationTimer{
    if (_animationTimer == nil) {
        _animationTimer = ({
            NSTimer *animationTimer = [NSTimer timerWithTimeInterval:0.02 target:self selector:@selector(showHightColor:) userInfo:nil repeats:YES];
            [[NSRunLoop mainRunLoop] addTimer:animationTimer forMode:NSRunLoopCommonModes];
            animationTimer;
        });
    }
    
    return _animationTimer;
}
#pragma mark - set
//Setter方法
- (void)setTodoTask:(NSInteger)todoTask
{
    if (_todoTask != todoTask) {
        [self showNormalColor];
        _todoTask = todoTask;
        self.toTaskNumLabel.text = [NSString stringWithFormat:@"%lddb",(long)_todoTask];
        if (_todoTask != 0) {
            [self.animationTimer fire];
        }
    }
}
@end
