//
//  CaijiShareAlertView.m
//  Open
//
//  Created by 雷俊博 on 2020/6/8.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "CaijiShareAlertView.h"
#import "UIView+frameAdjust.h"

@interface CaijiShareAlertView ()
{
    NSString* _name;
    NSString* _address;
}

@property (nonatomic, strong)UIView* contentView;

@property (nonatomic, strong)UIImageView* topBackRedImg;


@property (nonatomic, copy)CaiJiButtonClickBlock block;

@end

@implementation CaijiShareAlertView
- (instancetype)initWithName:(nonnull NSString *)name Address:(nonnull NSString *)address Block:(CaiJiButtonClickBlock)block
{
    self = [super init];
    if (self) {
        _name = name;
        _address = address;
        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, 290*WIDTHRADIU, 380 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    
    //顶部背景图
    self.topBackRedImg = [[UIImageView alloc]init];
    self.topBackRedImg.image = [UIImage imageNamed:@"Socialsecuritycollection_success_title_normal"];
    [self addSubview:self.topBackRedImg];
    
    [self.topBackRedImg mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerX.equalTo(self.contentView);
           make.top.equalTo(self.contentView.mas_top).offset(- 34 * WIDTHRADIU);
           make.width.offset(310 * WIDTHRADIU);
           make.height.offset(83 * WIDTHRADIU);
       }];
    
    UILabel*lb_title = [[UILabel alloc]init];
    lb_title.text = @"恭喜采集成功";
    lb_title.textColor = [UIColor whiteColor];
    lb_title.textAlignment = NSTextAlignmentCenter;
    lb_title.font = [UIFont boldSystemFontOfSize:24 * WIDTHRADIU];
    [self.topBackRedImg addSubview:lb_title];
    
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.topBackRedImg);
        make.centerY.equalTo(self.topBackRedImg.mas_centerY).offset(-14 * WIDTHRADIU);
    }];
    
     UILabel*lb_name = [[UILabel alloc]init];
     lb_name.text = _name;
     lb_name.textColor = [UIColor colorWithHex:0x4d4d4d];
     lb_name.textAlignment = NSTextAlignmentCenter;
     lb_name.font = [UIFont boldSystemFontOfSize:25 * WIDTHRADIU];
     [self.contentView addSubview:lb_name];
     [lb_name mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.offset(28 * WIDTHRADIU);
         make.centerX.equalTo(self.contentView);
     }];
    
    //虚线
     UIView *dashedView = [[UIView alloc] initWithFrame:CGRectMake(15 * WIDTHRADIU, 69 * WIDTHRADIU, 260 * WIDTHRADIU, 1)];
    dashedView.backgroundColor = [UIColor clearColor];
     [self.contentView addSubview:dashedView];
    
    [self drawLineOfDashByCAShapeLayer:dashedView lineLength:4 lineSpacing:2 lineColor:K_MainColor lineDirection:YES];
   
    //分享APP
    UILabel*lb_shareTitle  = [[UILabel alloc]init];
    lb_shareTitle.text = @"分享APP";
    lb_shareTitle.textColor = [UIColor colorWithHex:0x4d4d4d];
    lb_shareTitle.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    lb_shareTitle.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:lb_shareTitle];
    [lb_shareTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(dashedView.mas_bottom).offset(14 * WIDTHRADIU);
    }];
    
    
    //富文本
    UILabel*lb_shareContent  = [[UILabel alloc]init];
    lb_shareContent.textColor = [UIColor colorWithHex:0x4d4d4d];
    lb_shareContent.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    lb_shareContent.textAlignment = NSTextAlignmentCenter;
    lb_shareContent.numberOfLines = 0;
    [self.contentView addSubview:lb_shareContent];
    [lb_shareContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_shareTitle.mas_bottom).offset(13 * WIDTHRADIU);
        make.left.offset(20 * WIDTHRADIU);
        make.right.offset(- 20 * WIDTHRADIU);
    }];
    NSString* str = [NSString stringWithFormat:@"我们家乡%@可以刷脸进行认证了。足不出村，足不出户；轻松认证，放心领钱！！\n看看生活，科技惠民，科技便民，人工智能走入寻常百姓家。",_address];
    lb_shareContent.attributedText = [self getAttributedStrWithString:str font:[UIFont systemFontOfSize:14 * WIDTHRADIU] color:[UIColor colorWithHex:0x808080] lineSpace:8 * WIDTHRADIU];
    
    UIButton*wxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [wxBtn setImage:[UIImage imageNamed:@"Socialsecuritycollection_success_share_WeChat_normal"] forState:UIControlStateNormal];
    [wxBtn setTitle:@"微信" forState:UIControlStateNormal];
    [wxBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    wxBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    wxBtn.tag = 200;
    [wxBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [wxBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:8* WIDTHRADIU];
    [self.contentView addSubview:wxBtn];
    
    [wxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_shareContent.mas_bottom).offset(15 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView.mas_centerX).offset(-52.5 * WIDTHRADIU);
        make.width.offset(55 * WIDTHRADIU);
        make.height.offset(83 * WIDTHRADIU);
    }];
    
    UIButton*btn_CircleFriends = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn_CircleFriends setImage:[UIImage imageNamed:@"Socialsecuritycollection_success_share_Circleoffriends_normal"] forState:UIControlStateNormal];
    [btn_CircleFriends setTitle:@"朋友圈" forState:UIControlStateNormal];
    [btn_CircleFriends setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    btn_CircleFriends.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    btn_CircleFriends.tag = 201;
    [btn_CircleFriends addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn_CircleFriends layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:8* WIDTHRADIU];
    [self.contentView addSubview:btn_CircleFriends];
    
    [btn_CircleFriends mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_shareContent.mas_bottom).offset(15 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView.mas_centerX).offset(52.5 * WIDTHRADIU);
        make.width.offset(55 * WIDTHRADIU);
        make.height.offset(83 * WIDTHRADIU);
    }];
    
    UIButton* btn_backHome = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_backHome.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    [btn_backHome setTitleColor:K_MainColor forState:UIControlStateNormal];
    [btn_backHome setTitle:@"返回首页" forState:UIControlStateNormal];
    btn_backHome.tag = 202;
    [btn_backHome addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn_backHome];
    
    [btn_backHome mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-17 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.offset(60 * WIDTHRADIU);
        make.height.offset(16 * WIDTHRADIU);
    }];
    

     //左分割线
    UIView* leftLine = [[UIView alloc]init];
    leftLine.backgroundColor = K_MainColor;
    [self.contentView addSubview:leftLine];
    //右分割线
    UIView* rightLine = [[UIView alloc]init];
    rightLine.backgroundColor = K_MainColor;
    [self.contentView addSubview:rightLine];
    
    [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerY.equalTo(btn_backHome);
           make.right.equalTo(btn_backHome.mas_left).offset(-12 * WIDTHRADIU);
           make.width.offset(51 * WIDTHRADIU);
           make.height.offset(1);
    }];
       
    [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerY.equalTo(btn_backHome);
           make.left.equalTo(btn_backHome.mas_right).offset(12 * WIDTHRADIU);
           make.width.offset(51 * WIDTHRADIU);
            make.height.offset(1);
    }];
    
    
    
}

