//
//  KKShareView.m
//  Open
//
//  Created by 雷俊博 on 2021/6/11.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "KKShareView.h"

/*   <!-- 应用规范字体大小 -->*/
#define  text_size_big_1    21
#define  text_size_big_2    20
#define  text_size_big_3    19
#define  text_size_middle_1 18
#define  text_size_middle_2 17
#define  text_size_middle_3 15
#define  text_size_little_1 14
#define  text_size_little_2 13
#define  text_size_little_3 12
#define  text_size_little_4 10
#define  text_size_other    16

#define  AppFont(c_font) [UIFont systemFontOfSize:c_font]
#define ApplicationframeValue [[UIScreen mainScreen]bounds].size


@interface KKShareView ()

@property (nonatomic, strong) UIControl *overlayView;
@property(nonatomic,strong)UIView *viewS;

@property (nonatomic,strong)NSArray *imageArr;
@property (nonatomic,strong)NSArray *titleArr;

@property (nonatomic,copy)KKShareViewBtnClickBlock block;

@end

@implementation KKShareView
-(NSArray *)imageArr
{

    if (!_imageArr) {
        _imageArr = [NSArray arrayWithObjects:@"share_pengyouquan",@"share_wechat",nil];
    }
    return _imageArr;
}

-(NSArray *)titleArr
{
    
    if (!_titleArr) {
        _titleArr = [NSArray arrayWithObjects:@"朋友圈",@"微信好友",nil];
    }
    return _titleArr;
}

-(id)initWithFrame:(CGRect)frame block:(KKShareViewBtnClickBlock)block
{
    float Margin = ceilf(ApplicationframeValue.width/self.imageArr.count/2);
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.block = block;
        self.overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
        [self.overlayView addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *viewS = [[UIView alloc] initWithFrame:self.bounds];
        viewS.backgroundColor =[UIColor whiteColor];
        [self addSubview:viewS];
        
        UILabel *TitleLabel = [[UILabel alloc] initWithFrame:CGRectMake((ApplicationframeValue.width-100)/2, 15, 100, 20)];
        TitleLabel.textAlignment = NSTextAlignmentCenter;
        TitleLabel.text = @"分享到";
        TitleLabel.backgroundColor = [UIColor whiteColor];
        TitleLabel.font = AppFont(text_size_other);
        [viewS addSubview:TitleLabel];
        
        UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 45, ApplicationframeValue.width, 1)];
        lineImage.alpha = 0.5f;
        lineImage.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4f];
        [viewS addSubview:lineImage];
     
        for (int i = 0; i < [self.imageArr count]; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = i +1;
            btn.frame = CGRectMake(Margin/2+2*Margin*i,60, Margin, Margin);
            //[btn setBackgroundImage:[UIImage imageNamed:self.imageArr[i]] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:self.imageArr[i]] forState:UIControlStateNormal];
            [btn setTitle:self.titleArr[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHex:0x4d4d4d] forState:UIControlStateNormal];
            btn.titleLabel.font =  AppFont(text_size_middle_3);
            btn.titleLabel.adjustsFontSizeToFitWidth = YES;
            
            [btn addTarget:self action:@selector(onBtn:) forControlEvents:UIControlEventTouchUpInside];
            [viewS addSubview:btn];
            
            [btn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:8];
        }
        
    }
    
    return self;
}

-(void)onBtn:(UIButton *)sender
{
    
    if (self.block) {
        self.block(sender.tag);
    }
    
    [self dismiss];
}


-(void)onCancleBtn{

    [self dismiss];

}


+ (void)showShareAlertViewWithBlock:(KKShareViewBtnClickBlock)block
{
    
    CGFloat height = 160 + (Height_TapBar - 49);
    
    KKShareView *shareView = [[KKShareView alloc]initWithFrame:CGRectMake(0, ApplicationframeValue.height - height, ApplicationframeValue.width, height) block:block];
    [shareView show];
}



- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:self.overlayView];
    [keywindow addSubview:self];
    
    [self fadeIn];
}

- (void)dismiss
{
    [self fadeOut];
}

//弹入层
- (void)fadeIn
{

    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
       
        self.alpha = 1;
 
    }];
    
}

//弹出层
- (void)fadeOut
{
    [UIView animateWithDuration:.35 animations:^{

        self.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        if (finished) {
            [self.overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}



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

@end
