//
//  ATNativeSelfRenderView.m
//  AnyThinkSDKDemo
//
//  Created by GUO PENG on 2022/5/7.
//  Copyright © 2022 AnyThink. All rights reserved.
//

#import "ATNativeSelfRenderView.h"


#import <AnyThinkSDK/ATImageLoader.h>
#import "ATNativeAttributeMode.h"
#import "ATDisposeDataTool.h"
#import "ATConfiguration.h"
#import "ATCommonTool.h"


@interface ATNativeSelfRenderView()

@property (nonatomic, strong) NSDictionary *extraDic;

@property(nonatomic, strong) ATNativeAdOffer *nativeAdOffer;
@property(nonatomic, strong) ATNativeAttributeMode *parentMode;
@property(nonatomic, strong) ATNativeAttributeMode *appIconMode;
@property(nonatomic, strong) ATNativeAttributeMode *mainImageMode;
@property(nonatomic, strong) ATNativeAttributeMode *mainTitleMode;
@property(nonatomic, strong) ATNativeAttributeMode *descMode;
//@property(nonatomic, strong) ATNativeAttributeMode *adLogoMode;
@property(nonatomic, strong) ATNativeAttributeMode *ctaMode;
@property(nonatomic, strong) ATNativeAttributeMode *dislikeMode;

@property (nonatomic, strong) NSMutableArray<ATNativeAttributeMode *> *attributeModes;

@end


@implementation ATNativeSelfRenderView

- (instancetype) initWithOffer:(ATNativeAdOffer *)offer{

    if (self = [super init]) {
        
        _attributeModes = @[].mutableCopy;
        _customViews = @[].mutableCopy;
        _nativeAdOffer = offer;
        [self addView];
        [self setupUI];
      
      self.userInteractionEnabled = YES;
      UITapGestureRecognizer *test = [[UITapGestureRecognizer alloc]
          initWithTarget:self action:@selector(testTap)];
      [self addGestureRecognizer:test];
    }
    return self;
}

- (void)testTap {
  
  NSLog(@"!!! testTap fired");
  
}

- (void)updateUIWithoffer:(ATNativeAdOffer *)offer{
    self.nativeAdOffer = offer;
    [self setupUI];
}
- (void)setUIWidget:(NSDictionary *)extraDic {
    self.extraDic = extraDic;
    [self setMode];
    [self setViewValue];
    [self setViewLayout];
}

- (void)setMode{
    
    self.parentMode = [ATDisposeDataTool disposeNativeData:self.extraDic keyStr:Parent];
    
    self.mainImageMode = [ATDisposeDataTool disposeNativeData:self.extraDic keyStr:MainImage];
    
    self.appIconMode = [ATDisposeDataTool disposeNativeData:self.extraDic keyStr:AppIcon];
    
    self.mainTitleMode = [ATDisposeDataTool disposeNativeData:self.extraDic keyStr:MainTitle];
    
    self.descMode = [ATDisposeDataTool disposeNativeData:self.extraDic keyStr:Desc];
    ATNativeAttributeMode *adLogoMode = [ATDisposeDataTool disposeNativeData:self.extraDic keyStr:AdLogo];
    self.hiddenAdLogo = [adLogoMode unsetFrame];
    
    self.ctaMode = [ATDisposeDataTool disposeNativeData:self.extraDic keyStr:Cta];
    
    self.dislikeMode = [ATDisposeDataTool disposeNativeData:self.extraDic keyStr:Dislike];
}

