//
//  SaveWelfareAlertView.m
//  Open
//
//  Created by 雷俊博 on 2020/9/4.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "SaveWelfareAlertView.h"
#import "UIImageView+WebCache.h"

#import <AssetsLibrary/AssetsLibrary.h>

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

@property (nonatomic, strong)UIImageView* qrImg;

@property (nonatomic, strong)UIButton* saveBtn;

@end

@implementation SaveWelfareAlertView
- (instancetype)init
{
    self = [super init];
    if (self) {
        [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, 295*WIDTHRADIU, 360 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    
    //顶部图片
    UIImageView*topImg = [[UIImageView alloc]init];
    topImg.image = [UIImage imageNamed:@"save_fulibg"];
    topImg.frame = CGRectMake(0, 0, 295*WIDTHRADIU, 80 * WIDTHRADIU);
    [self.contentView addSubview:topImg];
    
    //标题
    UILabel*lb_title = [UILabel new];
    lb_title.text = @"恭喜获得福利领取资格";
    lb_title.textColor = [UIColor whiteColor];
    lb_title.font = [UIFont boldSystemFontOfSize:18 * WIDTHRADIU];
    [topImg addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20 * WIDTHRADIU);
        make.top.offset(18 * WIDTHRADIU);
    }];
    
    //message
    UILabel*lb_message = [UILabel new];
    lb_message.text = @"提前领取，福利会失效";
    lb_message.textColor = [UIColor whiteColor];
    lb_message.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    [topImg addSubview:lb_message];
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20 * WIDTHRADIU);
        make.top.equalTo(lb_title.mas_bottom).offset(8 * WIDTHRADIU);
    }];
    
    
    //提示
    UILabel*  lb_tip = [UILabel new];
    lb_tip.text = @"保存二维码，微信添加\"福利君\"";
    lb_tip.textColor = [UIColor colorWithHex:0x4d4d4d];
    lb_tip.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    [self.contentView addSubview:lb_tip];
    [lb_tip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20 * WIDTHRADIU);
        make.top.equalTo(topImg.mas_bottom).offset(14 * WIDTHRADIU);
    }];
    
    
    UIImageView* qrImg = [[UIImageView alloc]init];
    qrImg.image = [UIImage imageNamed:@"save_fuliQr"];
    [qrImg sd_setImageWithURL:[NSURL URLWithString:@"http://i.rensheju.com/QRCode/JoinCode.png"] placeholderImage:[UIImage imageNamed:@"save_fuliQr"] options:SDWebImageRefreshCached];
    [self.contentView addSubview:qrImg];
    
    self.qrImg = qrImg;
    
    [qrImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(lb_tip.mas_bottom).offset(12 * WIDTHRADIU);
        make.width.height.offset(155 * WIDTHRADIU);
    }];
    
    UIButton* saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn setTitle:@"保存到相册" forState:UIControlStateNormal];
    [saveBtn  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setImage:[UIImage imageNamed:@"save_photoicon"] forState:UIControlStateNormal];
    [saveBtn setImage:[UIImage imageNamed:@"save_photoicon"] forState:UIControlStateHighlighted];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    saveBtn.backgroundColor = [UIColor colorWithHex:0x1683e5];
    saveBtn.layer.cornerRadius = 22.5 * WIDTHRADIU;
    [self.contentView addSubview:saveBtn];
    self.saveBtn = saveBtn;
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(225 * WIDTHRADIU);
        make.height.offset(45 * WIDTHRADIU);
        make.top.equalTo(qrImg.mas_bottom).offset(18 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    
    
    
    
    
    
    
    //关闭按钮
    UIButton*closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"save_close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_top).offset(-20 * WIDTHRADIU);
        make.left.equalTo(self.contentView.mas_right).offset(-40 * WIDTHRADIU);
        make.width.height.offset(45 * WIDTHRADIU);
    }];
    
    
}

#pragma mark - 显示弹框
+ (void)showAlertViewWithVC:(UIViewController *)vc
{
    SaveWelfareAlertView*baseView = [[SaveWelfareAlertView alloc]init];
    UIWindow* keyWindow = vc.view.window;
    
    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }

    [keyWindow addSubview:baseView];
}

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

#pragma mark - event response
- (void)saveBtnClick:(UIButton *)sender
{
    sender.userInteractionEnabled = NO;
   UIImage*saveImage = [self captureImageFromView:self.contentView];
    [self saveImageToPhotos:saveImage];
    [self dismissUI];
}
//关闭
- (void)closeBtnClick:(UIButton *)sender
{
    [self dismissUI];
}

#pragma mark - 保存回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    self.saveBtn.userInteractionEnabled = YES;
    if (error) {
         [SVProgressHUD showSuccessWithStatus:@"保存失败"];
        
    }else{
       [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
    
}


#pragma mark - private methods
-(UIImage *)captureImageFromView:(UIView *)view
{
    
    //UIGraphicsBeginImageContextWithOptions(view.frame.size,NO, 0);
    UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
    format.opaque = NO;
    format.scale = 0;
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:view.frame.size format:format];
    [[UIColor clearColor] setFill];

    [[UIBezierPath bezierPathWithRect:view.bounds] fill];

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    [view.layer renderInContext:ctx];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
  
}

- (void)saveImageToPhotos:(UIImage *)image {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
}

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

@end
