//
//  GMFacePhotosPopView.m
//  Gengmei
//
//  Created by 刘鹿杰 on 2019/12/26.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "GMFacePhotosPopView.h"
#import "GMAIFacePopViewObject.h"
#import "NSMutableAttributedString+Attachment.h"

#define popViewHeight (100 + OCNavigationBar.statusBarHeight)

@interface GMFacePhotosPopView ()

@property (nonatomic, strong) UIImageView *iconImage;
@property (nonatomic, strong) GMLabel *titleLabel;
@property (nonatomic, strong) GMLabel *contentLabel;

@end

@implementation GMFacePhotosPopView

- (void)setup {
    [super setup];
    
    self.backgroundColor = [UIColor whiteColor];
    // 添加底部关闭按钮
    self.iconImage = [[UIImageView alloc] init];
    _iconImage.userInteractionEnabled = YES;
    [self addSubview:_iconImage];
    [_iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(OCNavigationBar.statusBarHeight + 15);
        make.left.mas_equalTo(15);
        make.width.height.mas_equalTo(70);
    }];
    
    // 添加title
    self.titleLabel = [[GMLabel alloc] init];
    _titleLabel.textColor = UIColor.bodyText;
    _titleLabel.font = [UIFont gmBoldFont:16];
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.numberOfLines = 2;
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_iconImage.mas_top);
        make.left.mas_equalTo(_iconImage.mas_right).offset(10);
        make.right.mas_equalTo(-50);
    }];
    
    // 添加内容
    self.contentLabel = [GMLabel labelWithTextColor:UIColor.auxiliaryTextDark fontSize:12];
    _contentLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel.mas_left);
        make.right.mas_equalTo(_titleLabel.mas_right);
        make.bottom.mas_equalTo(_iconImage.mas_bottom);
        make.height.mas_equalTo(12);
    }];
    
    // 添加关闭按钮
    GMButton *closebtn = [[GMButton alloc] init];
    [self addSubview:closebtn];
    [closebtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [closebtn setImage:[UIImage imageNamed:@"face_skin_popview_hidden"] forState:UIControlStateNormal];
    [closebtn setImage:[UIImage imageNamed:@"face_skin_popview_hidden"] forState:UIControlStateHighlighted];
    [closebtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(_iconImage.mas_top);
        make.width.height.mas_equalTo(20);
    }];
    
    // 添加点击 和清扫手势效果
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushViewComtroller)];
    [self addGestureRecognizer:tap];
}

-(void)setPopModel:(GMAIFacePopViewObject *)popModel {
    _popModel = popModel;
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:popModel.imageUrl]];
    
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:@" "];
    [attribute appendAttributedString:[[NSAttributedString alloc] initWithString:SafeString(popModel.title)]];
    // 文字前面插入图片
    [attribute addAIPopViewAttachment];  // 添加图片
    //设置行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];
    [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
    [paragraphStyle setAlignment:NSTextAlignmentLeft];
    [attribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [popModel.title length])];
    self.titleLabel.attributedText = attribute;
    
    self.contentLabel.text = popModel.content;
}

- (void)pushViewComtroller { // 跳转控制器
    [self removeFromSuperview];
    [Phobos track:@"on_click_button" attributes:@{@"page_name" : @"face_detect_result", @"button_name" :@"learn_more"}];
    [[GMRouter sharedInstance] pushScheme:_popModel.gmUrl];
//    [AppDelegate.visibleController pushScheme:_popModel.gmUrl];
}

-(void)closeBtnClick {
    [UIView animateWithDuration:0.25 animations:^{
        self.mj_y = (-popViewHeight);
    } completion:^(BOOL finished) {
        [Phobos track:@"on_click_button" attributes:@{@"page_name" : @"face_detect_result", @"button_name" :@"close"}];
        [self removeFromSuperview];
    }];
}

@end
