//
//  Home3MiddleBannerCell.m
//  Open
//
//  Created by 雷俊博 on 2019/8/27.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "Home3MiddleBannerCell.h"
#import "PDBannerView.h"


@interface Home3MiddleBannerCell ()<PDBannerViewDelegate>
{
    NSArray* data2Array;
}

@property (nonatomic, strong)PDBannerView *bannerView;

@property (nonatomic, strong)PDBannerView *bannerview2;

@property (nonatomic, strong)NSMutableArray *dataArray;



@end
@implementation Home3MiddleBannerCell
#pragma mark - life cycle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.bannerView];
    
    }
    return self;
}

#pragma mark - PDBannerViewDelegate
- (void)selectImage:(PDBannerView *)bannerView currentImage:(NSInteger)currentImage
{
    NSLog(@"中间广告点击图片索引%ld",(long)currentImage);
    if (self.modelarray.count > 0) {
        KKRedPackageClickEventModel *model = self.modelarray[currentImage];
        if (self.delegate && [self.delegate respondsToSelector:@selector(selectedItem:)]) {
           [self.delegate selectedItem:model];
        }
    }
    
//    if (data2Array.count > 2) {
//        if (self.delegate && [self.delegate respondsToSelector:@selector(selectedItem:index:)]) {
//            [self.delegate selectedItem:data2Array[currentImage] index:currentImage];
//        }
//    }else{
//        if (self.delegate && [self.delegate respondsToSelector:@selector(selectedItemCell:)]) {
//               [self.delegate selectedItemCell:currentImage];
//           }
//    }
   
    
    
}
#pragma mark - override

- (void)prepareForReuse
{
    [super prepareForReuse];
    //[_bannerview2.adView unregisterDataObject];
}

//- (void)setupCellWithModel:(DHCWNativeAdDataObject *)model
//                  delegate:(id<DHCWNativeAdViewDelegate>)delegate
//        rootViewController:(UIViewController *)rootViewController
//{
//    if (_bannerview2) {
//        [_bannerview2 setupCellWithModel:model delegate:delegate rootViewController:rootViewController];
//    }
//}


#pragma mark - public methods
- (void)setupCellWithModelArray:(NSArray *)modelarray  AdModel:(DHCWNativeAdDataObject *)model
                  delegate:(id<DHCWNativeAdViewDelegate>)delegate loca:(id<DHCWNativeLocaVideoAdDelegate>)localDelegate
        rootViewController:(UIViewController *)rootViewController
{
    if (modelarray.count > 0) {
        if (self.bannerView) {
            [self.bannerView removeFromSuperview];
            self.bannerView = nil;
        }
        
        if (!_bannerview2) {
            [_bannerview2 removeFromSuperview];
            _bannerview2 = nil;
        }
        
        if (!self.bannerview2) {
            NSMutableArray *imagesarray =  [[NSMutableArray alloc]init];
            for (int i = 0; i < modelarray.count; i++) {
                
                KKRedPackageClickEventModel *adModel = modelarray[i];
                [imagesarray addObject:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,adModel.menuIcon]];
            }
    
            UIImage *image = [UIImage imageNamed:@"home_middleBanner"];
            float width = image.size.width;
            float height = image.size.height;
            float widthProportion = (ScreenWidth - 20 * WIDTHRADIU) / width;
            
            float widthProportion2 = ScreenWidth / width;
            
            CGFloat top = height *(widthProportion2 - widthProportion)/2.0;
                   
            _bannerview2 = [[PDBannerView alloc]init2WithFrame:CGRectMake(10 * WIDTHRADIU, top, ScreenWidth - 20* WIDTHRADIU, height * widthProportion) andImageArray:imagesarray];
            _bannerview2.modelArray = modelarray;
            _bannerview2.delegate = self;
            _bannerview2.layer.cornerRadius = 10 * WIDTHRADIU;
            _bannerview2.layer.masksToBounds = YES;
            [self addSubview:_bannerview2];
            data2Array = modelarray;
            
            NSMutableArray *tempArray = [NSMutableArray arrayWithArray:modelarray];
            [tempArray addObject:modelarray.firstObject];
            [tempArray insertObject:modelarray.lastObject atIndex:0];
            
//            for (int i = 0; i < tempArray.count; i++) {
//                KKRedPackageClickEventModel *adModel = tempArray[i];
//                if ([adModel.title isEqualToString:@"免费果园"]) {
//                    [_bannerview2 setupCellWithModel:model delegate:delegate loca:localDelegate rootViewController:rootViewController index:i];
//                }
//            }
            
//            if (model) {
//                _bannerview2.model = model;
//
//                _bannerview2.dhcDelegate = delegate;
//                //_bannerview2.dhcLocaDelegate = delegate;
//                _bannerview2.rootViewController = rootViewController;
//                //[_bannerview2 setupCellWithModel:model delegate:delegate rootViewController:rootViewController];
//            }
            
        }
        
    }
}




- (PDBannerView *)bannerView
{
    if (!_bannerView) {
        UIImage *image = [UIImage imageNamed:@"home_middleBanner"];
        float width = image.size.width;
        float height = image.size.height;
        float widthProportion = (ScreenWidth - 20 * WIDTHRADIU) / width;
        float widthProportion2 = ScreenWidth / width;
        CGFloat top = height *(widthProportion2 - widthProportion)/2.0;
        _bannerView = [[PDBannerView alloc]initWithFrame:CGRectMake(10 * WIDTHRADIU, top, ScreenWidth - 20 * WIDTHRADIU, height * widthProportion) andImageArray:self.dataArray];
        _bannerView.delegate = self;
        _bannerView.layer.cornerRadius = 10 * WIDTHRADIU;
        _bannerView.layer.masksToBounds = YES;
    }
    return _bannerView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        
        NSArray *imageNamesArray = @[@"home_middleBanner1",@"home_middleBanner2",@"home_middleBanner3"];
        
        _dataArray = [[NSMutableArray alloc]initWithArray:imageNamesArray];
    }
    return _dataArray;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
