//
//  InviteRegulationView.m
//  Open
//
//  Created by 雷俊博 on 2019/9/16.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

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

@interface InviteRegulationView ()

/** 弹窗主内容view */
@property (nonatomic,strong) UIView   *contentView;
/** 弹窗标题 */
@property (nonatomic,copy)   NSString *title;
/** 弹窗message */
@property (nonatomic,copy)   NSString *message;
/** message label */
@property (nonatomic,strong) UILabel  *messageLabel;
/** 按钮title */
@property (nonatomic,copy)   NSString *buttonTitle;
/** 关闭按钮 */
@property (nonatomic,strong) UIButton *btn_close;


@end

@implementation InviteRegulationView
#pragma mark - life cycle
/**
 申报异常弹窗的构造方法
 
 @param title 弹窗标题
 @param message 弹窗message
 @param delegate 确定代理方
 @param leftButtonTitle 左边按钮的title
 @param rightButtonTitle 右边按钮的title
 @return 一个申报异常的弹窗
 */
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate ButtonTitle:(NSString *)buttonTitle
{
    self = [super init];
    if (self) {
        self.title = title;
        self.message = message;
        self.buttonTitle = buttonTitle;
        [self setupUI];
    }
    return self;
}

#pragma mark - UI搭建
/** UI搭建 */
- (void)setupUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.frame = CGRectMake((ScreenWidth - 245) / 2, (ScreenHeight - 325) / 2, 245, 325);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15;
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.contentView.width, 22)];
    [self.contentView addSubview:titleLabel];
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor colorWithHex:0x4d4d4d];
    titleLabel.text = self.title;
    
    //关闭按钮
    UIButton *btn_close = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_close.frame = CGRectMake(self.contentView.width - 40, 0, 40, 40);
    [self.contentView addSubview:btn_close];
    [btn_close setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [btn_close addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    
    //弹框滑动scrollview
    
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(15, 42, self.contentView.width - 30, 238.5)];
    textView.textColor = [UIColor colorWithHex:0x808080];
    textView.font = [UIFont boldSystemFontOfSize:14.0];
    textView.editable = NO;
    [self.contentView addSubview:textView];
//    NSString *str = @"1、每天首次分享可获得100金币；\n2、好友在分享页面输入手机号后，下载注册看看生活，你和好友各得500金币，仅限新用户（若在分享页面输入手机号后未下载注册“看看生活”，奖励无法发放）；\n3、已成功邀请的好友会在“我的好友”中显示；\n4、每个用户只能被邀请一次，仅限邀请20人；\n5、邀请家人、朋友、同学、同事或分享微信群成功率更高；\n6、本活动最终解释权归看看生活所有。";
   
    textView.attributedText = [self getAttributedStringWithString:self.message lineSpace:8];
    

    
    //line
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.contentView.height - 44.5, self.contentView.width, 0.5)];
    [self.contentView addSubview:lineView];
    lineView.backgroundColor = [UIColor colorWithHex:0xcccccc];
    
    // 取消按钮
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.contentView.height - 44, self.contentView.width, 44)];
    [self.contentView addSubview:cancelButton];
    [cancelButton setTitle:self.buttonTitle forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorWithHex:0xec2f1c] forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    cancelButton.layer.cornerRadius = 6;
    [cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 弹窗展示/隐藏

/** 弹出此弹窗 */
- (void)show {
    // 出场动画
    self.alpha = 0;
    self.contentView.transform = CGAffineTransformScale(self.contentView.transform, 1.3, 1.3);
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformIdentity;
    }];
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
}

/** 移除此弹窗 */
- (void)dismiss {
    [self removeFromSuperview];
}


#pragma mark - event response
/** 取消按钮点击 */
- (void)cancelButtonClicked {
    if ([self.delegate respondsToSelector:@selector(CQDeclareAlertView:clickedButtonAtIndex:)]) {
        [self.delegate CQDeclareAlertView:self clickedButtonAtIndex:1];
    }
    [self dismiss];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    CGPoint tempoint = [self.contentView convertPoint:point fromView:self];
    
    if (CGRectContainsPoint(self.contentView.bounds, tempoint)) {
        
    }else{
        [self dismiss];
    }
    
    
}


-(NSAttributedString *)getAttributedStringWithString:(NSString *)string lineSpace:(CGFloat)lineSpace {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace; // 调整行间距
    NSRange range = NSMakeRange(0, [string length]);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    [attributedString addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor colorWithHex:0x808080]
     
                          range:range];
    [attributedString addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:13]
     
                          range:range];
    

    return attributedString;
}

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

@end