- (void)setViewValue{
    
    self.backgroundColor =  [ATCommonTool colorWithHexString:self.parentMode.backgroundColorStr];
    if (self.parentMode.cornerRadius != 0) {
        self.layer.cornerRadius = self.parentMode.cornerRadius;
        self.layer.masksToBounds = YES;
    }
    
    self.iconImageView.backgroundColor = [ATCommonTool colorWithHexString:self.appIconMode.backgroundColorStr];
    if (self.appIconMode.cornerRadius != 0) {
        self.iconImageView.layer.cornerRadius = self.appIconMode.cornerRadius;
        self.iconImageView.layer.masksToBounds = YES;
    }
    
    self.mainImageView.backgroundColor = [ATCommonTool colorWithHexString:self.mainImageMode.backgroundColorStr];
    if (self.mainImageMode.cornerRadius != 0) {
        self.mainImageView.layer.cornerRadius = self.mainImageMode.cornerRadius;
        self.mainImageView.layer.masksToBounds = YES;
        
        if (self.mediaView) {
            self.mediaView.layer.cornerRadius = self.mainImageMode.cornerRadius;
            self.mediaView.layer.masksToBounds = YES;
        }
    }
    
    self.dislikeButton.backgroundColor = [ATCommonTool colorWithHexString:self.dislikeMode.backgroundColorStr];
    if (self.dislikeMode.cornerRadius != 0) {
        self.dislikeButton.layer.cornerRadius = self.dislikeMode.cornerRadius;
        self.dislikeButton.layer.masksToBounds = YES;
    }
    
    self.titleLabel.font = [UIFont boldSystemFontOfSize:self.mainTitleMode.textSize];
    self.titleLabel.textColor = [ATCommonTool colorWithHexString:self.mainTitleMode.textColorStr];
    self.titleLabel.backgroundColor =  [ATCommonTool colorWithHexString:self.mainTitleMode.backgroundColorStr];
    self.titleLabel.textAlignment = [self setLabelTextAlignment:self.mainTitleMode.textAlignmentStr];
    if (self.mainTitleMode.cornerRadius != 0) {
        self.titleLabel.layer.cornerRadius = self.mainTitleMode.cornerRadius;
        self.titleLabel.layer.masksToBounds = YES;
    }
    
    self.textLabel.font = [UIFont systemFontOfSize:self.descMode.textSize];
    self.textLabel.textColor = [ATCommonTool colorWithHexString:self.descMode.textColorStr];
    self.textLabel.backgroundColor =  [ATCommonTool colorWithHexString:self.descMode.backgroundColorStr];
    self.textLabel.textAlignment = [self setLabelTextAlignment:self.descMode.textAlignmentStr];
    if (self.descMode.cornerRadius != 0) {
        self.textLabel.layer.cornerRadius = self.descMode.cornerRadius;
        self.textLabel.layer.masksToBounds = YES;
    }

    self.ctaLabel.font = [UIFont systemFontOfSize:self.ctaMode.textSize];
    self.ctaLabel.textColor = [ATCommonTool colorWithHexString:self.ctaMode.textColorStr];
    self.ctaLabel.backgroundColor =  [ATCommonTool colorWithHexString:self.ctaMode.backgroundColorStr];
    self.ctaLabel.textAlignment = [self setLabelTextAlignment:self.ctaMode.textAlignmentStr];
    if (self.ctaMode.cornerRadius != 0) {
        self.ctaLabel.layer.cornerRadius = self.ctaMode.cornerRadius;
        self.ctaLabel.layer.masksToBounds = YES;
    }
    
//    self.logoImageView.backgroundColor = [ATCommonTool colorWithHexString:self.adLogoMode.backgroundColorStr];
//    if (self.adLogoMode.cornerRadius != 0) {
//        self.logoImageView.layer.cornerRadius = self.adLogoMode.cornerRadius;
//        self.logoImageView.layer.masksToBounds = YES;
//    }
    
}

- (NSTextAlignment)setLabelTextAlignment:(NSString *)textAlignmentStr {

    if ([textAlignmentStr isEqualToString:@"left"]) {
        return NSTextAlignmentLeft;
    } else if ([textAlignmentStr isEqualToString:@"center"]) {
        return NSTextAlignmentCenter;
    } else if ([textAlignmentStr isEqualToString:@"right"]) {
        return NSTextAlignmentRight;
    } else {
        return NSTextAlignmentLeft;
    }
}

- (void)setViewLayout{
    self.iconImageView.frame = [self getRect:self.appIconMode];
    self.dislikeButton.frame = [self getRect:self.dislikeMode];
    self.titleLabel.frame = [self getRect:self.mainTitleMode];
    self.textLabel.frame = [self getRect:self.descMode];
    self.ctaLabel.frame = [self getRect:self.ctaMode];
//    self.logoImageView.frame = [self getRect:self.adLogoMode];
    self.mainImageView.frame = [self getRect:self.mainImageMode];
    self.mediaView.frame = _mainImageView.frame;
}

- (CGRect)getRect:(ATNativeAttributeMode *)mode{
    CGRect rect = CGRectMake(mode.x, mode.y, mode.width,mode.height);
    return rect;
}

- (void)setIconImageView:(UIImageView *)iconImageView {
    _iconImageView = iconImageView;
}

- (void)addView{
    
    self.advertiserLabel = [[UILabel alloc]init];
    self.advertiserLabel.font = [UIFont boldSystemFontOfSize:15.0f];
    self.advertiserLabel.textColor = [UIColor blackColor];
    self.advertiserLabel.textAlignment = NSTextAlignmentLeft;
    self.advertiserLabel.userInteractionEnabled = YES;
    [self addSubview:self.advertiserLabel];
        
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.userInteractionEnabled = YES;

    [self addSubview:self.titleLabel];
    
    self.textLabel = [[UILabel alloc]init];
    self.textLabel.font = [UIFont systemFontOfSize:15.0f];
    self.textLabel.textColor = [UIColor blackColor];
    self.textLabel.userInteractionEnabled = YES;

    [self addSubview:self.textLabel];
    
    self.ctaLabel = [[UILabel alloc]init];
    self.ctaLabel.font = [UIFont systemFontOfSize:15.0f];
    self.ctaLabel.textColor = [UIColor blackColor];
    self.ctaLabel.userInteractionEnabled = YES;

    [self addSubview:self.ctaLabel];

    self.ratingLabel = [[UILabel alloc]init];
    self.ratingLabel.font = [UIFont systemFontOfSize:15.0f];
    self.ratingLabel.textColor = [UIColor blackColor];
    self.ratingLabel.userInteractionEnabled = YES;

    [self addSubview:self.ratingLabel];
    
    self.iconImageView = [[UIImageView alloc]init];
    self.iconImageView.layer.cornerRadius = 4.0f;
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.iconImageView.userInteractionEnabled = YES;
    [self addSubview:self.iconImageView];
    
    
    self.mainImageView = [[UIImageView alloc]init];
    self.mainImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.mainImageView.userInteractionEnabled = YES;
    [self addSubview:self.mainImageView];
    
//    self.logoImageView = [[UIImageView alloc]init];
//    self.logoImageView.contentMode = UIViewContentModeScaleAspectFit;
//    self.logoImageView.userInteractionEnabled = YES;
//
//    [self addSubview:self.logoImageView];
    
    self.dislikeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
//    UIImage *closeImg = [UIImage imageNamed:@"icon_webview_close" inBundle:[NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"AnyThinkSDK" ofType:@"bundle"]] compatibleWithTraitCollection:nil];
    
    [self.dislikeButton setImage:[self getCloseImage] forState:0];
    [self addSubview:self.dislikeButton];
}

