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

#import "RedPackageView.h"
#import "RedPackageUNOpenView.h"
#import "RedPackageOpenedView.h"

@interface RedPackageView ()

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

@property (nonatomic, strong)RedPackageUNOpenView *unOpenView;

@property (nonatomic, strong)RedPackageOpenedView *openedView;

@property (nonatomic, copy)NSString* money;

@property (nonatomic, assign)KKRedPackageType type;

@property (nonatomic, assign)RedPackageFrom fromType;

@property (nonatomic, copy)clickRedPackageBtnBlock block;

@end

@implementation RedPackageView

- (instancetype)initWithMoney:(NSString *)money type:(KKRedPackageType)type  from:(RedPackageFrom)fromType block:(nonnull clickRedPackageBtnBlock)block
{
    self = [super init];
    if (self) {
        self.money = money;
        self.type = type;
        self.fromType = fromType;
        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 whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    WeakSelf
    self.unOpenView = [[RedPackageUNOpenView alloc]initWithFrame:self.contentView.bounds type:self.fromType];
    self.unOpenView.btnClickBlock = ^(BOOL success) {
        [weakSelf showUI];
    };
    
    self.openedView = [[RedPackageOpenedView alloc]initWithFrame:self.contentView.bounds type:self.type];
    
    if (self.type == KKRedPackageTypeMoney) {
        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.money, @"元"]
                                                            attributeAttay:@[attributesExtra,attributesPrice]];
        
        self.openedView.lb_number.attributedText = attributedString;
    }else if (self.type == KKRedPackageTypeCoins){
        self.openedView.lb_number.text = self.money;
        NSString*title = [NSString stringWithFormat:@"认证成功奖励\n赠您%@金币、免费抽奖机会",self.money];
           self.openedView.lb_title.attributedText = [self getAttributeWithString:title font:[UIFont systemFontOfSize:16 * WIDTHRADIU] color:[UIColor colorWithHex:0xfaecca] lineSpacing:5 * WIDTHRADIU];
    }
    
    self.openedView.btnClickBlock = ^(BOOL success) {
           weakSelf.block(success);
           [weakSelf dismissUI];
       };
    [self.contentView addSubview:self.openedView];
    [self.contentView addSubview:self.unOpenView];
    
}

- (void)showUI
{
    [UIView transitionFromView:self.unOpenView toView:self.openedView duration:0.5f options:UIViewAnimationOptionTransitionFlipFromRight completion:^(BOOL finished) {
                     NSLog(@"翻转到了列表页面");
     }];
}

#pragma mark - public methods
+ (void)showMoneyWithVC:(UIViewController *)vc money:(NSString *)money type:(KKRedPackageType)type from:(RedPackageFrom)fromType block:(clickRedPackageBtnBlock)block
{
    RedPackageView* redView = [[RedPackageView alloc]initWithMoney:money type:type from:fromType  block:block];
    
        UIWindow* keyWindow = vc.view.window;
    
        if (keyWindow == nil) {
            keyWindow = [UIApplication sharedApplication].keyWindow;
        }

    [keyWindow addSubview:redView];
}


- (void)dismissUI
{
     [self removeFromSuperview];
}


#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 - 获取单个自定义段落
- (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;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
