//
//  EZSaaSStreamLoadingView.m
//  EZSaaSVideoConferenceSDK
//
//  Created by EZSaaS on 2020/10/26.
//  Copyright © 2020 EZSaaS. All rights reserved.
//

#import "EZSaaSStreamLoadingView.h"
#import "YSMasonry.h"

@interface EZSaaSStreamLoadingView()

@property (nonatomic, strong) UIActivityIndicatorView *activityView;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) BOOL receiveData;
@property (nonatomic, assign) BOOL isSharing;
@property (nonatomic, assign) BOOL videoState;
@end

@implementation EZSaaSStreamLoadingView

-(instancetype)initWithFrame:(CGRect)frame
{
    NSLog(@"%@ alloc", [self class]);
    self = [super initWithFrame:frame];
    if (self) {
        self.ti = 1;
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = NO;
        [self addSubview:self.activityView];
        [self.activityView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
        
        self.activityView.hidden = YES;
    }
    
    return self;
}

-(void)beginLoadingView
{
    NSLog(@"EZSaaSStreamLoadingView beginLoadingView");
    [self beginLoadingCheckTimer];
}

-(void)endLoadingView
{
    NSLog(@"EZSaaSStreamLoadingView endLoadingView");
    [self endLoadingCheckTimer];
}

-(void)updateLoadingView
{
//    NSLog(@"EZSaaSStreamLoadingView updateLoadingView");
    [self endLoadingCheckTimer];
    [self beginLoadingCheckTimer];
    self.receiveData = YES;
}

-(void)onMemberInfoWithSharingUpdate:(ERTCUserInfo *)notifyModel isSharing:(BOOL)isSharing isSmall:(BOOL)isSmall
{
//    self.isSharing = isSharing;
//    self.activityView.activityIndicatorViewStyle = isSmall ? UIActivityIndicatorViewStyleWhite : UIActivityIndicatorViewStyleWhiteLarge;
//    DeviceState videoState = notifyModel.videoState;
//    if (videoState != DeviceState_DsActive) {
//        self.activityView.hidden = YES;
//        [self.activityView stopAnimating];
//    }
//
//    self.videoState = videoState;
}

-(void)onMemberInfoUpdate:(ERTCUserInfo *)notifyModel isSmall:(BOOL)isSmall
{
    self.activityView.activityIndicatorViewStyle = isSmall ? UIActivityIndicatorViewStyleWhite : UIActivityIndicatorViewStyleWhiteLarge;
    if (!notifyModel.isVideoAvailable) {
        self.activityView.hidden = YES;
        [self.activityView stopAnimating];
    }
    
    self.videoState = notifyModel.isVideoAvailable;
}

#pragma mark - Private Method
-(void)loadingTimerFire:(NSTimer *)timer {
    if (self.videoState) {
        self.activityView.hidden = NO;
        [self.activityView startAnimating];
    } else {
        self.activityView.hidden = YES;
        [self.activityView stopAnimating];
    }
}

-(void)beginLoadingCheckTimer {
    [self endLoadingCheckTimer];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.ti target:self selector:@selector(loadingTimerFire:) userInfo:nil repeats:YES];
}

-(void)endLoadingCheckTimer
{
    [self.timer invalidate];
    self.timer = nil;
    [self.activityView stopAnimating];
    self.activityView.hidden = YES;
    self.receiveData = NO;
}

-(UIActivityIndicatorView *)activityView
{
    if (!_activityView) {
//        if (@available(iOS 13.0, *)) {
//            _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleMedium)];
//        } else {
            _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhiteLarge)];
//        }
    }
    
    return _activityView;
}

-(void)dealloc
{
    NSLog(@"%@ dealloc", [self class]);
}

@end
