//
//  ERTCVideoMeetingBottomView.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/5/11.
//

#import "ERTCVideoMeetingBottomView.h"

@interface ERTCVideoMeetingBottomView ()
@property (nonatomic, strong) UIButton *micrBtn;
@property (nonatomic, strong) UIButton *micrImageBtn;
@property (nonatomic, strong) UIButton *cammerBtn;
@property (nonatomic, strong) UIButton *shareBtn;
@property (nonatomic, strong) UIButton *membersBtn;
@property (nonatomic, strong) UIButton *moreBtn;

@end

@implementation ERTCVideoMeetingBottomView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.8/1.0];
        self.micrBtn = [self getBtnImageName:@"大0" title:@"静音" selectImageName:@"大静音" selectTitle:@"解除静音" disableImageName:@"" disableTitle:@"开启麦克风"];
        [self.micrBtn.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj isKindOfClass:[UIButton class]]) {
                    self.micrImageBtn = obj;
                    *stop = YES;
                }
            }];
        }];
        
        @WeakObj(self);
        [[self.micrBtn ysrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            @StrongObj(self);
            if ([self.delegate respondsToSelector:@selector(micrBtnClick:)]) {
                [self.delegate micrBtnClick:self];
            }
        }];
        
        self.cammerBtn = [self getBtnImageName:@"停止视频" title:@"停止视频" selectImageName:@"开启视频" selectTitle:@"开启视频" disableImageName:@"" disableTitle:@"开启摄像头"];
        [[self.cammerBtn ysrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            @StrongObj(self);
            if ([self.delegate respondsToSelector:@selector(cammerBtnClick:)]) {
                [self.delegate cammerBtnClick:self];
            }
        }];
        
        self.shareBtn = [self getBtnImageName:@"共享屏幕" title:@"共享屏幕" selectImageName:@"停止共享" selectTitle:@"停止共享" disableImageName:@"分组 copy 3" disableTitle:@"共享屏幕"];
        [[self.shareBtn ysrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            @StrongObj(self);
            if ([self.delegate respondsToSelector:@selector(shareBtnClick:)]) {
                [self.delegate shareBtnClick:self];
            }
        }];
        
        self.membersBtn = [self getBtnImageName:@"chengyuan" title:@"成员" selectImageName:@"" selectTitle:@"" disableImageName:@"" disableTitle:@""];
        [[self.membersBtn ysrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            @StrongObj(self);
            if ([self.delegate respondsToSelector:@selector(membersBtnClick:)]) {
                [self.delegate membersBtnClick:self];
            }
        }];
        
        self.moreBtn = [self getBtnImageName:@"iconfont_hexinhu" title:@"更多" selectImageName:@"" selectTitle:@"更多" disableImageName:@"" disableTitle:@""];
        [[self.moreBtn ysrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            @StrongObj(self);
            if ([self.delegate respondsToSelector:@selector(moreBtnClick:)]) {
                [self.delegate moreBtnClick:self];
            }
        }];
    }
    
    return self;
}

-(void)setBottomViewMicrButtonState:(BOOL)selected
{
    [self.micrBtn.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)obj;
                btn.selected = selected;
            }
        }];
    }];
    
    self.micrBtn.selected = selected;
}

-(void)setBottomViewCammerButtonState:(BOOL)selected {
    [self.cammerBtn.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)obj;
                btn.selected = selected;
            }
        }];
    }];
    
    self.cammerBtn.selected = selected;
}

-(void)setBottomViewShareButtonState:(BOOL)selected
{
    [self.shareBtn.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)obj;
                btn.selected = selected;
            }
        }];
    }];
    
    self.shareBtn.selected = selected;
}

-(void)setBottomViewShareButtonEnableState:(BOOL)enable
{
    [self.shareBtn.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)obj;
                btn.enabled = enable;
            }
        }];
    }];
    
    self.shareBtn.enabled = enable;
}

-(void)setBottomViewDoubleStreamBtnState:(BOOL)selected {
    [self.membersBtn.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[UIButton class]]) {
                UIButton *btn = (UIButton *)obj;
                btn.selected = selected;
            }
        }];
    }];
    
    self.membersBtn.selected = selected;
}



#pragma mark - Get button state
-(BOOL)mircBtnSelected {
    return self.micrBtn.selected;
}

-(BOOL)cammerBtnSelected {
    return self.cammerBtn.selected;
}

- (BOOL)doubleStreamBtnSelected {
    return self.membersBtn.selected;
}

- (BOOL)shareBtnSelected {
    return self.shareBtn.selected;
}

#pragma mark - UI Method
-(void)relayout:(BOOL)isLand {
    NSArray<UIView *> *array = @[self.micrBtn, self.cammerBtn, self.shareBtn, self.membersBtn, self.moreBtn];
    [array enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self addSubview:obj];
    }];
    
    [array ysmas_distributeViewsAlongAxis:(YSMASAxisTypeHorizontal) withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    [array ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.top.equalTo(self).with.offset(5);
        make.bottom.equalTo(self);
    }];

}

-(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);
    }];
    
    return btn;
}

-(void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}


@end
