//
//  WorkFLowCollectionSelectView.m
//  collectioview
//
//  Created by Wenzhe Fan on 2019/11/7.
//  Copyright © 2019年 Wenzhe Fan. All rights reserved.
//

#import "WorkFLowCollectionSelectView.h"
#import "BYHLabelsLayout.h"
#define Xrang [ UIScreen mainScreen ].bounds.size.width/375 //屏幕宽比例
#define Yrang [ UIScreen mainScreen ].bounds.size.height/667//屏幕高比例
#define  textSeleColor_work   UIColorHex(#3979D3);
#define  backSeleColor_work   UIColorHex(E6F0FD);
#define  textColor_work   UIColorHex(999999);
#define  backColor_work   UIColorHex(F7F7F7);
//#define  height self.frame.size.height * 0.8;
@interface WorkFLowCollectionSelectView()<UICollectionViewDelegate, UICollectionViewDataSource, BYHLabelsLayoutDelegate>

@property (nonatomic, strong) NSIndexPath *currentIndex;
@property (strong,nonatomic)  BYHLabelsLayout * layout;
@property CGSize collviewSize;
@property (nonatomic, strong) NSMutableArray *selectedType;


@end

@implementation WorkFLowCollectionSelectView
//@synthesize approvalNameArray = _approvalNameArray;
static WorkFLowCollectionSelectView * selectView;

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setFrame:[UIScreen mainScreen].bounds];
        [self cusView];
    }
    return self;
}
- (void)cusView
{
    self.backgroundColor = [UIColorHex(#000000) colorWithAlphaComponent:0.3];
    [self addSubview:self.collectionView];
    CGFloat with = kGXScreenWidth - 80;
    CGFloat height = 480;
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.size.mas_offset(CGSizeMake(with, height));
    }];
    self.collectionView.backgroundColor = UIColorHex(#FFFFFF);
}
#pragma mark - setter
- (BYHLabelsLayout *)layout{
    if (!_layout) {
        _layout = [[BYHLabelsLayout alloc] init];
        _layout.delegate = self;
    }
    return  _layout;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.layer.cornerRadius = 5;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.layer.masksToBounds = YES;
        [_collectionView registerClass:[WorkFlowCollectViewCell class] forCellWithReuseIdentifier:@"Cell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
    }
    return _collectionView;
}
- (NSMutableArray<WorkFilterModel *> *)selectedType{
    if(!_selectedType){
        _selectedType = [NSMutableArray array];
    }
    return _selectedType;
}

//- (void)setApprovalNameArray:(NSMutableArray<WorkFilterModel *> *)approvalNameArray {
//    _approvalNameArray  = approvalNameArray;
//    //重新计算高度
//    
//}

- (WorkFilterModel *)getSelectedWithName:(NSString *)name {
    for (WorkFilterModel *model in self.selectedType) {
        if ([model.Label isEqualToString:name]) {
            return model;
        }
    }
    return nil;
}

- (NSIndexPath *)currentIndex{
    if (!_currentIndex) {
        _currentIndex = [[NSIndexPath alloc] init];
    }
    return _currentIndex;
}
- (void)collectionViewReloadData
{
    [self show];
    [self.collectionView reloadData];
    self.collectionView.scrollEnabled = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.collectionView scrollToBottomAnimated:NO];
        self.collectionView.scrollEnabled = NO;
    });
}
- (void)show
{
    [self.selectedType removeAllObjects];
    for (WorkFilterModel * model in self.approvalNameArray) {
        if (model.isSelected) {
            [self.selectedType addObject:model];
        }
    }
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.1 animations:^{
        
    } completion:^(BOOL finished) {}];
}
- (void)dismiss
{
    WS(weakSelf);
    [UIView animateWithDuration:0.1 animations:^{
        
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}
#pragma mark - 代理

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

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.approvalNameArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WorkFlowCollectViewCell *cell = (WorkFlowCollectViewCell *)[collectionView  cellForItemAtIndexPath:indexPath];
    NSArray *cellArray = [collectionView visibleCells];
    WorkFilterModel * fmodel = self.approvalNameArray.firstObject;
    WorkFilterModel * model = self.approvalNameArray[indexPath.item];
    model.isSelected = !model.isSelected;
    if (model.isSelected) {
        if (indexPath.item == 0) {
            [self.selectedType removeAllObjects];
            for (WorkFilterModel * smodel in self.approvalNameArray) {
                smodel.isSelected = NO;
            }
            for (WorkFlowCollectViewCell * celltem in cellArray) {
                [celltem selectNamel:NO];
            }
            model.isSelected = YES;
            [cell selectNamel:model.isSelected];
            [self.selectedType addObject:fmodel];
        }else{
            if (fmodel.isSelected) {
                WorkFlowCollectViewCell * celltem = (WorkFlowCollectViewCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
                fmodel.isSelected = NO;
                [celltem selectNamel:NO];
                [self.selectedType removeObject:fmodel];
            }
            [cell selectNamel:YES];
            [self.selectedType addObject:model];
        }
    }else{
        [cell selectNamel:model.isSelected];
        [self.selectedType removeObject:model];
    }
//    if ([self getSelectedWithName:self.approvalNameArray[indexPath.item].Label]) {
//        [self.selectedType removeObject:self.approvalNameArray[indexPath.item]];
//        [cell selectNamel:NO];
//    }else {
//        if (indexPath.item == 0) {
//            NSArray *cellArray = [collectionView visibleCells];
//            [self.selectedType removeAllObjects];
//            for (WorkFlowCollectViewCell * celltem in cellArray) {
//                [celltem selectNamel:NO];
//            }
//        }else{
//            WorkFlowCollectViewCell * celltem = (WorkFlowCollectViewCell *)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
//            if (celltem.isSelected) {
//                [celltem selectNamel:NO];
//                [self.selectedType removeAllObjects];
//            }
//        }
//        [cell selectNamel:YES];
//        [self.selectedType addObject:self.approvalNameArray[indexPath.item]];
//    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//    WorkFlowCollectViewCell * cell = (WorkFlowCollectViewCell *)[collectionView  cellForItemAtIndexPath:indexPath];
//    [cell setDefaultColor];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WorkFlowCollectViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    WorkFilterModel * model = self.approvalNameArray[indexPath.item];
    cell.namelabel.text = model.Label;
    //取消默认选中
    //    if (indexPath.item == 0) {
    //        [cell setSelectColor];
    //    }else{
    [cell selectNamel:model.isSelected];
    //}
    //cell.namelabel.backgroundColor = [UIColor blackColor];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
//        headerView.backgroundColor = [UIColor blueColor];
        UILabel *lablel = [[UILabel alloc]initWithFrame:CGRectMake(15,15, 8.5, 8.5)];
        lablel.backgroundColor = UIColorHex(#3979D3);
        lablel.layer.cornerRadius = 4.25;
        lablel.layer.masksToBounds = YES;
        [headerView addSubview:lablel];
        UILabel *lableName = [UILabel new];
        lableName.text = @"类型";
        lableName.font = [UIFont systemFontOfSize:14.f];
        CGSize lableSize = [lableName sizeThatFits:CGSizeZero];
        lableName.frame = CGRectMake(28,11.75 ,lableSize.width,lableSize.height);
        [headerView addSubview:lableName];
        return headerView;
    }else{
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        UIButton *okButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [okButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchDown];
        [okButton setTitle:@"确定" forState:UIControlStateNormal];
        [okButton setTitleColor:UIColorHex(#FFFFFF) forState:UIControlStateNormal];
        okButton.backgroundColor = UIColorHex(#5192EE);
        okButton.tag = 100;
        [self setButton:okButton];
        [footerView addSubview:okButton];
        CGFloat mywidt = collectionView.width - 38;
        CGFloat myhight = 36;
        okButton.frame  = CGRectMake(19,0, mywidt, myhight);
        
        UIButton *okButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [okButton2 addTarget:self action:@selector(didClickButton2:) forControlEvents:UIControlEventTouchDown];
        [okButton2 setTitle:@"取消" forState:UIControlStateNormal];
        [okButton2 setTitleColor:UIColorHex(#999999) forState:UIControlStateNormal];
        okButton2.backgroundColor = UIColorHex(#F7F7F7);
        okButton2.frame  = CGRectMake(19,36+15, mywidt, myhight);
        
        [self setButton:okButton2];
        okButton2.tag = 101;
        [footerView addSubview:okButton2];
        return footerView;
    }
   
}
- (void)setButton:(UIButton *)sender{
    sender.layer.cornerRadius = 5;
    sender.layer.masksToBounds = YES;
    [[sender titleLabel] setFont:[UIFont systemFontOfSize:15.f]];
}

- (void)didClickButton2:(UIButton *)sender{
    [self dismiss];
}

- (void)didClickButton:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(selecetWorkFLow:withModel:)]) {
        [self.delegate selecetWorkFLow:self.currentIndex withModel:self.selectedType];
    }
    [self dismiss];
}

#pragma mark - BYHLabelsLayoutDelegate

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (collectionView.frame.size.width - 30 - 27)/2;
    CGFloat height = 32;
    if (indexPath.row == 0) {
        return CGSizeMake(width, height);
    }else if (indexPath.row == 1) {
        return CGSizeMake(width, height);
    }else if (indexPath.row == 2) {
        return CGSizeMake(width, height);
    }else if (indexPath.row == 3) {
        return CGSizeMake(width, height);
    }
    return CGSizeMake(width, height);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 27;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 15, 20, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout referenceHeightForHeaderInSection:(NSInteger)section {
    if (section ==0) {
        return 40;
    }else{
      return 0;
    }
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(BYHLabelsLayout *)layout referenceHeightForFooterInSection:(NSInteger)section{
    if (section ==0) {
        return 102;
    }else{
        return 102;
    }
}
@end

@interface WorkFlowCollectHeaderView : UICollectionReusableView

@end
@implementation WorkFlowCollectHeaderView



@end

@interface WorkFlowCollectFooterView : UICollectionReusableView
@property (strong,nonatomic) UIButton *okButton;
@property (strong,nonatomic) UIButton *cancelButton;
@end
@implementation WorkFlowCollectFooterView

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

@end

//@interface  WorkFlowCollectViewCell: UICollectionViewCell
//@property (strong,nonatomic) UILabel *namelabel;
//@end
@implementation WorkFlowCollectViewCell
- (instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        [self.contentView addSubview:self.namelabel];
//        self.namelabel.center = self.contentView.center;
        self.namelabel.frame = self.contentView.frame;
    }
    return self;
}
- (UILabel *)namelabel{
    if (!_namelabel) {
        _namelabel = [UILabel new];
        _namelabel.text = @"请家";
        _namelabel.font = [UIFont systemFontOfSize:13];
        _namelabel.textAlignment = NSTextAlignmentCenter;
    }
    return _namelabel;
}
- (void)selectNamel:(BOOL)isSelect
{
    self.isSelected = isSelect;
    if (isSelect) {
        self.namelabel.textColor =textSeleColor_work;
        self.namelabel.backgroundColor = backSeleColor_work;
    }else{
        self.namelabel.textColor = textColor_work;
        self.namelabel.backgroundColor = backColor_work;
    }
}

@end