//
//  RedPackageOpenedView.m
//  Open
//
//  Created by 雷俊博 on 2020/3/19.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "RedPackageOpenedView.h"
#import "HGOrientationLabel.h"

@interface RedPackageOpenedView ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong)UIView *contentView;

@property (nonatomic, strong)UIImageView* backImg;


@property (nonatomic, strong)UILabel* lb_leftTitle;

@property (nonatomic, strong)UILabel* lb_message;

@property (nonatomic, strong)UIButton* btn_close;

@property (nonatomic, assign)KKRedPackageType type;

@property (nonatomic, copy)NSString *number;

@property (nonatomic, copy)NSString *btnTitle;

@end

@implementation RedPackageOpenedView
#pragma mark - life cycle
- (instancetype)initWithFrame:(CGRect)frame type:(KKRedPackageType)type
{
    self = [super initWithFrame:frame];
    if (self) {
       
        switch (type) {
            case KKRedPackageTypeCoins:
                [self setupUI2];
                break;
            case KKRedPackageTypeMoney:
                [self setupUI];
                break;
                
            default:
                break;
        }
        
    }
    return self;
}

- (instancetype)initWithNumber:(NSString *)number btnTitle:(NSString *)btnTitle Type:(KKRedPackageType)type block:(RedPackageOpenedViewBtnBlock)block
{
    self = [super init];
    if (self) {
        self.type = type;
        self.number = number;
        self.btnTitle = btnTitle;
        self.btnClickBlock = block;
        
        if (self.type == KKRedPackageTypeCoins) {
            
            [self setupCoinsUI];
            
        }else if (self.type == KKRedPackageTypeMoney){
            
            [self setupMoneyUI];
            
        }
        
    }
    return self;
}

- (void)setupUI
{
    
    //设置背景图片
     self.backImg = [[UIImageView alloc]initWithFrame:self.bounds];
    self.backImg.image = [UIImage imageNamed:@"package_openbg"];
    self.backImg.userInteractionEnabled = YES;
    [self addSubview:self.backImg];
    //设置标题
    self.lb_title = [[UILabel alloc]init];
    _lb_title.textColor = [UIColor colorWithHex:0x4d503e];
    _lb_title.font = [UIFont systemFontOfSize:14];
    _lb_title.textAlignment = NSTextAlignmentCenter;
    
    //数字
    _lb_number = [[UILabel alloc]init];
    _lb_number.textColor = [UIColor colorWithHex:0xed503a];
    _lb_number.font = [UIFont systemFontOfSize:50];
    _lb_number.textAlignment = NSTextAlignmentCenter;
    
    
    
    

    
    //message
    _lb_message = [[UILabel alloc]init];
    _lb_message.textColor = [UIColor colorWithHex:0xf8f2bb];
    _lb_message.font = [UIFont systemFontOfSize:13];
    _lb_message.textAlignment = NSTextAlignmentCenter;
    
    //button
    _btn_close = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btn_close setBackgroundImage:[UIImage imageNamed:@"package_btn_bg"] forState:UIControlStateNormal];
    [_btn_close setTitle:@"我知道了" forState:UIControlStateNormal];
    [_btn_close setTitleColor:[UIColor colorWithHex:0x8e2014] forState:UIControlStateNormal];
    _btn_close.titleLabel.font = [UIFont systemFontOfSize:17];
    [_btn_close addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _lb_title.text = @"“恭喜发财，大吉大利”";
    
    
    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:@[@"0.88", @"元"]
                                                     attributeAttay:@[attributesExtra,attributesPrice]];
    
    _lb_number.attributedText = attributedString;
    
    _lb_message.text = @"奖励已发到“我的钱包”";
    
    
    
    
    
    [self.backImg addSubview:self.lb_title];
    [self.backImg addSubview:self.lb_number];
    [self.backImg addSubview:self.lb_message];
    [self.backImg addSubview:self.btn_close];
    
    [self.lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(36);
        make.centerX.equalTo(self.backImg);
    }];
    [self.lb_number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_lb_title.mas_bottom).offset(25);
        make.centerX.equalTo(self.backImg);
    }];
   

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

