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

#import "FindPackageAlertView.h"

@interface FindPackageAlertView ()

//背景蒙层视图
@property (nonatomic, strong)UIView *backgroundView;

@property (nonatomic, strong)UIView *contentView;

@property (nonatomic, strong)UIImageView *img_redPackage;

@property (nonatomic, strong)UILabel *lb_message1;


@property (nonatomic, strong)UILabel *lb_message2;

@property (nonatomic, strong)UIButton *btn_confirm;

@end

@implementation FindPackageAlertView
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.frame =ScreenBounds;
    [self addSubview:self.backgroundView];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(-40 * WIDTHRADIU);
        make.width.offset(258 * WIDTHRADIU);
        make.height.offset(268 * WIDTHRADIU);
    }];
    [self.img_redPackage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.width.height.equalTo(self.contentView);
    }];
    [self.lb_message1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(120 * WIDTHRADIU);
        make.centerX.equalTo(self.img_redPackage);
        make.height.offset(26 * WIDTHRADIU);
    }];
    [self.lb_message2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lb_message1.mas_bottom).offset(20 * WIDTHRADIU);
        make.centerX.equalTo(self.img_redPackage);
        make.height.offset(13 * WIDTHRADIU);
    }];
    
    [self.btn_confirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lb_message2.mas_bottom).offset(20 * WIDTHRADIU);
        make.height.offset(44 * WIDTHRADIU);
        make.centerX.equalTo(self.lb_message2);
        make.width.offset(220 * WIDTHRADIU);
    }];
    
}

#pragma mark - private methods
/**
 *  @brief  生成渐变色图片
 *
 *  @param  bounds  图片的大小
 *  @param  colors      渐变颜色组
 *  @param  gradientType     渐变方向
 *
 *  @return 图片
 */
- (UIImage*)gradientImageWithBounds:(CGRect)bounds andColors:(NSArray*)colors andGradientType:(GradientDirection)gradientType{
    NSMutableArray *ar = [NSMutableArray array];
    
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    //UIGraphicsBeginImageContextWithOptions(bounds.size, YES, 1);
    UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
    format.opaque = YES;
    format.scale = 1;
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:bounds.size format:format];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint startPt =  CGPointMake(0.0, 0.0);
    CGPoint endPt =  CGPointMake(0.0, 0.0);
    
    switch (gradientType) {
        case GradientDirectionTopToBottom:
            startPt= CGPointMake(0.0, 0.0);
            endPt= CGPointMake(0.0, bounds.size.height);
            break;
        case GradientDirectionLeftToRight:
            startPt = CGPointMake(0.0, 0.0);
            endPt = CGPointMake(bounds.size.width, 0.0);
            break;
        case GradientDirectionBottomToTop:
            startPt = CGPointMake(0.0, bounds.size.height);
            endPt = CGPointMake(0.0, 0.0);
            break;
        case GradientDirectionRightToLeft:
            startPt = CGPointMake(bounds.size.width, 0.0);
            endPt = CGPointMake(0, 0.0);
            break;
    }
    CGContextDrawLinearGradient(context, gradient, startPt, endPt, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark -
+ (void)showAlertView
{
    FindPackageAlertView *view = [[FindPackageAlertView alloc]init];
    [view showWithAnimation:YES];
}


#pragma mark - event response
- (void)didTapBackgroundView:(UITapGestureRecognizer *)tap
{
     [self dismissWithAnimation:YES];
}

- (void)btnClick:(UIButton *)sender
{
    [self dismissWithAnimation:YES];
}


#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation
{
    
    //1.获取当前应用的主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview: self];
   
    [self addAnimation];
}

-(void)addAnimation
{
    self.contentView.alpha=0;
    
    
    self.contentView.transform=CGAffineTransformMakeScale(0.02, 0.02);
    
    [UIView animateWithDuration:0.4f animations:^{
        self.contentView.transform=CGAffineTransformMakeScale(1.0, 1.0);
        self.contentView.alpha=1;
    }];
    
    
}

- (void)dismissWithAnimation:(BOOL)animation
{
    [self.lb_message1 removeFromSuperview];
    [self.lb_message2 removeFromSuperview];
    [self.btn_confirm removeFromSuperview];
    [self.img_redPackage removeFromSuperview];
    [self.contentView removeFromSuperview];
    [self.backgroundView removeFromSuperview];
    [self removeFromSuperview];
    
    
    self.lb_message1 = nil;
    self.lb_message2 = nil;
    self.btn_confirm = nil;
    self.img_redPackage = nil;
    self.contentView = nil;
    self.backgroundView = nil;
   
    
}


#pragma mark - getters and setters
- (UIView *)backgroundView
{
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:ScreenBounds];
        _backgroundView.backgroundColor = [UIColor blackColor] ;
        _backgroundView.alpha = 0.3f ;
        _backgroundView.userInteractionEnabled = YES;
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
        [_backgroundView addGestureRecognizer:myTap];
    }
    return _backgroundView;
}

- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = [UIColor clearColor];
        [_contentView addSubview:self.img_redPackage];
        [_contentView addSubview:self.lb_message1];
        [_contentView addSubview:self.lb_message2];
        [_contentView addSubview:self.btn_confirm];
    }
    return _contentView;
}

- (UIImageView *)img_redPackage
{
    if (!_img_redPackage) {
        _img_redPackage = [[UIImageView alloc]init];
        _img_redPackage.image = [UIImage imageNamed:@"findAlertBg"];
    }
    return _img_redPackage;
}

- (UILabel *)lb_message1
{
    if (!_lb_message1) {
        _lb_message1 = [[UILabel alloc]init];
        _lb_message1.text = @"还差一点就能领取";
        _lb_message1.textColor = [UIColor whiteColor];
        _lb_message1.font = [UIFont boldSystemFontOfSize:26 * WIDTHRADIU];
        _lb_message1.textAlignment = NSTextAlignmentCenter;
    }
    return _lb_message1;
}

- (UILabel *)lb_message2
{
    if (!_lb_message2) {
        _lb_message2 = [[UILabel alloc]init];
        _lb_message2.text = @"最低满5000步才能领取奖励";
        _lb_message2.textColor = [UIColor whiteColor];
        _lb_message2.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    }
    return _lb_message2;
}

- (UIButton *)btn_confirm
{
    if (!_btn_confirm) {
        _btn_confirm = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_confirm.frame = CGRectMake(0, 0, 220 * WIDTHRADIU, 44 * WIDTHRADIU);
        [_btn_confirm setTitle:@"我知道了" forState:UIControlStateNormal];
        [_btn_confirm setTitleColor:[UIColor colorWithHex:0xca2f13] forState:UIControlStateNormal];
        _btn_confirm.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
        _btn_confirm.backgroundColor = [UIColor colorWithHex:0xffd27c];
        _btn_confirm.layer.cornerRadius = 22 * WIDTHRADIU;
        _btn_confirm.layer.masksToBounds = YES;
        
       UIImage*bgImage = [self gradientImageWithBounds:_btn_confirm.bounds andColors:@[[UIColor colorWithHex:0xfac119],[UIColor colorWithHex:0xfad31d]] andGradientType:GradientDirectionLeftToRight];
        [_btn_confirm setBackgroundImage:bgImage forState:UIControlStateNormal];
        [_btn_confirm addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _btn_confirm;
}

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

@end
