//
//  HWToolViewController.m
//  双子星云手机
//
//  Created by 余衡武 on 2022/3/21.
//

#import "HWToolViewController.h"
#import "HWToolListCell.h"


@interface HWToolViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIView             *bigBGView;
@property (weak, nonatomic) IBOutlet UIView             *bgView;
@property (weak, nonatomic) IBOutlet UICollectionView   *collectionView;
@property (strong, nonatomic) NSMutableArray            *dataSource;

@end

@implementation HWToolViewController
#pragma mark 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    [self drawView];
    [self getData];
}

#pragma mark UI布局
- (void)drawView {
    
    self.bgView.backgroundColor = HW1C1C1EColor;
    self.bgView.layer.cornerRadius = 10;
    self.bgView.layer.masksToBounds = YES;
    [self.bigBGView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewClick)]];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HWToolListCell" bundle:nil] forCellWithReuseIdentifier:@"HWToolListCell"];
    
    UICollectionViewFlowLayout *dataLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    CGFloat W = (SCREEN_W - 12*2)*0.25;
    CGFloat H = 88;
    dataLayout.itemSize = CGSizeMake(W, H);
    dataLayout.minimumLineSpacing = 0;
    dataLayout.minimumInteritemSpacing = 0;
    dataLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}

#pragma mark 获取数据
- (void)getData {
    
    [self.dataSource removeAllObjects];
    
    NSArray *nameArr;
    NSArray *iconArr;
    if (self.isSetUnavailable) {
        nameArr  = @[NSLocalizedString(@"more_add_bookmark",nil), NSLocalizedString(@"more_bookmark",nil), NSLocalizedString(@"more_history",nil), NSLocalizedString(@"more_set",nil), NSLocalizedString(@"more_traceless",nil), NSLocalizedString(@"more_refresh",nil), NSLocalizedString(@"more_share",nil)];
        iconArr  = @[@"tianjia_hui_icon", @"shuqian_icon", @"lishi_icon", @"shezhi_icon", @"wuheng_icon", @"shuaxin_hui_icon", @"fenxiang_hui_icon"];
    }else {
        nameArr  = @[NSLocalizedString(@"more_add_bookmark",nil), NSLocalizedString(@"more_bookmark",nil), NSLocalizedString(@"more_history",nil), NSLocalizedString(@"more_set",nil), NSLocalizedString(@"more_traceless",nil), NSLocalizedString(@"more_refresh",nil), NSLocalizedString(@"more_share",nil)];
        iconArr  = @[@"tianjia_icon", @"shuqian_icon", @"lishi_icon", @"shezhi_icon", @"wuheng_icon", @"shuaxin_icon", @"fenxiang_icon"];
    }

    for (int i = 0; i < nameArr.count; i++) {
        NSString *name = nameArr[i];
        NSString *icon = iconArr[i];
        BaseModel *model = [[BaseModel alloc] init];
        model.name = name;
        model.iconName = icon;
        
        if ([name isEqualToString:NSLocalizedString(@"more_add_bookmark",nil)] ||
            [name isEqualToString:NSLocalizedString(@"more_refresh",nil)] ||
            [name isEqualToString:NSLocalizedString(@"more_share",nil)]) {
            model.isSetUnavailable = self.isSetUnavailable;
        }else {
            model.isSetUnavailable = NO;
        }
        
        [self.dataSource addObject:model];
    }
    
    [self.collectionView reloadData];
}

#pragma mark UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWToolListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HWToolListCell" forIndexPath:indexPath];
    BaseModel *model = self.dataSource[indexPath.item];
    cell.model = model;
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    BaseModel *model = self.dataSource[indexPath.item];
//    if ([model.name isEqualToString:@"添加书签"]) {
//        HLog(@"添加书签");
//    }else if ([model.name isEqualToString:@"书签"]) {
//        HLog(@"书签");
//    }else if ([model.name isEqualToString:@"历史"]) {
//        HLog(@"历史");
//    }else if ([model.name isEqualToString:@"设置"]) {
//        HLog(@"设置");
//    }else if ([model.name isEqualToString:@"无痕"]) {
//        HLog(@"无痕");
//    }else if ([model.name isEqualToString:@"刷新"]) {
//        HLog(@"刷新");
//    }else if ([model.name isEqualToString:@"分享"]) {
//        HLog(@"分享");
//    }
    
    if ([_delegate respondsToSelector:@selector(toolDidClickItem:)]) {
        [_delegate toolDidClickItem:model.name];
    }
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark 点击事件
- (void)bgViewClick {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark 懒加载
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

@end