- (void)setupUI2
{
    //设置背景图片
     self.backImg = [[UIImageView alloc]initWithFrame:self.bounds];
    self.backImg.image = [UIImage imageNamed:@"package_user_bg"];
    self.backImg.userInteractionEnabled = YES;
    [self addSubview:self.backImg];
    
    //设置标题
    self.lb_title = [[UILabel alloc]init];
    _lb_title.textColor = [UIColor colorWithHex:0xfaecca];
    _lb_title.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    _lb_title.textAlignment = NSTextAlignmentCenter;
    _lb_title.numberOfLines = 0;
    NSString*title = @"认证成功奖励\n赠您200金币、免费抽奖机会";
    _lb_title.attributedText = [self getAttributeWithString:title font:[UIFont systemFontOfSize:16 * WIDTHRADIU] color:[UIColor colorWithHex:0xfaecca] lineSpacing:5 * WIDTHRADIU];
    
    
    //赠您金币
    self.lb_leftTitle = [[UILabel alloc]init];
    _lb_leftTitle.textColor = [UIColor colorWithHex:0xef563f];
    _lb_leftTitle.font = [UIFont systemFontOfSize:17 * WIDTHRADIU];
    _lb_leftTitle.textAlignment = NSTextAlignmentCenter;
    _lb_leftTitle.numberOfLines = 0;
    NSString*leftTitle = @"赠您\n金币";
    _lb_leftTitle.attributedText = [self getAttributeWithString:leftTitle font:[UIFont systemFontOfSize:17 * WIDTHRADIU] color:[UIColor colorWithHex:0xef563f] lineSpacing:3 * WIDTHRADIU];
    
    //数字
    _lb_number = [[UILabel alloc]init];
    _lb_number.textColor = [UIColor colorWithHex:0xef563f];
    _lb_number.font = [UIFont boldSystemFontOfSize:50 * WIDTHRADIU];
    _lb_number.textAlignment = NSTextAlignmentCenter;
    
    
    //message
   _lb_message = [[UILabel alloc]init];
   _lb_message.textColor = [UIColor colorWithHex:0x901910];
   _lb_message.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
   _lb_message.textAlignment = NSTextAlignmentCenter;
   _lb_message.text = @"奖励已发放到您的账户";

    
   //button
   _btn_close = [UIButton buttonWithType:UIButtonTypeCustom];
   [_btn_close setBackgroundImage:[UIImage imageNamed:@"package_btn_bg"] forState:UIControlStateNormal];
   [_btn_close setTitle:@"去抽奖" forState:UIControlStateNormal];
   [_btn_close setTitleColor:[UIColor colorWithHex:0x8e2014] forState:UIControlStateNormal];
   _btn_close.titleLabel.font = [UIFont systemFontOfSize:17 * WIDTHRADIU];
   [_btn_close addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
    
   [self.backImg addSubview:self.lb_title];
    [self.backImg addSubview:self.lb_leftTitle];
   [self.backImg addSubview:self.lb_number];
   [self.backImg addSubview:self.lb_message];
   [self.backImg addSubview:self.btn_close];
    
   [self.lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(18 * WIDTHRADIU);
        make.centerX.equalTo(self.backImg);
    }];
    
    
    [self.lb_number setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.lb_number setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.lb_number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backImg.mas_centerX).offset(20 * WIDTHRADIU);
        make.top.offset(95 * WIDTHRADIU);
    }];
    
    [self.lb_leftTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self->_lb_number.mas_left).offset(-6 * WIDTHRADIU);
        make.centerY.equalTo(self.lb_number);
    }];
   

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

