//
//  LogBannerView.m
//  Open
//
//  Created by 雷俊博 on 2021/7/1.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "LogBannerView.h"



@interface LogBannerView ()<UIScrollViewDelegate>

@end

@implementation LogBannerView
{
    UIScrollView *_mainScrollView;
    UIImageView *currentImageView;
    NSInteger _currentIndex;
    NSMutableArray *_dataArray;
}
/*
 *初始化部件，添加点击手势和定时器
 */
-(instancetype)initWithDataArray:(NSArray *)dataArray
{
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, 75 * WIDTHRADIU)];
    
    if (self) {
        
        _mainScrollView = [[UIScrollView alloc] init];
        _mainScrollView.frame = self.bounds;
        _mainScrollView.backgroundColor = [UIColor whiteColor];
        _mainScrollView.delegate = self;
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.userInteractionEnabled=YES;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.showsVerticalScrollIndicator=NO;
        _mainScrollView.bounces = NO;
        [_mainScrollView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
        
        KKRedPackageClickEventModel *firstModel = dataArray[0];
        KKRedPackageClickEventModel *lastModel = dataArray.lastObject;
        
        _dataArray=[NSMutableArray arrayWithArray:dataArray];
        [_dataArray insertObject:lastModel atIndex:0];
        [_dataArray addObject:firstModel];
        
        
        _currentIndex = 0;
        
        
        [self addSubview:_mainScrollView];
        
        _mainScrollView.contentSize = CGSizeMake(self.bounds.size.width*_dataArray.count, self.bounds.size.height);
        
        [self setUpWithDataArray:_dataArray];
        
       
        
        [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        
    }
    return self;
}

-(void)setUpWithDataArray:(NSArray *)dataArray
{
    for(UIView *view in _mainScrollView.subviews)
    {
        if([view isKindOfClass:[UIImageView class]])
            [view removeFromSuperview];
    }
    
    for (int i = 0; i < dataArray.count; i++) {
        
        KKRedPackageClickEventModel* model = dataArray[i];
        
        UIImageView* currentImageView = [[UIImageView alloc] init];
        [currentImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,model.menuIcon]]];
        currentImageView.userInteractionEnabled=YES;
        currentImageView.frame = CGRectMake(i *  CGRectGetWidth(self.bounds), 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
        currentImageView.tag = 200 + i;
        [_mainScrollView addSubview:currentImageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [currentImageView addGestureRecognizer:tap];
    }
    
}

-(void)timerAction
{
   
    _currentIndex++;
    
    if (_currentIndex == 0) {
        _currentIndex = _dataArray.count - 2 - 1;
        [_mainScrollView setContentOffset:CGPointMake(self.bounds.size.width* (_dataArray.count - 3), 0)];
    }else if (_currentIndex == (_dataArray.count - 1)){
        _currentIndex = 0;
        [_mainScrollView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
    }else{
        [_mainScrollView setContentOffset:CGPointMake(self.bounds.size.width * _currentIndex, 0)];
    }
    
    
}

/*
 *图片的点击响应方法
 */
-(void)tapClick:(UIGestureRecognizer *)ges
{
    if ([self.delegate respondsToSelector:@selector(didClickScrollViewWithModel:)]) {
        UIView *view = ges.view;
        
        KKRedPackageClickEventModel *model = _dataArray[view.tag - 200];
        [self.delegate didClickScrollViewWithModel:model];
    }

}


/*
 *UIScrollViewDelegate  协议方法，拖动图片的处理方法
 */
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int index = scrollView.contentOffset.x/CGRectGetWidth(self.bounds);
    if (index == 0) {
        _currentIndex = _dataArray.count - 2 - 1;
        [scrollView setContentOffset:CGPointMake(self.bounds.size.width* (_dataArray.count - 3), 0)];
    }else if (index == (_dataArray.count - 1)){
        _currentIndex = 0;
        [scrollView setContentOffset:CGPointMake(self.bounds.size.width, 0)];
    }else{
        _currentIndex = index - 1;
    }
    
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
