//
//  BannerViewController.m
//  AdViewSDKDemo
//
//  Created by unakayou on 11/1/19.
//  Copyright © 2019 AdView. All rights reserved.
//

#import "BannerViewController.h"

@interface BannerViewController () <AdViewViewDelegate>
@property (nonatomic, strong) AdViewView * banner;
@end

@implementation BannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)requestButtonClick:(UIButton *)sender {
    [self.banner removeFromSuperview];
    self.banner = [AdViewView requestBannerSize:AdViewBannerSize_320x50 positionId:nil delegate:self];
    [self.backgroundScrollView addSubview:self.banner];
}

- (void)closeButtonClick:(UITapGestureRecognizer *)recognizer {
    [self.banner removeFromSuperview];
    self.banner = nil;
}

- (void)sliderValueChanged:(UISlider *)slider {
    CGRect bannerFrame = self.banner.frame;
    bannerFrame.size.height *= slider.value;
    self.backgroundScrollView.bounds = bannerFrame;
}

- (NSString *)appId {
    return ADVIEW_APPID;
}

- (UIViewController *)viewControllerForShowModal {
    return self;
}

- (BOOL)logMode {
    return YES;
}

- (void)didReceivedAd:(AdViewView *)adView {
    NSLog(@"%s",__FUNCTION__);
    self.backgroundScrollViewFrameSlider.value = 1.0f;
    self.backgroundScrollView.bounds = self.banner.frame;
}

- (void)didFailToReceiveAd:(AdViewView *)adView Error:(NSError *)error {
    NSLog(@"%s",__FUNCTION__);
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGRect contentFrame = self.backgroundScrollView.frame;
    contentFrame.origin.x = (self.view.frame.size.width - self.banner.frame.size.width) / 2;
    contentFrame.origin.y = SPACE;
    self.backgroundScrollView.frame = contentFrame;
    self.backgroundScrollView.contentSize = CGSizeMake(0, self.banner.frame.size.height);
    
    CGRect requestButtonFrame = self.requestButton.frame;
    requestButtonFrame.origin.y += SPACE + requestButtonFrame.size.height;
    self.closebutton.frame = requestButtonFrame;
}

@end
