//
//  GMShareVideoAlertView.m
//  Gengmei
//
//  Created by 朱璇 on 2019/8/3.
//  Copyright © 2019 更美互动信息科技有限公司. All rights reserved.
//

#import "GMShareVideoAlertView.h"
#import <Masonry/Masonry.h>

@interface GMShareVideoAlertView()
@property (nonatomic, copy) GMShareVideoAlertViewBlcok block;

@end

@implementation GMShareVideoAlertView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:.6];
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"share_wechatline_video_bg"]];
    background.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:background];
    CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;

    [background mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(93 + statusBarHeight);
        make.bottom.mas_equalTo(-81 - self.safeAreaInsetsBottom);
    }];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setBackgroundImage:[UIImage imageNamed:@"share_wechatline_video"] forState:UIControlStateNormal];
    [confirmBtn setBackgroundImage:[UIImage imageNamed:@"share_wechatline_video"] forState:UIControlStateNormal | UIControlStateHighlighted];
    [confirmBtn addTarget:self action:@selector(didClickConfirm) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:confirmBtn];
    
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(81, 35));
        make.centerX.mas_equalTo(self.mas_centerX);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-50 - self.safeAreaInsetsBottom);
    }];
}

+ (instancetype)showNoticeAtertViewWithConfirmBtnClickBlock:(GMShareVideoAlertViewBlcok)confirmBtnClickBlock {
    UIView *backView = (UIView *)[[UIApplication sharedApplication] delegate];
    GMShareVideoAlertView *alertView = [[GMShareVideoAlertView alloc] initWithFrame:backView.window.bounds];
    alertView.block = confirmBtnClickBlock;
    [backView.window addSubview:alertView];
    return alertView;
}

- (void)didClickConfirm {
    [self removeFromSuperview];
    if (self.block) {
        self.block();
    }
}

- (CGFloat)safeAreaInsetsBottom {
    if (@available(iOS 11.0, *)) {
        return [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom;
    } else {
        return 0;
    }
}

@end
