#import <AdScopeFoundation/AdScopeFoundation.h>
#import <AMPSAdSDK/AMPSAdSDK.h>
#import "AdScopeDemoStatusObject.h"
#import "AdScopeDemoMacro.h"
@interface AMPSADNUnifiedNativeController () <AMPSUnifiedNativeManagerDelegate, AMPSUnifiedNativeViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *resultsLabel;
@property (nonatomic, strong) AMPSUnifiedNativeManager *nativeManager;
@property (nonatomic, strong) AMPSUnifiedNativeAd *nativeAd;
@property (nonatomic, strong) AMPSUnifiedNativeView *nativeView;
@end
@implementation AMPSADNUnifiedNativeController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    [self setScrollViewAutoLayout];
    
    UIButton *requestBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [requestBtn setTitleColor:[UIColor adScopeColorWithHexString:@"#000000"] forState:UIControlStateNormal];
    [requestBtn setTitle:@"请求广告" forState:UIControlStateNormal];
    requestBtn.frame = CGRectMake(15, 15, self.view.frame.size.width-30, 40);
    requestBtn.layer.masksToBounds = YES;
    requestBtn.layer.borderWidth = 1;
    requestBtn.layer.borderColor = [UIColor adScopeColorWithHexString:@"999999"].CGColor;
    requestBtn.layer.cornerRadius = 3;
    [requestBtn addTarget:self action:@selector(requestBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:requestBtn];
    
    UIButton *showBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [showBtn setTitleColor:[UIColor adScopeColorWithHexString:@"#000000"] forState:UIControlStateNormal];
    [showBtn setTitle:@"显示广告" forState:UIControlStateNormal];
    showBtn.frame = CGRectMake(requestBtn.adScopeFrameLeft, requestBtn.adScopeFrameBottom+15, requestBtn.adScopeFrameWidth, 40);
    showBtn.layer.masksToBounds = YES;
    showBtn.layer.borderWidth = 1;
    showBtn.layer.borderColor = [UIColor adScopeColorWithHexString:@"999999"].CGColor;
    showBtn.layer.cornerRadius = 3;
    [showBtn addTarget:self action:@selector(showBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:showBtn];
    
    self.resultsLabel = [[UILabel alloc]initWithFrame:CGRectMake(showBtn.adScopeFrameLeft, showBtn.adScopeFrameBottom+15, showBtn.adScopeFrameWidth, 30)];
    self.resultsLabel.textColor = [UIColor redColor];
    self.resultsLabel.textAlignment = NSTextAlignmentCenter;
    self.resultsLabel.text = @"广告请求日志";
    [self.scrollView addSubview:self.resultsLabel];
}
- (void)requestBtnClick {
    AMPSAdConfiguration *cfg = [[AMPSAdConfiguration alloc]init];
    cfg.spaceId = kAdScopeDemoADNSDKUnifiedNativeID;
    cfg.adSize = CGSizeMake(self.view.frame.size.width, 0);
    self.nativeManager = [[AMPSUnifiedNativeManager alloc]initWithAdConfiguration:cfg];
    self.nativeManager.delegate = self;
    [self.nativeManager loadUnifiedNativeManager];
    self.resultsLabel.text = @"请求中。。。";
}
- (void)showBtnClick {
    [self.view endEditing:YES];
    self.nativeView.adScopeFrameTop = self.resultsLabel.adScopeFrameBottom + 50;
    [self.scrollView addSubview:self.nativeView];
}
- (void)ampsNativeAdLoadSuccess:(AMPSUnifiedNativeManager *)nativeManager {
    CookieADNLog(@"AMPS-LoadSuccess");
    if (nativeManager.adArray.count > 0) {
        self.nativeAd = [nativeManager.adArray adScopeSafeObjectAtIndex:0];
        self.nativeView = [[AMPSUnifiedNativeView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
        self.nativeView.delegate = self;
        self.nativeView.viewController = self;
        self.nativeView.backgroundColor = [UIColor adScopeColorWithHexString:@"#EEEEEE"];
        [self.nativeView refreshData:self.nativeAd];
        UIImageView *adImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.nativeView.adScopeFrameWidth, self.nativeView.adScopeFrameWidth/16*9)];
        NSURL *imageUrl = [NSURL URLWithString:self.nativeAd.imageUrl];
        NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
        adImageView.image = [UIImage imageWithData:imageData];
        
        [self.nativeView addSubview:adImageView];
        
        UIButton *actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [actionBtn setTitleColor:[UIColor adScopeColorWithHexString:@"#000000"] forState:UIControlStateNormal];
        [actionBtn setTitle:@"查看详情" forState:UIControlStateNormal];
        actionBtn.frame = CGRectMake(adImageView.adScopeFrameRight - 115, adImageView.adScopeFrameBottom + 15, 100, 35);
        actionBtn.layer.masksToBounds = YES;
        actionBtn.layer.borderWidth = 1;
        actionBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        actionBtn.layer.borderColor = [UIColor adScopeColorWithHexString:@"000000"].CGColor;
        actionBtn.layer.cornerRadius = 17;
        [self.nativeView addSubview:actionBtn];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(adImageView.adScopeFrameLeft + 15, adImageView.adScopeFrameBottom + 15, self.nativeView.adScopeFrameWidth - 200, 35)];
        titleLabel.font = [UIFont systemFontOfSize:18.0f];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.text = self.nativeAd.title;
        [self.nativeView addSubview:titleLabel];
        
        [self.nativeView registerClickableViews:@[actionBtn]];
        
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        closeBtn.backgroundColor = [UIColor adScopeColorWithHexString:@"#30999999"];
        [closeBtn setTitleColor:[UIColor adScopeColorWithHexString:@"#000000"] forState:UIControlStateNormal];
        [closeBtn setTitle:@"✕" forState:UIControlStateNormal];
        closeBtn.frame = CGRectMake(adImageView.adScopeFrameRight - 35, adImageView.adScopeFrameTop + 15, 20, 20);
        closeBtn.layer.masksToBounds = YES;
        closeBtn.layer.borderWidth = 1;
        closeBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        closeBtn.layer.borderColor = [UIColor adScopeColorWithHexString:@"#666666"].CGColor;
        closeBtn.layer.cornerRadius = 10;
        [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.nativeView addSubview:closeBtn];
    }
}
- (void)closeBtnClick {
    self.resultsLabel.text = @"广告关闭";
    [self.nativeView removeFromSuperview];
    [self.nativeView unregisterNativeAdDataObject];
    self.nativeView = nil;
    self.nativeAd = nil;
}
- (void)ampsNativeAdLoadFail:(AMPSUnifiedNativeManager *)nativeManager error:(NSError *_Nullable)error {
    CookieADNLog(@"AMPS-ampsNativeAdLoadFail:%@", error);
    self.resultsLabel.text = @"请求失败";
}
- (void)ampsNativeAdRenderSuccess:(AMPSUnifiedNativeView *)nativeView {
    CookieADNLog(@"AMPS-ampsNativeAdRenderSuccess");
    self.resultsLabel.text = @"渲染成功";
}
- (void)ampsNativeAdRenderFail:(AMPSUnifiedNativeView *)nativeView error:(NSError *_Nullable)error {
    CookieADNLog(@"AMPS-ampsNativeAdRenderFail:%@", error);
}
- (void)ampsNativeAdExposured:(AMPSUnifiedNativeView *)nativeView {
    CookieADNLog(@"AMPS-ampsNativeAdExposured");
}
- (void)ampsNativeAdDidClick:(AMPSUnifiedNativeView *)nativeView {
    CookieADNLog(@"AMPS-ampsNativeAdDidClick");
}
- (void)ampsNativeAdDidClose:(AMPSUnifiedNativeView *)nativeView {
    CookieADNLog(@"AMPS-ampsNativeAdDidClose");
}
#pragma mark 页面视图
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.scrollEnabled = YES;
        _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height-kAdScopeDemoNavHeight-30);
        _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _scrollView;
}
- (void)setScrollViewAutoLayout {
    NSDictionary *views = @{@"scrollView":self.scrollView};
    NSArray *hConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[scrollView]-0-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views];
    [self.view addConstraints:hConstraints];
    NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[scrollView]-0-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views];
    [self.view addConstraints:vConstraints];
}
@end
         
      
          
          /**
 本次广告价格,单位分
 */
- (NSInteger)eCPM;
/**
 @pararm winInfo 竞胜信息,字典类型
 AMPS_WIN_PRICE :竞胜价格 (单位: 分),必填
 AMPS_WIN_ADNID :竞胜渠道ID,必填
 AMPS_HIGHRST_LOSS_PRICE :失败渠道中最高价格,必填
 AMPS_EXPECT_PRICE :期望价格,选填
 */
- (void)sendWinNotificationWithInfo:(NSDictionary *)winInfo;
/**
 @pararm lossInfo 竞败信息,字典类型
 AMPS_WIN_PRICE :竞胜价格 (单位: 分),必填
 AMPS_WIN_ADNID :竞胜渠道ID,必填
 AMPS_HIGHRST_LOSS_PRICE :失败渠道中最高价格,必填
 AMPS_LOSS_REASON :失败原因,必填
 AMPS_EXPECT_PRICE :期望价格,选填
 */
- (void)sendLossNotificationWithInfo:(NSDictionary *)lossInfo;