//
//  KKSaveBankRedPackageOPenedView.m
//  Open
//
//  Created by 雷俊博 on 2021/4/22.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "KKSaveBankRedPackageOPenedView.h"

@interface KKSaveBankRedPackageOPenedView ()<UIGestureRecognizerDelegate>

/** 弹窗主内容view */
@property (nonatomic,strong) UIView   *contentView;

@property (nonatomic, copy)NSString *title;

@property (nonatomic, copy)NSString *number;

@property (nonatomic, copy)NSString *message;

@property (nonatomic, copy)NSString *btnTitle;

@property (nonatomic, copy)KKSaveBankRedPackageOPenedViewBtnBlock block;

@end

@implementation KKSaveBankRedPackageOPenedView

- (instancetype)initWithTitle:(NSString *)title number:(NSString *)number message:(NSString *)message btnTitle:(NSString *)btnTitle block:(KKSaveBankRedPackageOPenedViewBtnBlock)block
{
    self = [super init];
    if (self) {
        self.title = title;
        self.number = number;
        self.message = message;
        self.btnTitle = btnTitle;
        
        self.block = block;
        
        [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:)];
    [self addGestureRecognizer:tap];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, 245*WIDTHRADIU, 290 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
    //self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    
    //设置背景图片
     
    UIImageView* backImg = [[UIImageView alloc]initWithFrame:self.contentView.bounds];
    backImg.image = [UIImage imageNamed:@"package_openbg"];
    backImg.userInteractionEnabled = YES;
    [self.contentView addSubview:backImg];
    //设置标题
    UILabel* lb_title = [[UILabel alloc]init];
    lb_title.textColor = [UIColor colorWithHex:0x4d503e];
    lb_title.font = [UIFont systemFontOfSize:14];
    lb_title.textAlignment = NSTextAlignmentCenter;
    
    //数字
    UILabel* lb_number = [[UILabel alloc]init];
    lb_number.textColor = [UIColor colorWithHex:0xed503a];
    lb_number.font = [UIFont systemFontOfSize:50];
    lb_number.textAlignment = NSTextAlignmentCenter;
    
    
    
    

    
    //message
    UILabel* lb_message = [[UILabel alloc]init];
    lb_message.textColor = [UIColor colorWithHex:0xf8f2bb];
    lb_message.font = [UIFont systemFontOfSize:13];
    lb_message.textAlignment = NSTextAlignmentCenter;
    
    //button
    UIButton* btn_close = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_close setBackgroundImage:[UIImage imageNamed:@"package_btn_bg"] forState:UIControlStateNormal];
    [btn_close setTitle:self.btnTitle forState:UIControlStateNormal];
    [btn_close setTitleColor:[UIColor colorWithHex:0x8e2014] forState:UIControlStateNormal];
    btn_close.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn_close addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    lb_title.text = self.title;
    
    
    NSDictionary *attributesExtra = @{NSFontAttributeName:[UIFont systemFontOfSize:50],//字号12
                                                  NSForegroundColorAttributeName: [UIColor colorWithHex:0xed503a]};
    NSDictionary *attributesPrice = @{NSFontAttributeName:[UIFont systemFontOfSize:17],//字号18
                                                   NSForegroundColorAttributeName: [UIColor colorWithHex:0xed503a]};
    NSAttributedString *attributedString = [self attributedText:@[self.number, @"元"]
                                                     attributeAttay:@[attributesExtra,attributesPrice]];
    
    lb_number.attributedText = attributedString;
    
    lb_message.text = self.message;
    
    
    
    
    
    [backImg addSubview:lb_title];
    [backImg addSubview:lb_number];
    [backImg addSubview:lb_message];
    [backImg addSubview:btn_close];
    
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(36);
        make.centerX.equalTo(backImg);
    }];
    [lb_number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom).offset(25);
        make.centerX.equalTo(backImg);
    }];
   

    [btn_close mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(200 * WIDTHRADIU);
        make.height.offset(45 * WIDTHRADIU);
        make.centerX.equalTo(backImg);
        make.bottom.offset(- 25 * WIDTHRADIU);
    }];
    
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
           make.bottom.equalTo(btn_close.mas_top).offset(-10* WIDTHRADIU);
           make.centerX.equalTo(backImg);
    }];
    
    
  
    
}


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


#pragma mark - 显示弹框
+ (void)showAlertViewWithVC:(UIViewController *)vc Title:(NSString *)title number:(NSString *)number message:(NSString *)message btnTitle:(NSString *)btnTitle block:(KKSaveBankRedPackageOPenedViewBtnBlock)block
{
    KKSaveBankRedPackageOPenedView*baseView = [[KKSaveBankRedPackageOPenedView alloc]initWithTitle:title number:number message:message btnTitle:btnTitle block:block];
   

    [vc.view addSubview:baseView];
}

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


#pragma mark - event response
- (void)confirmBtnClick:(UIButton *)sender
{
    if (self.block) {
        self.block();
    }
    
    [self dismissUI];
}

- (void)tapClick:(UITapGestureRecognizer *)tap {
    [self dismissUI];
}



#pragma mark - 获取单个自定义段落
- (NSMutableAttributedString *)getAttributeWithString:(NSString *)string font:(UIFont*)font  color:(UIColor *)color lineSpacing:(CGFloat)lineSpacing
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    [attributeString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
    [attributeString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
    [attributeString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, string.length)];
    
    return attributeString;
}
#pragma mark - 获取自定义富文本
// 获取带有不同样式的文字内容
//stringArray 字符串数组
//attributeAttay 样式数组
- (NSAttributedString *)attributedText:(NSArray*)stringArray attributeAttay:(NSArray *)attributeAttay{
　// 定义要显示的文字内容
    
    
   
    
    NSString * string = [stringArray componentsJoinedByString:@""];　　//拼接传入的字符串数组
　// 通过要显示的文字内容来创建一个带属性样式的字符串对象
    NSMutableAttributedString * result = [[NSMutableAttributedString alloc] initWithString:string];
        for(NSInteger i = 0; i < stringArray.count; i++){
　　　　// 将某一范围内的字符串设置样式
        [result setAttributes:attributeAttay[i] range:[string rangeOfString:stringArray[i]]];
    }
    // 返回已经设置好了的带有样式的文字
    return [[NSAttributedString alloc] initWithAttributedString:result];
}


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

@end
