//
//  DrawerView.m
//  smartRhino
//
//  Created by tederen on 2019/10/21.
//  Copyright © 2019 tederen. All rights reserved.
//

#import "DrawerView.h"
#import "DrawerCell.h"

#define Xrang [ UIScreen mainScreen ].bounds.size.width/375 //屏幕宽比例
#define Yrang [ UIScreen mainScreen ].bounds.size.height/667//屏幕高比例

@interface DrawerView () <UITableViewDataSource, UITableViewDelegate,UIGestureRecognizerDelegate> {
    NSArray *_imageNameArr;
    NSArray *_drawerNameArr;
}

@property (nonatomic, strong) UIView *leftBgView;
@property (nonatomic, strong) UILabel *drawerTitleLab;
@property (nonatomic, strong) UIView *blueView;
@property (nonatomic, strong) TDTableView *tableView;

@end

@implementation DrawerView
TDShareInstance_implementation(DrawerView)
- (instancetype)init{
    self = [super  init];
    if (self) {
        self = [[DrawerView alloc]initWithFrame:CGRectMake(0, 0,kGXScreenWidth,kGXScreenHeigh - kStatusBarHeight)];
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    return self;
    
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.65];
        [self addSubview:self.leftBgView];
        [self.leftBgView addSubview:self.drawerTitleLab];
        [self.leftBgView addSubview:self.blueView];
        [self.leftBgView addSubview:self.tableView];
        
        CGSize titleSize = [_drawerTitleLab sizeThatFits:CGSizeZero];
        _drawerTitleLab.frame = CGRectMake(32, kNavigationHeight, 275*Xrang - 64, titleSize.height);
        _blueView.frame = CGRectMake(32, kNavigationHeight+titleSize.height+4, 275*Xrang - 64, 3.f);
        _blueView.layer.cornerRadius = 1.5;
        _blueView.layer.masksToBounds = YES;
        _tableView.frame = CGRectMake(0,kNavigationHeight+titleSize.height+4 + 3 + 20, 275*Xrang, kGXScreenHeigh-kNavigationHeight+titleSize.height+4 + 3 + 20);
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeFromSuperview)];
        [self addGestureRecognizer:tapGesture];
        tapGesture.delegate = self;
        [self reloadDrawerData];
        [self.tableView setScrollEnabled:NO];
        [self.tableView reloadData];
    }
    return self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
     // 若为UITableViewCellContentView(即点击了tableViewCell),
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
    // cell 不需要响应 父视图的手势,保证didselect 可以正常
        return NO;
    }
    //默认都需要响应
    return  YES;
}
- (void)reloadDrawerData {
    self.drawerTitleLab.text = [NSString stringWithFormat:@"%@,您好",[AppUserModel sharedAppUserModel].Nick];

    _imageNameArr = @[@[@"draw_home", @"draw_common", @"draw_source", @"draw_mine"], @[@"draw_mail", @"draw_sixin", @"draw_collect",@"draw_note"],@[@"draw_txl", @"draw_set"]];
    _drawerNameArr = @[@[@"首页", @"常用", @"信源", @"我的"],@[@"收件箱", @"私信", @"收藏",@"笔记"],@[@"通讯录", @"设置"]];
}

#pragma mark - UITableViewDataSource && UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _imageNameArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [(NSArray*)_imageNameArr[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54.f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = NSStringFromSelector(_cmd);
    DrawerCell *cell = (DrawerCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[DrawerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    [cell loadCellDataWithTitle:_drawerNameArr[indexPath.section][indexPath.row] imageName:_imageNameArr[indexPath.section][indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.SelectDrawerBlock) {
        self.SelectDrawerBlock(indexPath);
    }
    [self removeFromSuperview];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return [[UIView alloc]initWithFrame:CGRectZero];
    }
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,tableView.width,1)];
    view.backgroundColor = UIColorHex(E1E1E1);
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return  [[UIView alloc]initWithFrame:CGRectZero];
}
#pragma mark - setter
- (UIView *)leftBgView {
    if (!_leftBgView) {
        _leftBgView = [UIView new];
        _leftBgView.frame = CGRectMake(0, 0, 275*Xrang, kGXScreenHeigh);
        _leftBgView.backgroundColor = [UIColor whiteColor];
    }
    return _leftBgView;
}

- (UILabel *)drawerTitleLab {
    if (!_drawerTitleLab) {
        _drawerTitleLab = [UILabel new];
        _drawerTitleLab.textColor = UIColorHex(232323);
        _drawerTitleLab.font = [UIFont systemFontOfSize:18.f];
    }
    return _drawerTitleLab;
}

- (UIView *)blueView {
    if (!_blueView) {
        _blueView = [UIView new];
        _blueView.backgroundColor = UIColorHex(0x3979D3);
    }
    return _blueView;
}

- (TDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[TDTableView alloc] init];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

@end