//
//  ERTCShareTipView.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/11/21.
//

#import "ERTCShareTipView.h"

@interface ERTCShareTipView ()

@property (nonatomic, strong) UIButton *screenBtn;
@property (nonatomic, strong) UIButton *photoBtn;
@property (nonatomic, strong) UIButton *whiteBoardBtn;

@end

@implementation ERTCShareTipView

-(instancetype)initWithAbility:(int)ability
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.screenBtn = [self getBtnImageName:@"icn_screen_share_screen" title:@"屏幕"
                               selectImageName:@"icn_screen_share_screen" selectTitle:@"屏幕"
                              disableImageName:@"icn_screen_share_screen" disableTitle:@"屏幕"];
        
        self.photoBtn = [self getBtnImageName:@"icn_img_share_screen" title:@"照片"
                              selectImageName:@"icn_img_share_screen" selectTitle:@"照片"
                             disableImageName:@"icn_img_share_screen" disableTitle:@"照片"];
        
        self.whiteBoardBtn = [self getBtnImageName:@"icn_whiteboard_share_screen" title:@"共享白板"
                                   selectImageName:@"icn_whiteboard_share_screen" selectTitle:@"共享白板"
                                  disableImageName:@"icn_whiteboard_share_screen" disableTitle:@"共享白板"];
        
        [self.screenBtn addTarget:self action:@selector(screenBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.photoBtn addTarget:self action:@selector(photoBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.whiteBoardBtn addTarget:self action:@selector(whiteBoardBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        
        if (ability & ERTCShareTipViewAbility_Screen) {
            [self addSubview:self.screenBtn];
            [self addSubview:self.photoBtn];
            [self addSubview:self.whiteBoardBtn];
            
            [self.screenBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                make.width.height.ysmas_equalTo(76);
                make.centerY.equalTo(self.photoBtn);
                make.right.equalTo(self.photoBtn.ysmas_left).with.offset(-14);
            }];
            
            [self.photoBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                make.width.height.ysmas_equalTo(76);
                make.centerX.equalTo(self);
                make.bottom.equalTo(self).with.offset(-10);
            }];
            
            [self.whiteBoardBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                make.width.height.ysmas_equalTo(76);
                make.centerY.equalTo(self.photoBtn);
                make.left.equalTo(self.photoBtn.ysmas_right).with.offset(14);
            }];
        } else {
            [self addSubview:self.photoBtn];
            [self addSubview:self.whiteBoardBtn];
            
            UIView *spaceView = [UIView new];
            spaceView.backgroundColor = [UIColor clearColor];
            [self addSubview:spaceView];
            
            [spaceView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                make.centerX.equalTo(self);
                make.width.ysmas_equalTo(14);
                make.height.ysmas_equalTo(76);
                make.bottom.equalTo(self).with.offset(-10);
            }];
            
            [self.photoBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                make.width.height.ysmas_equalTo(76);
                make.centerY.equalTo(spaceView);
                make.right.equalTo(spaceView.ysmas_left).with.offset(-14);
            }];
            
            [self.whiteBoardBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
                make.width.height.ysmas_equalTo(76);
                make.centerY.equalTo(spaceView);
                make.left.equalTo(spaceView.ysmas_right).with.offset(14);
            }];
        }
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelTipView:)];
        [self addGestureRecognizer:tapGes];
    }
    
    return self;
}

-(void)screenBtnClick:(UIButton *)btn {
    NSLog(@"screenBtnClick");
    if ([self.delegate respondsToSelector:@selector(clickScreenShare:)]) {
        [self.delegate clickScreenShare:self];
    }
    
    [self cancelTipView:nil];
}

-(void)photoBtnClick:(UIButton *)btn {
    NSLog(@"photoBtnClick");
    if ([self.delegate respondsToSelector:@selector(clickPhotoShare:)]) {
        [self.delegate clickPhotoShare:self];
    }
    
    [self cancelTipView:nil];
}

-(void)whiteBoardBtnClick:(UIButton *)btn {
    NSLog(@"whiteBoardBtnClick");
    if ([self.delegate respondsToSelector:@selector(clickWhiteBoardShare:)]) {
        [self.delegate clickWhiteBoardShare:self];
    }
    
    [self cancelTipView:nil];
}

-(UIButton *)getBtnImageName:(NSString *)imageName
                       title:(NSString *)title
             selectImageName:(NSString *)selectImageName
                 selectTitle:(NSString *)selectTitle
            disableImageName:(NSString *)disableImageName
                disableTitle:(NSString *)disableTitle {
    UIButton *btn = [[UIButton alloc] init];
    UIView *centerView = [UIView new];
    centerView.backgroundColor = [UIColor clearColor];
    centerView.userInteractionEnabled = NO;
    
    UIButton *imageBtn = [[UIButton alloc] init];
    imageBtn.backgroundColor = [UIColor clearColor];
    [imageBtn setImage:[UIImage imageNamed:imageName]
              forState:(UIControlStateNormal)];
    [imageBtn setImage:[UIImage imageNamed:selectImageName]
              forState:(UIControlStateSelected)];
    [imageBtn setImage:[UIImage imageNamed:disableImageName]
              forState:(UIControlStateDisabled)];
    imageBtn.userInteractionEnabled = NO;
    [centerView addSubview:imageBtn];
    
    UIButton *titleBtn = [[UIButton alloc] init];
    [titleBtn setTitle:title forState:(UIControlStateNormal)];
    [titleBtn setTitle:selectTitle forState:(UIControlStateSelected)];
    [titleBtn setTitle:disableTitle forState:(UIControlStateDisabled)];
    titleBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [titleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    titleBtn.userInteractionEnabled = NO;
    titleBtn.tag = 9988;
    [centerView addSubview:titleBtn];
    [btn addSubview:centerView];
    
    [centerView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.width.ysmas_equalTo(30);
        make.center.equalTo(btn);
    }];
    
    [imageBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.top.equalTo(centerView);
        make.bottom.equalTo(titleBtn.ysmas_top).with.offset(-0.5);
        make.centerX.equalTo(centerView);
        make.width.height.ysmas_equalTo(30);
    }];
    
    [titleBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.bottom.equalTo(centerView);
        make.centerX.equalTo(centerView);
        make.height.ysmas_equalTo(14);
    }];
    
    btn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    btn.layer.cornerRadius = 10;
    
    return btn;
}

#pragma mark - UITapGestureRecognizer
-(void)cancelShareTipView {
    [self cancelTipView:nil];
}

-(void)cancelTipView:(UITapGestureRecognizer *)tapGes {
    self.hidden = YES;
    [self removeFromSuperview];
}

#pragma mark - UI Getter Method
-(void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

@end
