//
//  LuxADSSModeVC.m
//  TallPaper
//
//  Created by aaa on 2024/7/26.
//

#import "LuxADSSModeVC.h"
#import "LuxNetManager.h"
#import "LuxADManager.h"

@interface LuxADSSButton : UIButton
@property (nonatomic,strong) LuxADManagerInterstitialItem *item;
@property (nonatomic) NSInteger clickCount;
- (void)setADItem:(LuxADManagerInterstitialItem *)item;
@property (nonatomic,strong) NSTimer *checkTimer;
@end

@implementation LuxADSSButton

- (void)setADItem:(LuxADManagerInterstitialItem *)item {
    _clickCount = 0;
    _item = item;
    self.backgroundColor = item.adItem.isReady ? [UIColor blueColor] : [UIColor orangeColor];
    _checkTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(checkIsReady) userInfo:nil repeats:YES];
}

- (void)setClickCount:(NSInteger)clickCount{
    _clickCount = clickCount;
    [self setTitle:[NSString stringWithFormat:@"ID:%@    CN:%ld",_item.adItem.adUnitIdentifier,(long)_clickCount] forState:UIControlStateNormal];
}

- (void)dealloc {
    [_checkTimer invalidate];
    _checkTimer = nil;
}

- (void)checkIsReady {
    self.backgroundColor = _item.adItem.isReady ? [UIColor blueColor] : [UIColor orangeColor];
}
@end

@interface LuxADSSModeVC ()
@property (nonatomic,strong) UITextView *adInfoTextView;
@property (nonatomic,strong) UITextView *actionTextView;
@end



@implementation LuxADSSModeVC


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];
    self.navigationController.navigationBarHidden = YES;
    __block CGFloat currY = 50.0;
    CGFloat cellHeight = 50;
    CGFloat cellPadding = 20;
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    
    CGFloat cellWidth = (screenW - cellPadding*2);
    
    [[[LuxADManager shareInstance] allInterstitialAds] enumerateObjectsUsingBlock:^(LuxADManagerInterstitialItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGRect rect = CGRectMake(cellPadding, currY, cellWidth, cellHeight);
        LuxADSSButton *button = [LuxADSSButton new];
        [button setADItem:obj];
        button.frame = rect;
        button.tag = idx + 1;
        obj.tapPointX = @(button.center.x);
        obj.tapPointY = @(button.center.y);
        [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:[NSString stringWithFormat:@"ID: %@",obj.adItem.adUnitIdentifier] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:18];
        button.backgroundColor = [UIColor orangeColor];
        
        [self.view addSubview:button];
        currY += cellPadding + cellHeight;
        
    }];
    
    _adInfoTextView = [UITextView new];
    _adInfoTextView.frame = CGRectMake(0, currY, screenW*0.5, screenH - currY);
    [self.view addSubview:_adInfoTextView];
    _adInfoTextView.backgroundColor = [UIColor purpleColor];
    _adInfoTextView.editable = NO;
    _adInfoTextView.textColor = [UIColor whiteColor];
    _adInfoTextView.font = [UIFont systemFontOfSize:14];
    _adInfoTextView.layoutManager.allowsNonContiguousLayout = NO;
    
    _actionTextView = [UITextView new];
    _actionTextView.frame = CGRectMake(screenW - _adInfoTextView.frame.size.width, _adInfoTextView.frame.origin.y, _adInfoTextView.frame.size.width, _adInfoTextView.frame.size.height);
    [self.view addSubview:_actionTextView];
    _actionTextView.backgroundColor = [UIColor lightGrayColor];
    _actionTextView.editable = NO;
    _actionTextView.textColor = [UIColor whiteColor];
    _actionTextView.font = [UIFont systemFontOfSize:14];
    _actionTextView.layoutManager.allowsNonContiguousLayout = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adInfoNtification:) name:kLuxADSSModeVC_Notification_AdInfo object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionInfoNtification:) name:kLuxADSSModeVC_Notification_ActionInfo object:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)clickAction:(LuxADSSButton *)sender {
    sender.item.actionBlock(self);
    sender.clickCount += 1;
    NSLog(@"rect:%@",NSStringFromCGRect(sender.frame));
}

- (void)actionInfoNtification:(NSNotification *)notification {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *info = notification.userInfo[@"info"];
        NSString *text = _actionTextView.text ?  _actionTextView.text : @"";
        text = [text stringByAppendingFormat:@"\n\n%@",info];
        _actionTextView.text = text;

        [_actionTextView scrollRangeToVisible:NSMakeRange(_actionTextView.text.length, 1)];
    });
}

- (void)adInfoNtification:(NSNotification *)notification {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *info = notification.userInfo[@"info"];
        NSString *text = _adInfoTextView.text ?  _adInfoTextView.text : @"";
        text = [text stringByAppendingFormat:@"\n\n%@",info];
        _adInfoTextView.text = text;
        [_adInfoTextView scrollRangeToVisible:NSMakeRange(_actionTextView.text.length, 1)];
    });
    
}

- (void)textViewScrollToBottom:(UITextView *)textView {
    NSLog(@"the info:%@",textView.text);
    
    CGFloat bottomOffset = textView.contentSize.height - textView.bounds.size.height;
    NSLog(@"the %@, bound:\n%@",NSStringFromCGSize(textView.contentSize),NSStringFromCGSize(textView.bounds.size));
    if (bottomOffset > 0) {
        [textView setContentOffset:CGPointMake(0, bottomOffset)];
    }
}
@end
