//
//  HomeCollectionFooterReusableView.m
//  ALand
//
//  Created by Z on 16/5/13.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "HomeCollectionFooterReusableView.h"
#import "SDCycleScrollView.h"
#import "JavenAdModel.h"
#import "HomeAdViewController.h"
#import "UIView+Category.h"
#import "NSString+Category.h"
@interface HomeCollectionFooterReusableView ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray<JavenAdModel *> *arrDatas;

@end

@implementation HomeCollectionFooterReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //  获取当前增广视图的宽高
        CGFloat width = frame.size.width;
        CGFloat height = frame.size.height;
        
        
        WS(weakSelf)
        [[HTTPCilent shareCilent] GET:@"adsense/getAll" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            
            NSMutableArray *arrPic = [NSMutableArray array];
            for (NSDictionary *dic in responseObject[@"list"]) {
                JavenAdModel *model = [JavenAdModel modelObjectWithDictionary:dic];
                [weakSelf.arrDatas addObject: model];
                
                NSString *imgUrl;
                if ([model.picture isHasChinese]) {
                    imgUrl = [model.picture stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                }else{
                    imgUrl = model.picture;
                }

                [arrPic addObject:imgUrl];
            }
            
            SDCycleScrollView *scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, width, height) delegate:self placeholderImage:[UIImage imageNamed:@"cyclescrollplaceholder"]];
            scrollView.imageURLStringsGroup = arrPic;
            
            scrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
            scrollView.autoScrollTimeInterval = 3;
            [weakSelf addSubview:scrollView];
        
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
        
    }
    return self;
}


- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    HomeAdViewController *adVC = [[HomeAdViewController alloc] init];
    adVC.isShowNavigationBar = YES;
    adVC.strUrl = [self.arrDatas[index] url];
    [[self viewController] PushViewController:adVC animated:YES];
}


- (NSMutableArray *)arrDatas {
    if (!_arrDatas) {
        _arrDatas = [NSMutableArray array];
        
    }
    return  _arrDatas;
}
@end
