//
//  BCNewActivitySheetView.m
//  Open
//
//  Created by 雷俊博 on 2020/12/10.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "BCNewActivitySheetView.h"
#import "BCNewAcivityCell.h"

@interface BCNewActivitySheetView ()<UIGestureRecognizerDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UIView* contentView;

@property (nonatomic, strong)UITableView* tableView;

@property (nonatomic, strong)BCNewActivityCriterionModel* model;

@property (nonatomic,copy)BCNewActivitySheetViewBlock  block;

@end

@implementation BCNewActivitySheetView
- (instancetype)initWithModel:(nonnull BCNewActivityCriterionModel *)model block:(nonnull BCNewActivitySheetViewBlock)blcok{
    self = [super init];
    if (self) {
        self.model = model;
        self.block = blcok;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.frame = CGRectMake(0, ScreenHeight, self.bounds.size.width, 300 +(TAB_BAR_HEIGHT - 49));
    [self addSubview:self.contentView];
    
    
    
    
    
   
    
    //title
    UILabel* lb_title = [UILabel new];
    lb_title.text = @"我的福利";
    lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
    lb_title.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.centerX.equalTo(self.contentView);
        make.height.offset(40);
    }];
    
    
    UIButton* closeBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = CGRectMake(self.bounds.size.width - 50, 0, 50, 40);
    [closeBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:closeBtn];
    
    
    
    UITableView* tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, self.bounds.size.width, 260) style:UITableViewStyleGrouped];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.dataSource = self;
    tableView.delegate = self;
    [tableView registerNib:[UINib nibWithNibName:@"BCNewAcivityCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    tableView.estimatedSectionFooterHeight = 10;
    tableView.estimatedSectionHeaderHeight = 0;
    tableView.estimatedRowHeight = 50;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:tableView];
    
    
    
    
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.model.criterion.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BCNewActivityCriterionSubModel* model = self.model.criterion[indexPath.section];
    
    BCNewAcivityCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if ([model.status isEqualToString:@"1"]) {
        cell.selectState = YES;
    }else{
        cell.selectState = NO;
    }
    
    
    NSString *priceStr = model.aWardInfo;
    NSString *amountStr = [NSString stringWithFormat:@"¥%@",model.aWardInfo];
    NSRange range = [amountStr rangeOfString:priceStr];
    NSMutableAttributedString *finalStr = [[NSMutableAttributedString alloc] initWithString:amountStr];
    // 添加需要改变的字体的属性
    NSDictionary *dict = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:38]};
    [finalStr addAttributes:dict range:range];
    
    cell.lb_amount.attributedText = finalStr;
    
    cell.lb_amountDescri.text = [NSString stringWithFormat:@"满%@元送红包",model.minAmount];
    
    cell.lb_message2.text = [NSString stringWithFormat:@"有效期: %@~%@",[self.model.startTime substringToIndex:10],[self.model.endTime substringToIndex:10]];
    
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}




#pragma mark - public methods
+ (void)showWithVC:(UIViewController *)vc model:(nonnull BCNewActivityCriterionModel *)model block:(nonnull BCNewActivitySheetViewBlock)blcok {
    BCNewActivitySheetView*presentView = [[BCNewActivitySheetView alloc]initWithModel:model block:blcok];
    UIWindow* keyWindow = vc.view.window;
          
      if (keyWindow == nil) {
          keyWindow = [UIApplication sharedApplication].keyWindow;
      }

      [keyWindow addSubview:presentView];
      [presentView showView];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.contentView]) {
        return NO;
    }
    
    return YES;
}

#pragma mark - event response
- (void)tapClick:(UITapGestureRecognizer *)ges {
    
    
    [self hideView];
}
- (void)signBtnClick:(UIButton *)sender {
    [self hideView];
    if (self.block) {
        self.block();
    }
}

- (void)closeBtnClick:(UIButton *)sender {
    [self hideView];
}


#pragma mark - 隐藏或显示
- (void)showView
{
    [UIView animateWithDuration:0.2 animations:^{
        self.contentView.frame = CGRectMake(0, ScreenHeight - 300 -(TAB_BAR_HEIGHT - 49) , SCREEN_WIDTH, 300 +(TAB_BAR_HEIGHT - 49));
    } completion:^(BOOL finished) {
        
    }];
}

//关闭界面
- (void)hideView{

    [UIView animateWithDuration:0.1 animations:^{
        self.contentView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 300 +(TAB_BAR_HEIGHT - 49));
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
