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

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

#import "Constants.h"

@interface InsuranceAlertView ()

/** 弹窗主内容view */
@property (nonatomic,strong) UIView   *contentView;
/** 弹窗标题 */
@property (nonatomic,copy)   NSString *title;
/** 弹窗message */
@property (nonatomic,copy)   NSString *message;

@end

@implementation InsuranceAlertView
#pragma mark - 构造方法
- (instancetype)init
{
    self = [super init];
    if (self) {
        //UI搭建
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.frame = CGRectMake((SCREEN_WIDTH - 270) / 2, (SCREEN_HEIGHT - 297) / 2, 270, 297);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15;
    
    UIImageView *topBackImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.width, 97)];
    topBackImg.image = [UIImage imageNamed:@"img_insurance_alertView_back"];
    [self.contentView addSubview:topBackImg];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, topBackImg.width, 20)];
    [topBackImg addSubview:titleLabel];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"房屋险领取提示";
    //message
    UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, titleLabel.maxY + 8, topBackImg.width, 12)];
    [topBackImg addSubview:messageLabel];
    messageLabel.font = [UIFont systemFontOfSize:12];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.textColor = [UIColor whiteColor];
    messageLabel.text = @"房屋险领取提示";
    
    //contentMessage
    
    UITextView *contentTV = [[UITextView alloc]initWithFrame:CGRectMake(8, topBackImg.maxY , topBackImg.width-16, 150)];
    [self.contentView addSubview:contentTV];
    contentTV.font = [UIFont systemFontOfSize:15];
    contentTV.textAlignment = NSTextAlignmentCenter;
    contentTV.editable = NO;
    contentTV.textColor = [UIColor colorWithHex:0x808080];
    contentTV.scrollEnabled = NO;
    
    
     NSString *text = @"       此次房屋险需要您预先垫付，后期添加客服微信'kankanrenzheng'；需提交您填写的投保信息及付款截图给客服，待客服人员审核后会以微信红包形式返还您30元(仅退一份)。";
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc]initWithString:text];
    
    
    // 创建NSMutableParagraphStyle实例
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5;       //字间距5
    paragraphStyle.paragraphSpacing = 12;       //行间距是20
    paragraphStyle.alignment = NSTextAlignmentJustified;   //对齐方式为居中对齐
    
    [attributeStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
    [attributeStr addAttribute:NSFontAttributeName
     
                         value:[UIFont systemFontOfSize:15]
     
                         range:NSMakeRange(0 , text.length)];
   // [attributeStr addAttribute:<#(nonnull NSAttributedStringKey)#> value:<#(nonnull id)#> range:<#(NSRange)#>]
     [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0x808080] range:NSMakeRange(0, text.length)];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfe5733] range:NSMakeRange(29, 14)];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfe5733] range:NSMakeRange(46, 2)];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfe5733] range:NSMakeRange(52, 12)];
     [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0xfe5733] range:NSMakeRange(81, 6)];
    
    
    contentTV.attributedText = attributeStr;
    
    
    UIView *HLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.contentView.height - 45.5, self.contentView.width, 0.5)];
    [self.contentView addSubview:HLine];
    HLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    
    UIView *VLine = [[UIView alloc]initWithFrame:CGRectMake((self.contentView.width - 0.5)/2, self.contentView.height - 45, 0.5, 45)];
    [self.contentView addSubview:VLine];
    VLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    
    
    // 取消按钮
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, HLine.maxY, (self.contentView.width - 0.5)/2, 45)];
    [self.contentView addSubview:cancelButton];
    [cancelButton setTitle:@"取消领取" forState:UIControlStateNormal];
    cancelButton.backgroundColor = [UIColor clearColor];
    [cancelButton setTitleColor:[UIColor colorWithHex:0x666666] forState:UIControlStateNormal];
    [cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    [cancelButton addTarget:self action:@selector(cancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    // 继续领取按钮
    UIButton *creceivedButton = [[UIButton alloc]initWithFrame:CGRectMake(cancelButton.maxX+0.5, cancelButton.minY,  (self.contentView.width - 0.5)/2, 45)];
    [self.contentView addSubview:creceivedButton];
    creceivedButton.backgroundColor = [UIColor clearColor];
    [creceivedButton setTitleColor:[UIColor colorWithHex:0x666666] forState:UIControlStateNormal];
    [creceivedButton setTitle:@"继续领取" forState:UIControlStateNormal];
    [creceivedButton.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    [creceivedButton addTarget:self action:@selector(abnormalButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
   
    //------- 调整弹窗高度和中心 -------//
    self.contentView.height = cancelButton.maxY ;
    self.contentView.center = self.center;
    
}


#pragma mark - User Action

/** 申报异常按钮点击 */
- (void)abnormalButtonClicked {
    if ([self.delegate respondsToSelector:@selector(InsuranceAlertView:clickedButtonAtIndex:)]) {
        [self.delegate InsuranceAlertView:self clickedButtonAtIndex:200];
    }
    [self dismiss];
}

/** 取消按钮点击 */
- (void)cancelButtonClicked {
    if ([self.delegate respondsToSelector:@selector(InsuranceAlertView:clickedButtonAtIndex:)]) {
        [self.delegate InsuranceAlertView:self clickedButtonAtIndex:201];
    }
    [self dismiss];
}

#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];
}


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

@end
