//
//  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;
    if (videoMREC) {
        self.MREC = [AdViewView requestBannerSize:AdViewBannerSize_300x250
                                       bannerType:AdViewBannerType_normal
                                       positionId:@"POSIDd6sl3jn9vzqd"
                                         delegate:self];
    } else {
        self.MREC = [AdViewView requestBannerSize:AdViewBannerSize_300x250
                                       bannerType:AdViewBannerType_video
                                       positionId:@"POSID4zt29dh4zbp4"
                                         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
