//
//  OfficialAccountsAlertView.m
//  Open
//
//  Created by 雷俊博 on 2019/8/20.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "OfficialAccountsAlertView.h"
#import "Constants.h"


@interface OfficialAccountsAlertView ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong)UIView *contentView;

@property (nonatomic, strong)UILabel *lb_title;

@property (nonatomic, strong)UIImageView *img_message;

@property (nonatomic, strong)UIButton *btn_click;


@property (nonatomic, copy)NSString* title;

@property (nonatomic, copy)NSString* imageName;

@property (nonatomic, copy) clickActionBlock block;

@end

@implementation OfficialAccountsAlertView
#pragma mark - life cycle
- (instancetype)initWithTitle:(NSString *)title imageName:(NSString *)imageName block:(clickActionBlock)block;
{
    self = [super init];
    if (self) {
        self.title = title;
        self.imageName = imageName;
        self.block = block;
        
        [self setupUI];
    }
    return self;
}
- (void)setupUI
{
    
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, 280 * WIDTHRADIU, 225 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    
    //[self.contentView addSubview:self.lb_title];
    [self.contentView addSubview:self.img_message];
    [self.contentView addSubview:self.btn_click];
    
    
//    [self.lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.offset(16 * WIDTHRADIU);
//        make.centerX.equalTo(self.contentView);
//        make.height.offset(15 * WIDTHRADIU);
//    }];
    
//    UIImage *image = [UIImage imageNamed:@"TencentTask"];
//    CGFloat  scale = image.size.height/image.size.width;
    
    [self.img_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.offset(250 * WIDTHRADIU);
        make.height.offset(130 * WIDTHRADIU);
    }];
    
    [self.btn_click mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.img_message.mas_bottom).offset(20 * WIDTHRADIU);
        make.width.offset(230 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.height.offset(40 * WIDTHRADIU);
    }];
    
    self.lb_title.text = [NSString stringWithFormat:@"公众号账号:%@",self.title];
    [self.img_message sd_setImageWithURL:[NSURL URLWithString:self.imageName] placeholderImage:[UIImage imageNamed:@"TencentTask"]];
    
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.contentView]) {
        return NO;
    }
    
    return YES;
}


+ (void)alertWithVC:(UIViewController *)vc Title:(NSString *)title imageName:(NSString *)imageName block:(clickActionBlock)block;
{
    
    OfficialAccountsAlertView* alertView = [[OfficialAccountsAlertView alloc]initWithTitle:title imageName:imageName block:block];
    
    UIWindow* keyWindow = vc.view.window;
    
    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }

    [keyWindow addSubview:alertView];
    
}


#pragma mark - event response
- (void)tapClick:(UITapGestureRecognizer *)tap {
    [self dismissUI];
}
- (void)btnClick:(UIButton *)sender
{
    [self dismissUI];
    if (self.block) {
        self.block(YES);
    }
}

#pragma mark -隐藏弹框
- (void)dismissUI
{
     [self removeFromSuperview];
}



#pragma mark - getters and setters
- (UILabel *)lb_title
{
    if (!_lb_title) {
        _lb_title = [[UILabel alloc]init];
        _lb_title.font = [UIFont boldSystemFontOfSize:15 * WIDTHRADIU];
        _lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
        _lb_title.textAlignment = NSTextAlignmentCenter;
    }
    return _lb_title;
}

- (UIImageView *)img_message
{
    if (!_img_message) {
        _img_message = [[UIImageView alloc]init];
        //_img_message.image = [UIImage imageNamed:self.imageName];
    }
    return _img_message;
}

- (UIButton *)btn_click
{
    if (!_btn_click) {
        _btn_click = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_click.backgroundColor = [UIColor colorWithHex:0xfa9504];
        [_btn_click setTitle:@"复制账号并关注" forState:UIControlStateNormal];
        [_btn_click setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btn_click.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
        [_btn_click addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _btn_click.layer.cornerRadius = 20 * WIDTHRADIU;
    }
    return _btn_click;
}

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

@end
