//
//  FtCjhxDialog.m
//  Open
//
//  Created by weface on 2023/6/9.
//  Copyright © 2023 QXY. All rights reserved.
//

#import "FtCjhxDialog.h"

@interface FtCjhxDialog ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong)UIView *contentView;
@property (nonatomic, copy)NSString *topTitle;
@property (nonatomic, copy)table01 table01;
@property (nonatomic, copy) table02 table02;
@property (nonatomic, copy)btnYes btnYes;
@property (nonatomic, copy) btnNo btnNo;

@end

@implementation FtCjhxDialog

- (instancetype)initWithTitle:(NSString *)btnTitle text01:(table01)text01 text02:(table02)text02 btnYes:(btnYes)btnYes btnNo:(btnNo)btnNo{
    self = [super init];
    if (self) {
        self.topTitle = btnTitle;
        self.table01 = text01;
        self.table02 = text02;
        self.btnYes = btnYes;
        self.btnNo = btnNo;
        [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.bounds = CGRectMake(0, 0, ScreenWidth/20*17, 50 * WIDTHRADIU*9.5);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 5 * WIDTHRADIU;
    
    //标题title
    UILabel *lb_title = [UILabel new];
    lb_title.text = self.topTitle;
    lb_title.font = [UIFont systemFontOfSize:22 * WIDTHRADIU weight:UIFontWeightMedium];
    lb_title.textAlignment = NSTextAlignmentCenter;
    lb_title.textColor = [UIColor colorWithHex:0x4c4c4c];
    [self.contentView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    //窗体主内容
    NSString *msg=@"为了更好的保护您的权益,在此为您介绍\n在基金开户和交易服务过程中我们将如\n何规范安全的收集、存储以及使用您的\n信息,请您充分了解:为了正常识别手机\n设备和运营商号码、保证您的账号和资金\n安全,需要您授权设备信息和手机号码。";
    UILabel *lb_message = [UILabel new];
    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    style.lineSpacing = 10;
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:msg];
    [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, msg.length)];
    lb_message.attributedText = attrString;
    lb_message.text =msg;
    //lb_message.bounds=CGRectMake(0, 0, ScreenWidth/5*4, ScreenHeight/10*3);
    lb_message.font = [UIFont systemFontOfSize:17 * WIDTHRADIU];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.textColor = [UIColor colorWithHex:0x505050];
    lb_message.lineBreakMode=UILineBreakModeWordWrap;
    lb_message.numberOfLines=0;
    [self.contentView addSubview:lb_message];
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom).offset(10 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    //协议01
    UIButton *text01 = [UIButton buttonWithType:UIButtonTypeCustom];
    [text01 setTitle:@"《创金合信基金电子直销平台隐私政策》" forState:UIControlStateNormal];
    [text01 setTitleColor:[UIColor colorWithHex:0x086cd6] forState:UIControlStateNormal];
    text01.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    text01.backgroundColor = [UIColor whiteColor];
    [text01 addTarget:self action:@selector(text01Click:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:text01];
    [text01 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_message.mas_bottom).offset(18 * WIDTHRADIU);
        make.left.equalTo(self.contentView);
    }];
    
    //协议02
    UIButton *text02 = [UIButton buttonWithType:UIButtonTypeCustom];
    [text02 setTitle:@"《创金合信基金个人信息使用授权书》" forState:UIControlStateNormal];
    [text02 setTitleColor:[UIColor colorWithHex:0x086cd6] forState:UIControlStateNormal];
    text02.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    text02.backgroundColor = [UIColor whiteColor];
    [text02 addTarget:self action:@selector(text02Click:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:text02];
    [text02 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(text01.mas_bottom).offset(12 * WIDTHRADIU);
        make.left.equalTo(self.contentView);
    }];
    
    //同意按钮
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:@"同意" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:17 * WIDTHRADIU];
    confirmBtn.backgroundColor = [UIColor colorWithHex:0xFF3A2E];
    confirmBtn.layer.cornerRadius = 5 * WIDTHRADIU;
    confirmBtn.layer.masksToBounds = YES;
    [confirmBtn addTarget:self action:@selector(btnYes:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(text02.mas_bottom).offset(20 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.offset(ScreenWidth/20*17-40*WIDTHRADIU);
        make.height.offset(50 * WIDTHRADIU);
    }];
    
    //不同意
    UILabel *textNo = [UILabel new];
    textNo.text =@"不同意";
    textNo.font = [UIFont systemFontOfSize:16 * WIDTHRADIU weight:UIFontWeightMedium];
    textNo.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapText03 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnNo:)];
    [textNo addGestureRecognizer:tapText03];
    //text01.textAlignment = NSTextAlignmentCenter;
    textNo.textColor = [UIColor colorWithHex:0x505050];
    [self.contentView addSubview:textNo];
    [textNo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(confirmBtn.mas_bottom).offset(15*WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
}


-(void)text01Click:(UIButton *)sender{
    if (self.table01) {
        self.table01();
    }
    
}



-(void)text02Click:(UIButton *)sender{
    if (self.table02) {
        self.table02();
    }
    
}

-(void)btnYes:(UIButton *)sender{
    if (self.btnYes) {
        self.btnYes();
    }
    [self dismissUI];
    
}
-(void)btnNo:(UITapGestureRecognizer *)tap{
    if (self.btnNo) {
        self.btnNo();
    }
    [self dismissUI];
}
#pragma mark - public methods,
+ (void)showWithTitle:(UINavigationController *) view:(NSString *)btnTitle text01:(table01)text01 text02:(table02)text02 btnYes:(btnYes)btnYes btnNo:(btnNo)btnNo{
    
    FtCjhxDialog *alertView = [[FtCjhxDialog alloc] initWithTitle:btnTitle text01:text01 text02:text02 btnYes:btnYes btnNo:btnNo];
    
    //UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [view.view addSubview:alertView];
}

//点击空白处
- (void)tapClick:(UITapGestureRecognizer *)tap {
    [self dismissUI];
}

#pragma mark -隐藏弹框
- (void)dismissUI{
    [self removeFromSuperview];
}

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