//
//  YMBaseMainView.m
//  PCDBank
//
//  Created by Gusont on 2021/2/18.
//  Copyright © 2021 YMBank. All rights reserved.
//

#import "SLBaseMainView.h"

@interface SLBaseMainView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation SLBaseMainView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self registerCollectionViewCell];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self registerCollectionViewCell];
    }
    return self;
}

- (void)registerCollectionViewCell
{
    self.contentBgColor = [UIColor colorFormString:@"#F2F3F7"];
    [self.mainCollectionView registerCellWithReuseIdentifierClass:SLMeHeaderCollectionViewCell.class];
    [self.mainCollectionView registerNibCellWithReuseIdentifierClass:SLMyListCollectionViewCell.class];
    [self.mainCollectionView registerNibCellWithReuseIdentifierClass:SLBaseCollectionViewCell.class];
    [self.mainCollectionView registerNibCellWithReuseIdentifierClass:SLMySpeedCollectionViewCell.class];
    [self.mainCollectionView registerNibCellWithReuseIdentifierClass:SLHomeTopCollectionViewCell.class];
    [self.mainCollectionView registerNibCellWithReuseIdentifierClass:SLHorScrBtnsCell.class];
    [self.mainCollectionView registerNibCellWithReuseIdentifierClass:SLLxTopCell.class];
    [self.mainCollectionView registerNibCellWithReuseIdentifierClass:SLVocabularyCell.class];
    [self.mainCollectionView registerNibCellWithReuseIdentifierClass:SLPlaceholderCell.class];

    
    [self.mainCollectionView registerClass:UICollectionReusableView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass(UICollectionReusableView.class)];
    [self.mainCollectionView registerClass:UICollectionReusableView.class forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass(UICollectionReusableView.class)];
}
//

#pragma mark - UICollectionViewDataSource
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    SLPageViewModel *viewModel = [self.viewModels objectAtIndex:indexPath.section];
    Class cellClass = viewModel.cellClass;
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(cellClass) forIndexPath:indexPath];
    cell.backgroundColor = self.contentBgColor;
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.viewModels.count;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SLPageViewModel *viewModel = [self.viewModels objectAtIndex:indexPath.section];
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    SLPageViewModel *viewModel = [self.viewModels objectAtIndex:indexPath.section];
    UICollectionReusableView *reusableView = nil;
    reusableView.backgroundColor = self.contentBgColor;
    return reusableView;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SLPageViewModel *viewModel = [self.viewModels objectAtIndex:indexPath.section];
    return viewModel.viewSize;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
/**

 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
///SizeForHeader
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    SLPageViewModel *viewModel = [self.viewModels objectAtIndex:section];
    return viewModel.headerSize;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    
    return CGSizeZero;
}

- (UICollectionView *)mainCollectionView
{
    if (!_mainCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0.0;
        layout.minimumInteritemSpacing = 0.0;
        _mainCollectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:layout];
        _mainCollectionView.delegate = self;
        _mainCollectionView.dataSource = self;
        _mainCollectionView.backgroundColor = [UIColor clearColor];
        _mainCollectionView.showsVerticalScrollIndicator = NO;
        _mainCollectionView.showsHorizontalScrollIndicator = NO;
        _mainCollectionView.bounces = YES;
        [self addSubview:_mainCollectionView];
    }
    return _mainCollectionView;
}

- (NSMutableArray<SLPageViewModel *> *)viewModels
{
    if (!_viewModels) {
        _viewModels = [NSMutableArray arrayWithArray:@[]];
    }
    return _viewModels;
}

@end