+ (void)showAlertViewWithVC:(UIViewController *)vc name:(nonnull NSString *)name address:(nonnull NSString *)address block:(nonnull CaiJiButtonClickBlock)block
{
    CaijiShareAlertView* redView = [[CaijiShareAlertView alloc]initWithName:name Address:address Block:block];
       
    UIWindow* keyWindow = vc.view.window;
       
   if (keyWindow == nil) {
       keyWindow = [UIApplication sharedApplication].keyWindow;
   }

   [keyWindow addSubview:redView];
}

#pragma mark - private methods
/**
 *  通过 CAShapeLayer 方式绘制虚线
 *
 *  param lineView:       需要绘制成虚线的view
 *  param lineLength:     虚线的宽度
 *  param lineSpacing:    虚线的间距
 *  param lineColor:      虚线的颜色
 *  param lineDirection   虚线的方向  YES 为水平方向， NO 为垂直方向
 **/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [shapeLayer setBounds:lineView.bounds];

    if (isHorizonal) {

        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];

    } else{
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
    }

    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    if (isHorizonal) {
        [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    } else {

        [shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
    }
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);

    if (isHorizonal) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    } else {
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
    }

    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

- (NSAttributedString *)getAttributedStrWithString:(NSString *)str font:(UIFont *)font color:(UIColor *)color lineSpace:(CGFloat)lineSpace
{
    NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc]init];
    paragraph.lineSpacing = lineSpace;
    
    paragraph.firstLineHeadIndent = 27.0f * WIDTHRADIU;
    
    NSAttributedString * attributedStr = [[NSAttributedString alloc]initWithString:str attributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:paragraph}];
    return attributedStr;
}



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


#pragma mark - event response
- (void)tapClick:(UITapGestureRecognizer *)ges
{
    [self dismissUI];
}
- (void)btnClick:(UIButton *)sender
{
    CaiJiShareButtonType type = CaiJiShareButtonTypeBack;
    switch (sender.tag) {
        case 200:
            type = CaiJiShareButtonTypeWX;
            break;
        case 201:
            type = CaiJiShareButtonTypeFriend;
            break;
        case 202:
            type = CaiJiShareButtonTypeBack;
            break;
            
        default:
            break;
    }
    self.block(type);
    [self dismissUI];
}

#pragma mark - getters and settters


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

@end
