//
//  KWMSuperLoadingView.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/1/24.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMSuperLoadingView.h"
#import "ArcToCircleLayer.h"

static CGFloat const kRadius = 12;
static CGFloat const kLineWidth = 1.2;
//static CGFloat const kStep1Duration = 5.0;
static CGFloat const kStep1Duration = 2;


@interface KWMSuperLoadingView ()
@property (nonatomic) ArcToCircleLayer *arcToCircleLayer;
@property (nonatomic) CABasicAnimation *animation;

@end

@implementation KWMSuperLoadingView

- (void)awakeFromNib {
    [super awakeFromNib];
}

#pragma mark - public

-(void)initLoadingView{
    [self reset];
    [self doStep1];
}

- (void)startAnimation {
    if(self.arcToCircleLayer && self.animation){
        [self.arcToCircleLayer addAnimation:self.animation forKey:nil];
    }
}

- (void)stopAnimation{
    if(self.arcToCircleLayer && self.animation){
        [self.arcToCircleLayer removeAllAnimations];
    }
}

-(void)setProgress:(CGFloat)progress{
    [self stopAnimation];
    if(self.arcToCircleLayer){
        if(progress>=1){
            self.arcToCircleLayer.progress = -0.0001;
        }else{
            self.arcToCircleLayer.progress = (progress-1)/2;
        }
        // end status
    }
}

#pragma mark - animation
- (void)reset {
    [self.arcToCircleLayer removeFromSuperlayer];
}

- (void)doStep1 {
    self.arcToCircleLayer = [ArcToCircleLayer layer];
    self.arcToCircleLayer.contentsScale = [UIScreen mainScreen].scale;
    [self.layer addSublayer:self.arcToCircleLayer];
    
    self.arcToCircleLayer.bounds = CGRectMake(0, 0, kRadius * 2 + kLineWidth, kRadius * 2 + kLineWidth);
    self.arcToCircleLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    
    // animation
    self.arcToCircleLayer.progress = 1; // end status
    
    self.animation = [CABasicAnimation animationWithKeyPath:@"progress"];
    self.animation.duration = kStep1Duration;
    self.animation.repeatCount = HUGE_VALF;
    self.animation.fromValue = @0.0;
    self.animation.toValue = @1.0;
}

@end