//
//  GMBookReachStoreConsultView.m
//  AFNetworking
//
//  Created by Jessica on 2020/7/20.
//

#import "GMBookReachStoreConsultView.h"
#import "GMBookConsultViewModel.h"
#import "GMLibraryHeader.h"

@interface GMBookReachStoreConsultView()
@property (nonatomic, strong) UIButton *authBtn;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *iconLabel;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, copy) NSString *recordId;
@property (nonatomic, assign) NSInteger leadPhoneRequestId;
@property (nonatomic, assign) BOOL isHadPhone;


@end
@implementation GMBookReachStoreConsultView
-(void)setup{
    [super setup];
    self.animationType = GMPopupAnimationTypeFlipFromBottom;
    self.container.backgroundColor = UIColor.whiteColor;

    [self.container mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(267 + UIView.safeAreaInsetsBottom);
        make.bottom.mas_equalTo(0);
    }];
    self.container.frame = CGRectMake(0, MAINSCREEN_HEIGHT - 267 - UIView.safeAreaInsetsBottom, MAINSCREEN_WIDTH, 267 + UIView.safeAreaInsetsBottom);
    self.container.clipsToBounds = NO;
    [self containerRectCorner:self.container];

    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = UIColor.clearColor;
    titleView.frame = CGRectMake(0, MAINSCREEN_HEIGHT - 267 - UIView.safeAreaInsetsBottom, MAINSCREEN_WIDTH, 85);
    titleView.bottom = self.container.top;
    [self addSubview:titleView];
   
    _headerImageView = [[UIImageView alloc] init];
    [titleView addSubview:_headerImageView];
    [_headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(85);
        make.centerX.mas_equalTo(self.container.mas_centerX);
        make.top.mas_equalTo(42.5);
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = RGBCOLOR_HEX(0x464646);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.numberOfLines = 0;
    _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _titleLabel.font = [UIFont gmBoldFont:18];
    [self.container addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headerImageView.mas_bottom).offset(1);
        make.centerX.mas_equalTo(_headerImageView.mas_centerX);
        make.left.mas_equalTo(35);
        make.right.mas_equalTo(-35);
    }];
    
    _iconLabel = [[UILabel alloc] init];
    _iconLabel.textColor = UIColor.grayColor;
    _iconLabel.textAlignment = NSTextAlignmentCenter;
    _iconLabel.font = [UIFont gmFont:14];
    [self.container addSubview:_iconLabel];
    [_iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(14);
        make.centerX.mas_equalTo(_titleLabel.mas_centerX).offset(10.5);
        make.height.mas_equalTo(19);
    }];
    
    _icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"video_book_consult_small_icon"]];
    [self.container addSubview:_icon];
    [_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(16);
        make.right.mas_equalTo(_iconLabel.mas_left).offset(-5);
        make.centerY.mas_equalTo(_iconLabel.mas_centerY);
    }];
        
    _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"clue_cancl"] forState:UIControlStateNormal];
    [_cancelBtn addTarget:self action:@selector(cancelClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.container addSubview:_cancelBtn];
    _cancelBtn.layer.borderColor = UIColor.whiteColor.CGColor;
    [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(20);
        make.height.width.mas_equalTo(15);
    }];
    
    _phoneLabel = [[UILabel alloc] init];
    _phoneLabel.backgroundColor = RGBCOLOR_HEX(0xECECEC);
    _phoneLabel.textColor = RGBCOLOR_HEX(0x484848);
    _phoneLabel.textAlignment = NSTextAlignmentCenter;
    _phoneLabel.layer.cornerRadius = 7;
    _phoneLabel.layer.masksToBounds = YES;
    _phoneLabel.font = [UIFont gmFont:16];
    [self.container addSubview:_phoneLabel];
    [_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_iconLabel.mas_bottom).offset(20);
        make.centerX.mas_equalTo(_headerImageView.mas_centerX);
        make.height.mas_equalTo(37);
        make.left.mas_equalTo(self.container.mas_left).offset(35);
        make.right.mas_equalTo(self.container.mas_right).offset(-35);
    }];
    
    _authBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _authBtn.backgroundColor = RGBCOLOR_HEX(0x3AC3BD);
    _authBtn.layer.cornerRadius = 7;
    _authBtn.layer.masksToBounds = YES;
    _authBtn.titleLabel.font = [UIFont gmBoldFont:16];
    [_authBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [_authBtn setTitle:@"一键授权(加密)" forState:UIControlStateNormal];
    _authBtn.titleLabel.layer.shadowColor = [UIColor colorWithHexString:@"007872" alpha:1].CGColor; //阴影的颜色
    _authBtn.titleLabel.layer.shadowOpacity = 1.0f; //阴影的不透明度
    _authBtn.titleLabel.layer.shadowOffset = CGSizeMake(0.0f, 1.5f); //阴影的偏移量，这个很重要
    _authBtn.titleLabel.layer.shadowRadius = 0.5f; //阴影的扩散半径，
    [_authBtn addTarget:self action:@selector(authClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.container addSubview:_authBtn];
    _authBtn.layer.borderColor = UIColor.whiteColor.CGColor;
    [_authBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_phoneLabel.mas_bottom).offset(12);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(self.container.mas_left).offset(35);
        make.right.mas_equalTo(self.container.mas_right).offset(-35);
    }];
        
    _tipLabel = [[UILabel alloc] init];
    _tipLabel.text = @"更美为您提供号码保护，对方看不到您的真实手机号";
    _tipLabel.textColor = UIColor.grayColor;
    _tipLabel.textAlignment = NSTextAlignmentCenter;
    _tipLabel.font = [UIFont gmFont:11];
    [self.container addSubview:_tipLabel];
    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_authBtn.mas_bottom).offset(16);
        make.bottom.mas_equalTo(self.container.mas_bottom).offset(-25 - UIView.safeAreaInsetsBottom);
        make.centerX.mas_equalTo(_headerImageView.mas_centerX);
        make.height.mas_equalTo(15);
        make.left.mas_equalTo(self.container.mas_left).offset(33);
        make.right.mas_equalTo(self.container.mas_right).offset(-33);
    }];
    
}

