//
//  PrivateAlertView.m
//  Open
//
//  Created by 雷俊博 on 2020/1/7.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "PrivateAlertView.h"
#import "AgreeContentCell.h"
#import "AgreeFootView.h"
#import "UIView+frameAdjust.h"

#import <YYText.h>

@interface PrivateAlertView ()<UITableViewDelegate,UITableViewDataSource,AgreeFootViewDelegate>
{
    NSString* agreementText;
    CGFloat margin;
}

@property (nonatomic, strong)UITableView* contentTableView;

@property (nonatomic, strong)NSArray<NSString *>* contents;

@property (nonatomic, strong)UILabel* titleLabel;

@property (nonatomic, strong)UIButton* disAgreeBtn;

@property (nonatomic, strong)UIButton* agreeBtn;

@end

@implementation PrivateAlertView
- (instancetype)initWithFrame:(CGRect)frame contents:(NSArray*)contents
{
    self = [super initWithFrame:frame];
    if (self) {
        margin = 15;
        self.titleLabel = [[UILabel alloc]init];
        self.disAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.contents = contents;
        agreementText = @"点击同意即表示您已阅读并同意《看看生活用户协议》与《看看生活隐私政策》";
        [self setUI];
    }
    return self;
}
- (void)setUI
{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.titleLabel];
    [self addSubview:self.contentTableView];
    [self addSubview:self.disAgreeBtn];
    [self addSubview:self.agreeBtn];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(self->margin);
        make.right.offset(-self->margin);
    }];
    
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont boldSystemFontOfSize:18];
    _titleLabel.text = @"看看生活个人信息保护指引";
    _titleLabel.textColor = [UIColor blackColor];
    
    [self.contentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_titleLabel.mas_bottom).offset(self->margin);
        make.left.offset(self->margin);
        make.right.offset(-self->margin);
        make.bottom.offset(-100);
    }];
    
    _agreeBtn.backgroundColor = K_MainColor;
    [_agreeBtn setTitle:@"同意并使用" forState:UIControlStateNormal];
    _agreeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_agreeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _agreeBtn.layer.cornerRadius = 4;
    [_agreeBtn addTarget:self action:@selector(agreeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentTableView.mas_bottom).offset(self->margin);
        make.right.offset(-self->margin);
        make.left.offset(self->margin);
        make.height.offset(40);
    }];
    
    [_disAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.agreeBtn.mas_bottom);
        make.left.offset(self->margin);
        make.height.offset(40);
        make.right.offset(-self->margin);
    }];
    
    _disAgreeBtn.backgroundColor = [UIColor whiteColor];
    [_disAgreeBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    [_disAgreeBtn setTitle:@"暂不同意" forState:UIControlStateNormal];
     _disAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [_disAgreeBtn addTarget:self action:@selector(disAgreeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
   // _disAgreeBtn.layer.cornerRadius = 5;
    
    
    
    
    //_agreeBtn.layer.cornerRadius = 5;
    
}



#pragma mark - AgreeFootViewDelegate
- (void)userProtocolClick
{
    if (self.userProtocol) {
        self.userProtocol();
    }
}

- (void)privateProtocolClick
{
    if (self.privateProtocol) {
           self.privateProtocol();
       }
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contents.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AgreeContentCell* cell = [tableView dequeueReusableCellWithIdentifier:@"AgreeContentCell"];
    if (indexPath.row !=2) {
        
        NSMutableAttributedString* text = [[NSMutableAttributedString alloc]initWithString:self.contents[indexPath.row]];
        text.yy_lineSpacing = 5;
        text.yy_font = [UIFont systemFontOfSize:15];
        text.yy_color = [UIColor colorWithHex:0x4d4d4d];
        
        cell.contentLabel.attributedText = text;
    }else{
        cell.contentLabel.text = @"";
    }
    
    if (indexPath.row == 0) {
        cell.contentLabel.textColor = [UIColor blackColor];
        cell.contentLabel.font = [UIFont boldSystemFontOfSize:16];
    }else if (indexPath.row == 2){
        NSMutableAttributedString* text = [[NSMutableAttributedString alloc]initWithString:self.contents[2]];
        text.yy_lineSpacing = 5;
        text.yy_font = [UIFont systemFontOfSize:15];
        text.yy_color = [UIColor colorWithHex:0x4d4d4d];
        
        [text yy_setTextHighlightRange:NSMakeRange(34, 10) color:K_MainColor backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
             if (self.userProtocol) {
                 self.userProtocol();
             }
           }];
           
           
           [text yy_setTextHighlightRange:NSMakeRange(45, 10) color:K_MainColor backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {

               if (self.privateProtocol) {
                   self.privateProtocol();
               }
           }];
        cell.contentLabel2.attributedText = text;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        return 130;
    }
    if (indexPath.row == 0) {
        return 30;
    }
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    AgreeFootView *footView = [[AgreeFootView alloc]initWithFrame:CGRectMake(0, 0, self.width, 60)];
    footView.delegate = self;
    
    return footView;
}

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


#pragma mark - event response
- (void)disAgreeBtnAction:(UIButton *)sender
{
    if (self.disAgreeCallBack) {
        self.disAgreeCallBack();
    }
}
- (void)agreeBtnAction:(UIButton *)sender
{
    if (self.agreeCallBack) {
        self.agreeCallBack();
    }
}

#pragma mark - getters and setters
- (UITableView *)contentTableView
{
    if (!_contentTableView) {
        _contentTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,CGFLOAT_MIN)];
        _contentTableView.tableHeaderView = headView;
        
        _contentTableView.backgroundColor = [UIColor whiteColor];
        _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _contentTableView.delegate = self;
        _contentTableView.dataSource = self;
        _contentTableView.showsVerticalScrollIndicator = NO;
        _contentTableView.estimatedRowHeight = 60;
    
        [_contentTableView registerClass:[AgreeContentCell class] forCellReuseIdentifier:@"AgreeContentCell"];
    }
    return _contentTableView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end




