//
//  AddGroupLabelVC.m
//  smartRhino
//
//  Created by niuzhen on 2020/5/8.
//  Copyright © 2020 tederen. All rights reserved.
//

#import "AddGroupLabelVC.h"
#import "TDGroupLabelCell.h"

@interface AddGroupLabelVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (strong, nonatomic) NSMutableArray  *dataArray;
@end

@implementation AddGroupLabelVC
+(AddGroupLabelVC *)initAddGroupLabelVC{
    AddGroupLabelVC *controller = [StoryboardManager.shared.TDGroup instantiateViewControllerWithIdentifier:@"AddGroupLabelVC"];
    controller.modalPresentationStyle = UIModalPresentationFullScreen;
    return controller;
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView setCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.contentInset = UIEdgeInsetsMake(24, 30, 0, 24);
    [self.collectionView registerNib:[UINib nibWithNibName:@"TDGroupLabelCell" bundle:nil] forCellWithReuseIdentifier:@"TDGroupLabelCell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.textField.returnKeyType = UIReturnKeyDone;
    self.textField.delegate = self;
    self.dataArray = [NSMutableArray arrayWithArray:self.array];
    [self.collectionView reloadData];
    WS(weakSelf);
    [self.addBtn setAction:^{
        if (weakSelf.textField.text.length > 0) {
            [weakSelf.dataArray addObject:weakSelf.textField.text];
            [weakSelf.textField resignFirstResponder];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.collectionView reloadData];
            });
        }
    }];
    [self.doneBtn setAction:^{
        [weakSelf doneAction];
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.textField resignFirstResponder];
    return YES;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREEN_WIDTH - 63) * 0.5, 58.f);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 7.f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 9.f;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TDGroupLabelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TDGroupLabelCell" forIndexPath:indexPath];
    cell.titleL.text = self.dataArray[indexPath.item];
    cell.deleteBtn.hidden = NO;
    WS(weakSelf);
    [cell.deleteBtn setAction:^{
        [weakSelf.dataArray removeObjectAtIndex:indexPath.item];
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.collectionView reloadData];
        });
    }];
    return cell;
}
- (void)doneAction
{
    WS(weakSelf);
    NSString *string = [self.dataArray componentsJoinedByString:@","];
    [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Group_Update) parameters:@{@"LabelName":string,@"Id":@(self.Id)} responseStyle:DATA success:^(id  _Nonnull responseObject) {
        if (weakSelf.refreshDataBlock) {
            weakSelf.refreshDataBlock(weakSelf.dataArray);
        }
        [weakSelf dismissViewControllerAnimated:YES completion:^{
            
        }];
    } failure:^(NSError * _Nonnull error) {
        SHOWERROR([ZYCTool handerResultData:error]);
    }];
}
@end