- (void)containerRectCorner:(UIView *)view {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}

- (void)authClicked {
    [GMBookReachStoreConsultView on_click_buttonWithButtonName:@"appointment"];
    if (self.isHadPhone) {
        __weak typeof(self) weakSelf = self;
        [GMBookConsultViewModel bookConsultWithRecordId:SafeString(self.recordId) requestId:_leadPhoneRequestId  successBlock:^(id responseObject) {
            GMResponseOC *response = (GMResponseOC *)responseObject;
            NSDictionary *data = response.data;
            // 跳转发私信
            [[GMRouter sharedInstance] pushScheme:SafeString(data[@"message_url"])];
            [weakSelf removeFromSuperview];
        } failBlock:^(NSString *message) {
            [[GMBaseTool getCurrentViewController] toast:message];
            [weakSelf cancelClicked];
        }];
    } else {
        [[GMBaseTool getCurrentViewController] toast:@"请先在我的页面设置功能中绑定手机号"];
        [self cancelClicked];
    }
}

- (void)cancelClicked {
    [GMBookReachStoreConsultView on_click_buttonWithButtonName:@"close"];
    [self removeFromSuperview];
}

- (void)hide {
    // 点击其他部分不消失
}

+ (void)showBookConsultPopViewWithObject:(GMBookConsultInfoModel *)model {
    if (model) {
        GMBookReachStoreConsultView *popView = [[GMBookReachStoreConsultView alloc] initWithFrame:CGRectMake(0, 0, MAINSCREEN_WIDTH, MAINSCREEN_HEIGHT)];
        [popView popup_view]; // 埋点
        popView.titleLabel.text = model.title;
        [popView.headerImageView sd_setImageWithURL:[NSURL URLWithString:model.imgUrl]];
        popView.iconLabel.text = model.subTitle;
        popView.phoneLabel.text = model.telephone;
        popView.tipLabel.text = model.prompt;
        popView.recordId = model.recordId;
        popView.isHadPhone = model.hasPhone;
        popView.leadPhoneRequestId = model.leadPhoneRequestId;
        [[GMBaseTool getCurrentViewController].view addSubview:popView];
    }
}

- (void)popup_view {
    NSDictionary *dict = @{@"page_name":SafeString([GMBaseTool getCurrentViewController].pageName),
                           @"popup_name":@"free_to_clinic",
                           @"referrer_link":[GMBaseTool getCurrentViewController].referrerLink ? : @[],
                           @"business_id":SafeString([GMBaseTool getCurrentViewController].businessId),
                           @"referrer":SafeString([GMBaseTool getCurrentViewController].referer),
                           @"referrer_id":SafeString([GMBaseTool getCurrentViewController].referrerId)
    };
    [Phobos track:@"popup_view" attributes:dict];
}

+ (void)on_click_buttonWithButtonName:(NSString *)buttonName {
    NSDictionary *dict = @{@"page_name":SafeString([GMBaseTool getCurrentViewController].pageName),
                           @"popup_name":@"free_to_clinic",
                           @"referrer_link":[GMBaseTool getCurrentViewController].referrerLink ? : @[],
                           @"business_id":SafeString([GMBaseTool getCurrentViewController].businessId),
                           @"referrer":SafeString([GMBaseTool getCurrentViewController].referer),
                           @"referrer_id":SafeString([GMBaseTool getCurrentViewController].referrerId),
                           @"button_name":SafeString(buttonName)
    };
    [Phobos track:@"on_click_button" attributes:dict];
}

@end
