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

#import "AiDaAlertView.h"
#import "Constants.h"
#import <YYLabel.h>
#import <YYText.h>
#import "tabViewController.h"

@interface AiDaAlertView ()
{
    BOOL isSelected;
    CGFloat _height;
}

@property (nonatomic, strong)UIImageView* img_icon;

@property (nonatomic, strong)UILabel* lb_name;

@property (nonatomic, strong)UILabel* lb_content;

@property (nonatomic, strong)UILabel* lb_content2;

@property (nonatomic, strong)UIButton* agreeBtn;

@property (nonatomic, strong)UIButton* disAgreeBtn;


@property (nonatomic, strong)YYLabel* agreementLabel;

@property (nonatomic, assign)AiDaAlertViewType type;

@property (nonatomic, strong)UIViewController* viewController;


@end

@implementation AiDaAlertView
#pragma mark - life cycle
- (instancetype)initWithType:(AiDaAlertViewType)type ViewController:(UIViewController *)viewController
{
    CGFloat height  = 0.0;
    if (type == AiDaAlertViewTypeAD) {
        height  = 192.5 + 20;
    }else if (type == AiDaAlertViewTypeBC){
        height  = 192.5;
    }

    self.type = type;
    self.viewController = viewController;
    _height = height;
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, height)];
    if (self) {
        if (type == AiDaAlertViewTypeAD) {
           [self setUI2];
        }else if (type == AiDaAlertViewTypeBC){
            [self setUI];
        }
        
    }
    return self;
}
- (void)setUI
{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.img_icon];
    [self addSubview:self.lb_name];
    [self addSubview:self.lb_content];
    [self addSubview:self.lb_content2];
    [self addSubview:self.agreeBtn];
    [self addSubview:self.disAgreeBtn];
    
    
    [self.img_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(10);
        make.width.height.offset(50);
    }];
    
    [self.lb_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.img_icon);
        make.left.equalTo(self.img_icon.mas_right).offset(10);
    }];
    
    [self.lb_content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.img_icon);
        make.left.equalTo(self.lb_name.mas_right).offset(10);
    }];
    
    [self.lb_content2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.img_icon);
        make.top.equalTo(self.img_icon.mas_bottom).offset(15);
    }];
    
    [self.disAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.bottom.offset(-20);
        make.height.offset(45);
    }];

    
    [self.agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.disAgreeBtn);
        make.left.equalTo(self.disAgreeBtn.mas_right).offset(10);
        make.right.offset(-10);
        make.width.height.equalTo(self.disAgreeBtn);
    }];
    
}


- (void)setUI2
{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.img_icon];
    [self addSubview:self.lb_name];
    [self addSubview:self.lb_content];
    [self addSubview:self.lb_content2];
    [self addSubview:self.agreementLabel];
    [self addSubview:self.agreeBtn];
    [self addSubview:self.disAgreeBtn];
    
    self.lb_content.text = @"(精准的AI智能问诊)";
    self.lb_content2.text = @"申请获取您的手机号码";
    
    
    [self.img_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(10);
        make.width.height.offset(50);
    }];
    
    [self.lb_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.img_icon);
        make.left.equalTo(self.img_icon.mas_right).offset(10);
    }];
    
    [self.lb_content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.img_icon);
        make.left.equalTo(self.lb_name.mas_right).offset(10);
    }];
    
    [self.lb_content2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.img_icon);
        make.top.equalTo(self.img_icon.mas_bottom).offset(15);
    }];
    
    [self.agreementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lb_content2.mas_bottom).offset(5);
        make.left.equalTo(self.lb_content2);
    }];
    
    [self.disAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.bottom.offset(-20);
        make.height.offset(45);
    }];

    
    [self.agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.disAgreeBtn);
        make.left.equalTo(self.disAgreeBtn.mas_right).offset(10);
        make.right.offset(-10);
        make.width.height.equalTo(self.disAgreeBtn);
    }];
    
}


+ (UIView *)showAlertViewWithTitle:(NSString*)name imageName:(NSString *)imageName ViewController:(UIViewController *)viewController type:(AiDaAlertViewType)type block:(nonnull AiDaMessageClickBlcok)block
{
    AiDaAlertView *alert = [[AiDaAlertView alloc]initWithType:type ViewController:viewController];
    if (name && imageName) {
        alert.img_icon.image = [UIImage imageNamed:imageName];
        alert.lb_name.text = name;
    }
    alert.block = block;
                                 
    [alert showWithViewController:viewController];
    
    return alert;
}

#pragma mark - event response
- (void)btnClick:(UIButton *)sender
{
    if (self.block && sender == self.agreeBtn) {
        
        self.block(YES);
    }
    
    [self dismiss];
}

