//
//  KKLoadingAnimationView.m
//  Open
//
//  Created by 雷俊博 on 2020/7/17.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "KKLoadingAnimationView.h"

@interface KKLoadingAnimationView ()

@property (nonatomic,strong) UIImageView *imageView;

@property (nonatomic, strong)UIView* whiteView;

@property (nonatomic,strong) NSMutableArray *imageArray;

@end

@implementation KKLoadingAnimationView
+ (instancetype)shared {
    static KKLoadingAnimationView *shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[KKLoadingAnimationView alloc]init];
    });
    return shared;
}
- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.3];
    }
    
    return  self;
}

- (void)showInView:(UIView *)view {
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    if (self.superview) {
        return;
    }
    
    
    self.frame = view.bounds;
    self.whiteView.frame  = CGRectMake(0, 0, 150, 150);
   
    self.whiteView.center = self.center;
    //self.imageView.center = self.whiteView.center;
    
    
    
    [view addSubview:self];
    
    [self addSubview:self.whiteView];
    UIImageView*img = self.imageView;
    [self.whiteView addSubview:img];
    
    self.imageView.frame = CGRectMake(7.5, 12.5, 135, 125);
    
    [self.imageView startAnimating];
}




#pragma mark - public methods
+ (void)show
{
    KKLoadingAnimationView*view = [KKLoadingAnimationView shared];
    [view showInView:nil];
}

+ (void)dismiss
{
    KKLoadingAnimationView*view = [KKLoadingAnimationView shared];
    
    if (view.superview) {
        [ view dismiss];
    }
   
}

- (void)dismiss {
    [_imageArray removeAllObjects];
    [_imageView stopAnimating];
    [_imageView removeFromSuperview];
    [_whiteView removeFromSuperview];
    [self removeFromSuperview];
}

- (NSMutableArray *)imageArray {
    
    if (!_imageArray) {
        _imageArray = [NSMutableArray array];
    }
    return  _imageArray;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        
        UIImageView *img = [[UIImageView alloc]init];
        _imageView = img;
        for (NSInteger i = 1; i < 7; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading_%ld", i]];
            [self.imageArray addObject:image];
        }
        self.imageView.animationDuration = 1;
        self.imageView.animationRepeatCount = 0;
        self.imageView.animationImages = self.imageArray;
    }
      return _imageView;
    
}

- (UIView *)whiteView
{
    if (!_whiteView) {
        _whiteView = [[UIView alloc]init];
        _whiteView.backgroundColor = [UIColor whiteColor];
        _whiteView.layer.cornerRadius = 8;
    }
    return _whiteView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
