//
//  SMAAdMobNativeAdapterViewController.m
//  sdk-demo-ios-objc
//
//  Created by Smaato Inc on 17.04.20.
//  Copyright © 2020 Smaato Inc. All rights reserved.
//

#import <GoogleMobileAds/GoogleMobileAds.h>
#import <SmaatoSDKNative/SmaatoSDKNative.h>
#import "SMAAdMobNativeAdapterViewController.h"
#import "SMAAdMobSmaatoNativeAdapter.h"

NSString *const AdMobNativeAdsTip_TryLoad = @"Tap ‘Load’ to load a native";
NSString *const AdMobNativeAdsTip_Loading = @"Native is loading…";
NSString *const AdMobNativeAdsTip_Loaded = @"Native was loaded successfully";
NSString *const AdMobNativeAdsTip_Clicked = @"Native has been clicked";
NSString *const AdMobNativeAdsTip_Error = @"Something failed. See console logs";

@interface SMAAdMobNativeAdapterViewController () <GADUnifiedNativeAdLoaderDelegate, GADUnifiedNativeAdDelegate>

@property(nonatomic) GADUnifiedNativeAd *nativeAd;
@property(nonatomic) GADAdLoader *adLoader;

@property(nonatomic, weak) IBOutlet AdBlockView *adBlockView;
@property(nonatomic, weak) IBOutlet UILabel *adUnitIDLabel;
@property(nonatomic, weak) IBOutlet UIButton *showButton;
@property(nonatomic, weak) IBOutlet UILabel *adapterVersionLabel;
@property(nonatomic, weak) IBOutlet UILabel *nativeDidLoadLabel;
@property(nonatomic, weak) IBOutlet UILabel *nativeDidFailLabel;
@property(nonatomic, weak) IBOutlet UILabel *nativeWillPresentScreenLabel;
@property(nonatomic, weak) IBOutlet UILabel *nativeWillDismissScreenLabel;
@property(nonatomic, weak) IBOutlet UILabel *nativeDidDismissScreenLabel;
@property(nonatomic, weak) IBOutlet UILabel *nativeWillLeaveAppLabel;

@end

@implementation SMAAdMobNativeAdapterViewController

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

#pragma mark - Actions

- (IBAction)loadButtonTapped:(id)sender
{
    [self resetLabels];
    self.nativeAd = nil;
    self.adBlockView.tip = AdMobNativeAdsTip_Loading;
    self.showButton.enabled = NO;

    // Load new Native ads
    [self loadAdMobbNativeAdWithAdUnitId:self.adMobModel.adUnitId];
}

- (IBAction)showButtonTapped:(id)sender
{
    self.showButton.enabled = NO;
    [self addNativeViewOnScreen];
}

- (void)setupUI
{
    self.title = @"AdMob Native";
    self.adBlockView.tip = AdMobNativeAdsTip_TryLoad;
    self.adapterVersionLabel.text = [NSString stringWithFormat:@"Adapter version %@", SMAAdMobSmaatoNativeAdapter.version];
    self.adUnitIDLabel.text = self.adMobModel.adUnitId;
}

- (void)resetLabels
{
    self.nativeDidLoadLabel.alpha = 0.2;
    self.nativeDidFailLabel.alpha = 0.2;
    self.nativeWillPresentScreenLabel.alpha = 0.2;
    self.nativeWillDismissScreenLabel.alpha = 0.2;
    self.nativeDidDismissScreenLabel.alpha = 0.2;
    self.nativeWillLeaveAppLabel.alpha = 0.2;
}

#pragma mark - <GADUnifiedNativeAdLoaderDelegate>

- (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd
{
    NSLog(@"AdMob Native Loader %@ did load native ad %@", adLoader, nativeAd);
    [self resetLabels];
    self.nativeAd = nativeAd;
    self.nativeAd.delegate = self;
    self.adBlockView.tip = AdMobNativeAdsTip_Loaded;
    self.nativeDidLoadLabel.alpha = 1;
    self.showButton.enabled = YES;
}

- (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error
{
    NSLog(@"AdMob Native Loader %@ did fail to load: %@", adLoader, error);
    self.adBlockView.tip = AdMobNativeAdsTip_Error;
    self.nativeDidFailLabel.alpha = 1;
}

#pragma mark - <GADUnifiedNativeAdDelegate>

- (void)nativeAdDidRecordClick:(GADUnifiedNativeAd *)nativeAd
{
    NSLog(@"AdMob Native %@ has been clicked", nativeAd);
    self.adBlockView.tip = AdMobNativeAdsTip_Clicked;
}

- (void)nativeAdWillPresentScreen:(GADUnifiedNativeAd *)nativeAd
{
    NSLog(@"AdMob Native %@ will present modal view", nativeAd);
    self.nativeWillPresentScreenLabel.alpha = 1;
}

- (void)nativeAdWillDismissScreen:(GADUnifiedNativeAd *)nativeAd
{
    NSLog(@"AdMob Native %@ will dismiss modal view", nativeAd);
    self.nativeWillPresentScreenLabel.alpha = 1;
}

- (void)nativeAdDidDismissScreen:(GADUnifiedNativeAd *)nativeAd
{
    NSLog(@"AdMob Native %@ did dismiss modal view", nativeAd);
    self.nativeDidDismissScreenLabel.alpha = 1;
}

- (void)nativeAdWillLeaveApplication:(GADUnifiedNativeAd *)nativeAd
{
    NSLog(@"AdMob Native %@ will leave application", nativeAd);
    self.nativeWillLeaveAppLabel.alpha = 1;
}

#pragma mark - Private

- (void)loadAdMobbNativeAdWithAdUnitId:(NSString *)adUnitId
{
    if (!adUnitId.length) {
        NSLog(@"Expect for valid adUnitId");
        return;
    }
    
    GADNativeAdImageAdLoaderOptions *options = [GADNativeAdImageAdLoaderOptions new];
    options.disableImageLoading = NO;
    
    self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:adUnitId rootViewController:self adTypes:@[kGADAdLoaderAdTypeUnifiedNative] options:@[options]];
    self.adLoader.delegate = self;

    GADRequest *request = [GADRequest new];
    [self.adLoader loadRequest:request];
}

- (void)addNativeViewOnScreen
{
    if (!self.nativeAd) {
        NSLog(@"Native ads has not been loaded yet");
        return;
    }
    
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    UINib *nib = [UINib nibWithNibName:@"AdMobNativeAdapterAdView" bundle:bundle];
    GADUnifiedNativeAdView *nativeAdView = (GADUnifiedNativeAdView *)[[nib instantiateWithOwner:nil options:nil] firstObject];

    if (!nativeAdView) {
        NSLog(@"Invalid ad view");
        return;
    }
    
    nativeAdView.nativeAd = self.nativeAd;
    
    nativeAdView.frame = self.adBlockView.bounds;
    nativeAdView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.adBlockView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.adBlockView addSubview:nativeAdView];
    
    [(UILabel *)nativeAdView.headlineView setText:self.nativeAd.headline];
    [(UILabel *)nativeAdView.bodyView setText:self.nativeAd.body];
    [(UILabel *)nativeAdView.callToActionView setText:self.nativeAd.callToAction];
    nativeAdView.mediaView.mediaContent = self.nativeAd.mediaContent;
    [[(GADMediaView *)nativeAdView.imageView mediaContent] setMainImage:self.nativeAd.images.firstObject.image];
    [(UIImageView *)nativeAdView.iconView setImage:self.nativeAd.icon.image];
}

@end
