//
//  RDSHomeSceneView.m
//  Temperature
//
//  Created by RD on 2022/10/25.
//

#import "RDSHomeSceneView.h"
#import "RDSHomeSceneCell.h"

static NSString * const RDSHomeSceneCellID = @"RDSHomeSceneCellID";
const NSInteger CellNumber = 3;
@interface RDSHomeSceneView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, weak) UIPageControl *pageControl;

@end
@implementation RDSHomeSceneView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        [self p_setupCollectionView];
    }
    return self;
}

- (void)p_setupCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat viewW = (SCREEN_WIDTH-20*2-24)/CellNumber;
    layout.itemSize = CGSizeMake(viewW, 48);
    layout.minimumLineSpacing = 12;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 20, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
//    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.backgroundColor = self.backgroundColor;
    [collectionView registerNib:[UINib nibWithNibName:@"RDSHomeSceneCell" bundle:nil] forCellWithReuseIdentifier:RDSHomeSceneCellID];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.pagingEnabled = YES;
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
    }];
    
//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(p_longPressMethod:)];
//    longPress.minimumPressDuration = 0.3f;
//    [collectionView addGestureRecognizer:longPress];
}

- (UIPageControl *)pageControl {
    if (_pageControl == nil) {
        UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 60, 10)];
        pageControl.currentPageIndicatorTintColor = RDSGreenColor;
        pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        [self addSubview:pageControl];
        _pageControl = pageControl;
    }
    return _pageControl;
}

- (void)setScenes:(NSArray *)scenes {
    _scenes = scenes;
    int page = ceilf((scenes.count)/((float)CellNumber));
    if (page > 1) {
        self.pageControl.numberOfPages = page;
        self.pageControl.rds_width = page*30;
        [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self);
        }];
//        self.pageControl.rds_x = (self.rds_width-self.pageControl.rds_width)*0.5;
//        self.pageControl.rds_y = self.rds_height-20;
    }
    self.pageControl.hidden = page <= 1;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    RDS_WEAKSELF(weakSelf)
    if (indexPath.row == _scenes.count) {
        if ([self.delegate respondsToSelector:@selector(rds_sceneSelViewDidSelAddItem:)]) {
            [self.delegate rds_sceneSelViewDidSelAddItem:weakSelf];
        }
    }
    else {
//        if ([self.delegate respondsToSelector:@selector(rds_sceneSelView:didSelectedScene:atIndexPath:)]) {
//            [self.delegate rds_sceneSelView:weakSelf didSelectedScene:_scenes[indexPath.row] atIndexPath:indexPath];
//        }
        
        
//        RDSSceneModel *scene = self.scenes[indexPath.row];
//        [[SigControlHelper sharedInstance] rds_devCtrSceneWithScene:scene];
        
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    self.pageControl.currentPage = (int)round((scrollView.contentOffset.x/self.rds_width));
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.x == roundf(scrollView.contentSize.width)-self.rds_width) {
        self.pageControl.currentPage = self.pageControl.numberOfPages-1;
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _scenes.count; // +1为列表后面的添加按钮
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RDSHomeSceneCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RDSHomeSceneCellID forIndexPath:indexPath];
//    if (indexPath.row == _scenes.count) {
//        cell.sceneIconView.image = [UIImage imageNamed:@"sceneAdd"];
//        cell.nameLab.text = @"添加";
//    }
//    else {
        
//        RDSSceneModel *scene = self.scenes[indexPath.row];
//        cell.sceneIconView.image = [UIImage imageNamed:[NSString stringWithFormat:@"sceneIcon_%d", scene.icon]];
//        cell.nameLab.text = scene.name;
        cell.nameLab.text = [NSString stringWithFormat:@"模式%@",_scenes[indexPath.row]];
//    }
//    cell.contentView.backgroundColor = [UIColor grayColor];
    return cell;
}


@end
