//
//  FPSLable.m
//  MobilePortal
//
//  Created by xiejiangbo on 2019/4/29.
//  Copyright © 2019 yin chen. All rights reserved.
//

#import "FPSLable.h"
#define FPSLableSize CGSizeMake(55, 20)
#define TabBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)
#define TLDeviceHeight [UIScreen mainScreen].bounds.size.height
@interface FPSLable ()

@property (nonatomic, strong)CADisplayLink       *link;
@property (nonatomic, assign)NSUInteger         count;
@property (nonatomic, assign)NSTimeInterval     lastTime;

@end
@implementation FPSLable

+ (instancetype)sharedInstance {
    static FPSLable *config = nil;
    static dispatch_once_t s_predicate;
    dispatch_once(&s_predicate, ^{
        config = [[FPSLable alloc]init];
    });
    return config;
}

+ (void)show{
    
    [[UIApplication sharedApplication].keyWindow addSubview:[FPSLable sharedInstance]];
}
+ (void)hide{
    [[FPSLable sharedInstance].link invalidate];
    [[FPSLable sharedInstance] removeFromSuperview];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    frame = CGRectMake(15, TLDeviceHeight -TabBarHeight -15 -30, 75, 30);
    self = [super initWithFrame:frame];
    
    self.layer.cornerRadius = 5;
    self.clipsToBounds = YES;
    self.textAlignment = NSTextAlignmentCenter;
    self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.700];
    
    self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    return self;
}

- (void)tick:(CADisplayLink *)link {
    if (_lastTime == 0) {
        _lastTime = link.timestamp;
        return;
    }
    
    _count++;
    NSTimeInterval delta = link.timestamp - _lastTime;
    if (delta < 1) return;
    _lastTime = link.timestamp;
    float fps = _count / delta;
    _count = 0;
    NSString *str = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d FPS",(int)round(fps)]];
    
    self.textColor = [UIColor whiteColor];
    self.text = str;
}


@end