- (UIImage *)getCloseImage {
    
    NSString *imageBase64String = @"iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAYAAABV7bNHAAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAAEigAwAEAAAAAQAAAEgAAAAAYwsr7AAAAAlwSFlzAAALEwAACxMBAJqcGAAAABxpRE9UAAAAAgAAAAAAAAAkAAAAKAAAACQAAAAkAAACiSuooU0AAAJVSURBVHgB5JqhbsMwEECLisqGhldYNNaxqlJRP2CoY8Wjo/ub4UllI5VGy8f6ASMBA0Xb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OtFVEHabDYfvvPvnz9IL0IaaEwOkYRCrwWH+qogjUajOe/z8AfpJfPkhjTzPs3W6/U7wcBrwxGNeQTaIPX7/Ufe4+jz7y95ttTu5KpaCrQFhxqLkLDWixh/5+HBwGoqUSSzzxDqCm+z0ctSXAvR8684H9PyjiIA6uiojB6CQhRF+RtRZKiVtYdw6PEN0tUocNVdenIRSJkfXhAk7PVku5RUQ2phr06Giu7dG8kcrmHf9nPYW+2pVUfunOvRyXXHBkvlMySxSMKeVO2c+qNG/Y4LvXOIJPXIOQaJvO1i4YZm1NMg1pNVutRtQys0BzX0Dl1oJqHRuc/RIIqUSzGaoClYSjUBiZMhBVCoNdASNWpswJDnsUAlDaYMGFJvKqPtzwWsMZCRZMSIrlpDVGnCQqR0HkoVNRROvHGcLtgo6lZZ7eL1yf5ePIP/IwgaKT805vxmzF7SHAAAAABJRU5ErkJggg==";
        
        UIImage *closeImage = [UIImage imageWithData:[[NSData alloc] initWithBase64EncodedString:imageBase64String options:NSDataBase64DecodingIgnoreUnknownCharacters]];
    
    return closeImage;
}

- (void)setupUI{
    
    if (self.nativeAdOffer.nativeAd.icon) {
        self.iconImageView.image = self.nativeAdOffer.nativeAd.icon;
    } else {
        [[ATImageLoader shareLoader]loadImageWithURL:[NSURL URLWithString:self.nativeAdOffer.nativeAd.iconUrl] completion:^(UIImage *image, NSError *error) {
            if (!error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.iconImageView setImage:image];
                });
            }
        }];
    }
    
    if (self.nativeAdOffer.nativeAd.mainImage) {
        self.mainImageView.image = self.nativeAdOffer.nativeAd.mainImage;
    } else {
        [[ATImageLoader shareLoader]loadImageWithURL:[NSURL URLWithString:self.nativeAdOffer.nativeAd.imageUrl] completion:^(UIImage *image, NSError *error) {
            if (!error) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.mainImageView setImage:image];
                });
            }
        }];
    }
    
//    if (self.nativeAdOffer.nativeAd.logo) {
//        self.logoImageView.image = self.nativeAdOffer.nativeAd.logo;
//    } else {
//        [[ATImageLoader shareLoader]loadImageWithURL:[NSURL URLWithString:self.nativeAdOffer.nativeAd.logoUrl] completion:^(UIImage *image, NSError *error) {
//            if (!error) {
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    [self.logoImageView setImage:image];
//                });
//            }
//        }];
//    }
    
    self.advertiserLabel.text = self.nativeAdOffer.nativeAd.advertiser;

    self.titleLabel.text = self.nativeAdOffer.nativeAd.title;
  
    self.textLabel.text = self.nativeAdOffer.nativeAd.mainText;
     
    NSString *ctaText = [ATCommonTool checkStrParamsEmptyAndReturn:self.nativeAdOffer.nativeAd.ctaText];
    self.ctaLabel.text = ctaText.length > 0 ? ctaText : @"Download";
  
    self.ratingLabel.text = [NSString stringWithFormat:@"%@", self.nativeAdOffer.nativeAd.rating ? self.nativeAdOffer.nativeAd.rating : @""];
}

@end
