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

#import "MRECViewController.h"

@interface MRECViewController () <AdViewViewDelegate>
@property (nonatomic, strong) AdViewView * MREC;
@end

@implementation MRECViewController

- (void)requestButtonClick:(UIButton *)sender {
    [self.MREC removeFromSuperview];
    int videoMREC = arc4random() % 2;
    self.MREC = [AdViewView requestBannerSize:AdViewBannerSize_300x250 positionId:videoMREC?ADVIEW_VIDEO_POSITION_ID:nil delegate:self];
    [self.backgroundScrollView addSubview:self.MREC];
}

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

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

- (NSString *)appId {
    return ADVIEW_APPID;
}

- (UIViewController *)viewControllerForShowModal {
    return self;
}

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

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

- (BOOL)logMode {
    return YES;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGRect contentFrame = self.backgroundScrollView.frame;
    contentFrame.origin.x = (self.view.frame.size.width - self.MREC.frame.size.width) / 2;
    contentFrame.origin.y = SPACE;
    self.backgroundScrollView.frame = contentFrame;
    self.backgroundScrollView.contentSize = CGSizeMake(0, self.MREC.frame.size.height);
    
    CGRect requestButtonFrame = self.requestButton.frame;
    requestButtonFrame.origin.y += SPACE + requestButtonFrame.size.height;
    self.closebutton.frame = requestButtonFrame;
}

@end