- (void)setupCoinsUI
{
    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];
    
    CGFloat contentWidth = 245 * WIDTHRADIU;
    CGFloat contentHeight = 290 * WIDTHRADIU;
    
   
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, contentWidth, contentHeight);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    //背景图片
    UIImageView *bgImg = [UIImageView new];
    bgImg.frame = self.contentView.bounds;
    bgImg.image = [UIImage imageNamed:@"package_user_bg"];
    [self.contentView addSubview:bgImg];
    
    //设置标题
    UILabel *lb_title = [[UILabel alloc]init];
    lb_title.textColor = [UIColor colorWithHex:0xfaecca];
    lb_title.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    lb_title.textAlignment = NSTextAlignmentCenter;
    lb_title.numberOfLines = 0;
    NSString *title = [NSString stringWithFormat:@"认证成功奖励\n赠您%@金币、免费抽奖机会",self.number];
    lb_title.attributedText = [self getAttributeWithString:title font:[UIFont systemFontOfSize:16 * WIDTHRADIU] color:[UIColor colorWithHex:0xfaecca] lineSpacing:5 * WIDTHRADIU];
    [bgImg addSubview:lb_title];
    
    //赠您金币
    UILabel *lb_leftTitle = [[UILabel alloc]init];
    lb_leftTitle.textColor = [UIColor colorWithHex:0xef563f];
    lb_leftTitle.font = [UIFont systemFontOfSize:17 * WIDTHRADIU];
    lb_leftTitle.textAlignment = NSTextAlignmentCenter;
    lb_leftTitle.numberOfLines = 0;
    NSString*leftTitle = @"赠您\n金币";
    lb_leftTitle.attributedText = [self getAttributeWithString:leftTitle font:[UIFont systemFontOfSize:17 * WIDTHRADIU] color:[UIColor colorWithHex:0xef563f] lineSpacing:3 * WIDTHRADIU];
    [bgImg addSubview:lb_leftTitle];
    
    UILabel *lb_number = [[UILabel alloc]init];
    lb_number.textColor = [UIColor colorWithHex:0xef563f];
    lb_number.font = [UIFont boldSystemFontOfSize:50 * WIDTHRADIU];
    lb_number.textAlignment = NSTextAlignmentCenter;
    [bgImg addSubview:lb_number];
    lb_number.text = self.number;
    
    
    //message
    UILabel *lb_message = [[UILabel alloc]init];
    lb_message.textColor = [UIColor colorWithHex:0x901910];
    lb_message.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.text = @"奖励已发放到您的账户";
    [bgImg addSubview:lb_message];
    
    
    //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 * WIDTHRADIU];
    [btn_close addTarget:self action:@selector(close2Click:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn_close];
    
    
    
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(18 * WIDTHRADIU);
        make.centerX.equalTo(bgImg);
    }];
     
     
     [lb_number setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
     [lb_number setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
     [lb_number mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerX.equalTo(bgImg.mas_centerX).offset(20 * WIDTHRADIU);
         make.top.offset(95 * WIDTHRADIU);
     }];
     
     [lb_leftTitle mas_makeConstraints:^(MASConstraintMaker *make) {
         make.right.equalTo(lb_number.mas_left).offset(-6 * WIDTHRADIU);
         make.centerY.equalTo(lb_number);
     }];
    

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

- (void)setupMoneyUI
{
    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];
    
    CGFloat contentWidth = 245 * WIDTHRADIU;
    CGFloat contentHeight = 290 * WIDTHRADIU;
    
   
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, contentWidth, contentHeight);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    //背景图片
    UIImageView *bgImg = [UIImageView new];
    bgImg.frame = self.contentView.bounds;
    bgImg.image = [UIImage imageNamed:@"package_openbg"];
    [self.contentView addSubview:bgImg];
    
    //title
    UILabel *lb_title = [[UILabel alloc]init];
    lb_title.text = @"“恭喜发财，大吉大利”";
    lb_title.textColor = [UIColor colorWithHex:0x4d503e];
    lb_title.font = [UIFont systemFontOfSize:14*WIDTHRADIU];
    lb_title.textAlignment = NSTextAlignmentCenter;
    [bgImg addSubview:lb_title];
    
    //number
    UILabel *lb_number = [[UILabel alloc]init];
    lb_number.textColor = [UIColor colorWithHex:0xed503a];
    lb_number.font = [UIFont systemFontOfSize:50* WIDTHRADIU];
    lb_number.textAlignment = NSTextAlignmentCenter;
    [bgImg addSubview:lb_number];
    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;
    
    
    
    //message
    UILabel *lb_message = [[UILabel alloc]init];
    lb_message.text = @"奖励已发到“我的钱包”";
    lb_message.textColor = [UIColor colorWithHex:0xf8f2bb];
    lb_message.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    lb_message.textAlignment = NSTextAlignmentCenter;
    [bgImg addSubview:lb_message];
    
    //btn_close
    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 * WIDTHRADIU];
    [btn_close addTarget:self action:@selector(close2Click:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn_close];
    
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(36 * WIDTHRADIU);
        make.centerX.equalTo(bgImg);
    }];
    [lb_number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom).offset(25 * WIDTHRADIU);
        make.centerX.equalTo(bgImg);
    }];
    
    [btn_close mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(200 * WIDTHRADIU);
        make.height.offset(45 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.bottom.offset(- 25 * WIDTHRADIU);
    }];
    
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(- 80 * WIDTHRADIU);
        make.centerX.equalTo(bgImg);
    }];
    
}

+ (void)showAlertViewWithVC:(UIViewController *)vc number:(NSString *)number btnTitle:(NSString *)btnTitle type:(KKRedPackageType)type block:(RedPackageOpenedViewBtnBlock)block;
{
    RedPackageOpenedView *openedView = [[RedPackageOpenedView alloc]initWithNumber:number btnTitle:btnTitle Type:type block:block];
    
    [vc.view addSubview:openedView];
}


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

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

#pragma mark - event response
- (void)closeClick:(UIButton *)sender
{
    if (self.btnClickBlock) {
        self.btnClickBlock(YES);
    }
}

- (void)close2Click:(UIButton *)sender
{
    if (self.btnClickBlock) {
        self.btnClickBlock(YES);
    }
    [self dismissUI];
}

- (void)tapClick:(UITapGestureRecognizer *)ges
{
    [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];
}



#pragma mark - getters and setters




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

@end
