//
//  RDSHomeRoomView.m
//  Temperature
//
//  Created by RD on 2022/10/31.
//

#import "RDSHomeRoomView.h"
#import "RDSRoomCell.h"
#import "RDSHomeTopView.h"
#import "RDSHomeSceneView.h"
#import "RDSAddRoomView.h"

/*
 UICollectionView header吸顶悬停
 设置两个section。第一组的section的footer来充当整个UICollectionView的header效果划出；然后设置第二组的header，设置正常的items，UICollectionView 的flowLayout的sectionHeadersPinToVisibleBounds属性设置为true则有悬停效果。

 作者：落夏简叶
 链接：http://events.jianshu.io/p/dd1e87795b63
 */
static NSString * const RDSRoomCellID = @"RDSRoomCellID";
@interface RDSHomeRoomView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionReusableView *footerView;
@property (nonatomic, strong) UICollectionReusableView *headerView;

@property (nonatomic, strong) RDSHomeTopView *topView;
@property (nonatomic, strong) RDSHomeSceneView *sceneView;
@property (nonatomic, strong) RDSAddRoomView *addView;
@end

@implementation RDSHomeRoomView

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


- (void)p_setupCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat viewW = (SCREEN_WIDTH-20*2-10)/2;
    layout.itemSize = CGSizeMake(viewW, viewW);
    layout.minimumLineSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(0, 20, 20, 20);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionHeadersPinToVisibleBounds = YES;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.backgroundColor = self.backgroundColor;
    [collectionView registerNib:[UINib nibWithNibName:@"RDSRoomCell" bundle:nil] forCellWithReuseIdentifier:RDSRoomCellID];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.pagingEnabled = NO;
    [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);
    }];
    
    
    //注册行头
//    [self.collectView registerNib:[UINib nibWithNibName:@"NibName" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReuseIdentifier"];
    //或者
       [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
    
    
//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(p_longPressMethod:)];
//    longPress.minimumPressDuration = 0.3f;
//    [collectionView addGestureRecognizer:longPress];
}



- (void)reloadData{
    [self.collectionView reloadData];
    
    self.addView.hidden = NO;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
//    RDS_WEAKSELF(weakSelf)
//    if (indexPath.row == _scenes.count) {
//        if ([self.delegate respondsToSelector:@selector(rds_sceneSelViewDidSelAddItem:)]) {
//            [self.delegate rds_sceneSelViewDidSelAddItem:weakSelf];
//        }
//    }
//    else {
//
//
//    }
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if(section == 0){
        return 0;
    }
    return 0;//_rooms.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RDSRoomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RDSRoomCellID forIndexPath:indexPath];
    
    return cell;
}

// 不允许高亮
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}





// 设置header尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if(section == 0){
        return CGSizeZero;
    }
    
    return CGSizeMake(SCREEN_WIDTH, 50);
}

// 设置footer尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    if(section == 0){
        return CGSizeMake(SCREEN_WIDTH, 306-60);// -的是场景的高度，场景先不做
    }
    
    return CGSizeZero;
}



//sectionheader
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        footerView.backgroundColor = UIColor.clearColor;
        if(self.topView == nil){
            self.footerView = footerView;
            [self p_setupTopView];
            //[self p_setupSceneView]; 先不做场景功能
        }
        return footerView;
    }
    
    UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
    headView.backgroundColor = RDSViewBgColor;
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 20)];
    title.text = @"我的房间";
    title.font = [UIFont boldSystemFontOfSize:18];
    [headView addSubview:title];
    
    return headView;
}



// 顶部视图
- (void)p_setupTopView{
    
    // 375 217.5
    CGFloat viewH = 206;//kSCALE_WIDTH_6*222;

    RDSHomeTopView *view = [RDSHomeTopView rds_loadViewFromNib];
    view.frame = CGRectMake(0, 0, SCREEN_WIDTH, viewH);
    [self.footerView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.footerView);
        make.height.equalTo(@(viewH));
        make.left.equalTo(self.footerView);
        make.right.equalTo(self.footerView);
    }];
    self.topView = view;
    
}

- (void)p_setupSceneView{
    
    RDSHomeSceneView *scenesView = [[RDSHomeSceneView alloc] init];
    [self.footerView addSubview:scenesView];
    self.sceneView = scenesView;
    //scenesView.delegate = self;
    [scenesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom).offset(10);
        make.height.equalTo(@90);
        make.left.equalTo(self.footerView);//.offset(10);
        make.right.equalTo(self.footerView);//.offset(-10);
    }];
    
    NSArray *scenes = @[@1,@2,@3,@4,@5,@6];
    scenesView.scenes = scenes;
}

- (RDSAddRoomView *)addView{
    if(_addView == nil){
        RDS_WEAKSELF(weakSelf)
        _addView = [RDSAddRoomView rds_loadViewFromNib];
        _addView.hidden = YES;
        [self.collectionView addSubview:_addView];
        [_addView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);//.offset(20);
            make.right.equalTo(self);//.offset(-20);
            make.height.equalTo(@300);
            make.top.equalTo(@(SCREEN_HEIGHT*0.4));
        }];
    }
    return _addView;
}
@end
