//
//  ViceLoadingView.m
//  Unity-iPhone
//
//  Created by duowan123 on 2022/8/2.
//

/*************************** 获取屏幕 宽度、高度 ***************************/
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define RGBA(a,b,c,d) [UIColor colorWithRed:a/255.0 green:b/255.0 blue:c/255.0 alpha:d]

#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

#import "ViceLoadingView.h"
#import "MYFactoryManager.h"

@implementation ViceLoadingView

+(instancetype)shanreAnimationView{
    
    static ViceLoadingView * animationView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        animationView =  [ViceLoadingView new];
    });
    return animationView;
    
}


-(void)stopAnimation{
    
//    if (self.imageView.isAnimating){
        
        [self.imageView stopAnimating];
        [self.animationView removeFromSuperview];
        [self.backView removeFromSuperview];
        [self.imageView removeFromSuperview];
        [self.animationLabel removeFromSuperview];
        [self.countButton removeFromSuperview];
        
//    }
    
    [self pauseLayer:self.imageView.layer];
    
}

//初始化动画
-(void)addAnimation{
    
    if (self.animationView!=nil){
        [self stopAnimation];
    }
    
    self.animationView =  [UIView new];
    self.animationView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.animationView.backgroundColor = RGBA(76, 76, 76, 0.8);
    [[UIApplication sharedApplication].keyWindow addSubview:self.animationView];
    
    //
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 90) / 2,(SCREEN_HEIGHT - 90) / 2, 90, 90)];
    self.imageView.image = [MYFactoryManager imageString:@"loading"];
    [self.animationView addSubview:self.imageView];
    
    //tip
    self.animationLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 90) / 2, (SCREEN_HEIGHT - 90)/2+90+10,180, 40)];
//    self.animationLabel.text = @"链接鞋子...";
    self.animationLabel.textColor = [UIColor whiteColor];
    self.animationLabel.font = [UIFont systemFontOfSize:12];
    self.animationLabel.textAlignment = NSTextAlignmentCenter;
    self.animationLabel.backgroundColor = [UIColor clearColor];
    [self.animationView addSubview:self.animationLabel];
    
    //添加旋转动画
    _rotationAnimation = [self rotationDurTime:2 angle:360.f direction:-1 repeatCount:MAXFLOAT];
    _rotationAnimation.delegate = self;
    _rotationAnimation.removedOnCompletion = NO;
    [_rotationAnimation setRepeatCount:HUGE_VALF];
    _rotationAnimation.speed = 1;
    [self.imageView.layer addAnimation:self.rotationAnimation forKey:@"rotate"];
    
    WS(ws);
    //链接超时
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [ws stopAnimation];
    });
    
}

//暂停layer上面的动画
-(void)pauseLayer:(CALayer*)layer{
    
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
    
}

//周期时间 角度 方向 重复次数
-(CABasicAnimation *)rotationDurTime:(float)durTime angle:(float)angle direction:(int)direction repeatCount:(int)repeatCount{
    
    CABasicAnimation* animation  = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
//    animation.fromValue = [NSNumber numberWithFloat:0.f];
    animation.toValue = [NSNumber numberWithFloat:M_PI];
    animation.duration= durTime;
    animation.cumulative= YES;//是否累加
    animation.removedOnCompletion=NO;
    animation.fillMode =kCAFillModeForwards;
    animation.autoreverses = NO;//自动逆转
    animation.repeatCount = repeatCount;
    return animation;
    
}

@end