#pragma mark - 隐藏和显示
-(void)showWithViewController:(UIViewController *)viewController
{
    UIWindow* keyWindow = viewController.view.window;
    
    if (keyWindow == nil) {
           keyWindow = [UIApplication sharedApplication].keyWindow;
       }
    
     UIView* maskView = [[UIView alloc]init];
    maskView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    
    self.maskView = maskView;
     maskView.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.5];
    
    
    if (self.type != AiDaAlertViewTypeAD) {
        [keyWindow addSubview:maskView];
           
        [keyWindow  addSubview:self];
//        [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(keyWindow);
//        }];
    }else{
       [keyWindow addSubview:maskView];
           
        [keyWindow  addSubview:self];
//        [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(viewController.view);
//        }];
    }
   
   
    
   
    [maskView layoutIfNeeded];
    
    CGRect frame = self.frame;
    frame.origin.y = ScreenHeight;
    
    self.frame = frame;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        CGRect frame = self.frame;
        frame.origin.y = ScreenHeight - self->_height;
           
           self.frame = frame;
        
    } completion:nil];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.2 animations:^{
        self.maskView.alpha              = 0;
         CGRect frame = self.frame;
           frame.origin.y = ScreenHeight;
           self.frame = frame;
    } completion:^(BOOL finished) {
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        [self.maskView removeFromSuperview];
        [self removeFromSuperview];
    }];
}


#pragma mark - getters and setters
- (UIImageView *)img_icon
{
    if (!_img_icon) {
        _img_icon = [[UIImageView alloc]init];
        _img_icon.image = [UIImage imageNamed:@"aidalogo"];
    }
    return _img_icon;
}

- (UILabel *)lb_name
{
    if (!_lb_name) {
        _lb_name = [[UILabel alloc]init];
        _lb_name.text = @"爱达健康";
        _lb_name.textColor = [UIColor colorWithHex:0x333333];
        _lb_name.font = [UIFont systemFontOfSize:18];
    }
    return _lb_name;
}

- (UILabel *)lb_content
{
    if (!_lb_content) {
        _lb_content = [[UILabel alloc]init];
        _lb_content.text = @"申请";
        _lb_content.textColor = [UIColor colorWithHex:0x333333];
        _lb_content.font = [UIFont systemFontOfSize:16];
    }
    return _lb_content;
}
- (UILabel *)lb_content2
{
    if (!_lb_content2) {
        _lb_content2 = [[UILabel alloc]init];
        _lb_content2.text = @"获取您的手机号码";
        _lb_content2.textColor = [UIColor colorWithHex:0x333333];
        _lb_content2.font = [UIFont systemFontOfSize:23];
    }
    return _lb_content2;
}

- (YYLabel *)agreementLabel
{
    if (!_agreementLabel) {
        _agreementLabel = [[YYLabel alloc]init];
        
        NSString *agreementText = @"点击允许即表示已阅读和同意《服务条款》与《隐私协议》";

        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:agreementText];

        
        [text addAttributeFont:[UIFont systemFontOfSize:14] range:NSMakeRange(0, agreementText.length)];
        [text addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0x404040] range:NSMakeRange(0, 11)];
        [text addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(13, 6)];
        [text addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(agreementText.length - 6, 6)];
        
//        WeakSelf
        [text yy_setTextHighlightRange:NSMakeRange(13, 6) color:[UIColor blueColor] backgroundColor:[UIColor clearColor] userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            NSLog(@"服务条款被点击了");
           // [weakSelf dismiss];
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.adachina.com/raw/terms-and-conditions"]];
//            Web2ViewController * vc = [Web2ViewController new];
//            vc.title= @"服务条款";
//            vc.skipUrl = [NSURL URLWithString:@"https://www.adachina.com/raw/terms-and-conditions"];
//            vc.hidesBottomBarWhenPushed = YES;
//
//
//
//            [weakSelf.viewController.navigationController pushViewController:vc animated:YES];
        } longPressAction:nil];
        
        [text yy_setTextHighlightRange:NSMakeRange(agreementText.length - 6, 6) color:[UIColor blueColor] backgroundColor:[UIColor clearColor] userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            NSLog(@"隐私协议被点击了");
            //[weakSelf dismiss];
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.adachina.com/raw/privacy-policy/"]];
//            Web2ViewController * vc = [Web2ViewController new];
//            vc.title= @"隐私协议";
//            vc.skipUrl = [NSURL URLWithString:@"https://www.adachina.com/raw/privacy-policy/"];
//            vc.hidesBottomBarWhenPushed = YES;
//
//            
//            [weakSelf.viewController.navigationController pushViewController:vc animated:YES];
        } longPressAction:nil];
        
        _agreementLabel.attributedText = text;
    }
    return _agreementLabel;
}




- (UIButton *)agreeBtn
{
    if (!_agreeBtn) {
        _agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_agreeBtn setTitle:@"允许" forState:UIControlStateNormal];
        [_agreeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _agreeBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_agreeBtn setBackgroundColor:K_MainColor];
        _agreeBtn.layer.cornerRadius = 10;
        
        [_agreeBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _agreeBtn;
}
- (UIButton *)disAgreeBtn
{
    if (!_disAgreeBtn) {
        _disAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_disAgreeBtn setTitle:@"拒绝" forState:UIControlStateNormal];
        [_disAgreeBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
        _disAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:17];
        [_disAgreeBtn setBackgroundColor:[UIColor whiteColor]];
        _disAgreeBtn.layer.borderColor = [UIColor colorWithHex:0xb3b3b3].CGColor;
        _disAgreeBtn.layer.borderWidth = 0.5;
        _disAgreeBtn.layer.cornerRadius = 10;
         [_disAgreeBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _disAgreeBtn;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
