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

#import "ERTCVideoMeetingTopView.h"


@interface ERTCVideoMeetingTopView ()

@property (nonatomic, strong) UIButton *speakerBtn;
@property (nonatomic, strong) UIButton *switchCammerBtn;
@property (nonatomic, strong) UIImageView *safeImageView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIView *signBottomView;

@property (nonatomic, strong) UILabel *meetingIdNameLabel;

@property (nonatomic, strong) UIView *infoView;
@property (nonatomic, assign) int meetingTime;

@property (nonatomic, strong) UIButton *exitBtn;
//@property (nonatomic, strong) UIButton *showScreenShareButton;


@end

@implementation ERTCVideoMeetingTopView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        
        [self addSubview:self.speakerBtn];
        [self addSubview:self.switchCammerBtn];
        
        self.infoView = [UIView new];
        self.infoView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.infoView];
        [self.infoView addSubview:self.meetingIdNameLabel];
        [self.infoView addSubview:self.safeImageView];
        [self.infoView addSubview:self.signBottomView];
        [self.signBottomView addSubview:self.timeLabel];
        
        [self addSubview:self.exitBtn];
//        [self addSubview:self.showScreenShareButton];
        
        self.meetingTime = 0;
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapInfoView)];
        [self.infoView addGestureRecognizer:tapGes];
    }
    
    return self;
}

- (void)startTimer {
    self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerSelector:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}

-(void)tapInfoView {
    if ([self.delegate respondsToSelector:@selector(infoViewClick:)]) {
        [self.delegate infoViewClick:self];
    }
}

-(void)timerSelector:(NSTimer *)timer {
    self.meetingTime++;
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02d",self.meetingTime/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02d",(self.meetingTime%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02d",self.meetingTime%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    self.timeLabel.text = format_time;
}

-(void)setMeetingId:(NSString *)meetingId {
    _meetingId = meetingId;
    self.meetingIdNameLabel.text = meetingId;
}

-(void)setSignUrl:(NSString *)signUrl {
    _signUrl = signUrl;
}

-(void)exitMeeting {
    [self.timer invalidate];
    self.timer = nil;
}

-(void)relayout:(BOOL)isLand {
    NSLog(@"EZSaaSVideoCMeetingTopView relayout isLand:%d", isLand);
    [self.speakerBtn ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(17);
        make.centerY.equalTo(self.infoView);
        make.width.height.ysmas_equalTo(30);
    }];
    
    [self.switchCammerBtn ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(self.speakerBtn.ysmas_right).with.offset(5);
        make.centerY.equalTo(self.infoView);
        make.width.height.equalTo(self.speakerBtn);
    }];
    
    if (isLand) {
        [self.infoView ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    } else {
        [self.infoView ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
            make.bottom.equalTo(self).with.offset(-5);
            make.centerX.equalTo(self);
        }];
    }
    
    [self.meetingIdNameLabel ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
        make.left.top.equalTo(self.infoView);
        make.right.equalTo(self.safeImageView.ysmas_left).with.offset(-2);
        make.bottom.equalTo(self.signBottomView.ysmas_top).with.offset(-2);
    }];
    
    [self.safeImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.right.equalTo(self.infoView);
        make.centerY.equalTo(self.meetingIdNameLabel);
    }];
    
    [self.signBottomView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.bottom.equalTo(self.infoView).with.offset(-2);
        make.height.ysmas_equalTo(17);
        make.centerX.equalTo(self.infoView);
    }];
    
    [self.timeLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.signBottomView);
        make.right.equalTo(self.signBottomView).with.offset(-2);
    }];
    
    [self.exitBtn ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
        make.right.equalTo(self).with.offset(-10);
        make.top.bottom.equalTo(self.infoView);
        make.width.ysmas_equalTo(60);
    }];
    
//    [self.showScreenShareButton ysmas_remakeConstraints:^(YSMASConstraintMaker *make) {
//        make.right.equalTo(self.exitBtn.ysmas_left).with.offset(-10);
//        make.top.bottom.equalTo(self.infoView);
//        make.width.ysmas_equalTo(80);
//    }];
    
    if (self.isHost == NO) {
        [self.exitBtn setTitle:@"离开" forState:(UIControlStateNormal)];
    }
    else
    {
        [self.exitBtn setTitle:@"结束" forState:(UIControlStateNormal)];
    }
    
    [self setNeedsUpdateConstraints];
}

-(void)setIsHost:(BOOL)isHost
{
    _isHost = isHost;
    if (isHost == NO) {
        [self.exitBtn setTitle:@"离开" forState:(UIControlStateNormal)];
    }
    else
    {
        [self.exitBtn setTitle:@"结束" forState:(UIControlStateNormal)];
    }
}

-(void)speakerBtnStateSet:(BOOL)select {
    self.speakerBtn.selected = select;
}

-(void)switchCammerBtnEnable:(BOOL)enable {
    self.switchCammerBtn.enabled = enable;
}

- (void)showScreenShareButton:(BOOL)show {
//    self.showScreenShareButton.hidden = !show;
}

#pragma mark - Getter Method
-(UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.text = @"00:00:00";
    }
    
    return _timeLabel;
}

-(UILabel *)meetingIdNameLabel {
    if (!_meetingIdNameLabel) {
        _meetingIdNameLabel = [[UILabel alloc] init];
        _meetingIdNameLabel.font = [UIFont systemFontOfSize:14];
        _meetingIdNameLabel.textColor = [UIColor whiteColor];
        _meetingIdNameLabel.text = @"123 456 789";
        _meetingIdNameLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _meetingIdNameLabel;
}

-(UIButton *)speakerBtn {
    if (!_speakerBtn) {
        _speakerBtn = [[UIButton alloc] init];
        [_speakerBtn setImage:[UIImage imageNamed:@"小-1"] forState:UIControlStateNormal];
        [_speakerBtn setImage:[UIImage imageNamed:@"小-1-关闭"]
                          forState:(UIControlStateSelected)];
        
        @WeakObj(self);
        [[_speakerBtn ysrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            @StrongObj(self);
            if ([self.delegate respondsToSelector:@selector(switchSpeakerBtnClick:speaker:)]) {
                [self.delegate switchSpeakerBtnClick:self speaker:self.speakerBtn.selected];
            }
            self.speakerBtn.selected = !self.speakerBtn.selected;

        }];
    }
    
    return _speakerBtn;
}

-(UIButton *)switchCammerBtn
{
    if (!_switchCammerBtn) {
        _switchCammerBtn = [[UIButton alloc] init];
        [_switchCammerBtn setImage:[UIImage imageNamed:@"小-2"]
                          forState:(UIControlStateNormal)];
        
        @WeakObj(self);
        [[_switchCammerBtn ysrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            @StrongObj(self);
            if ([self.delegate respondsToSelector:@selector(switchCammerBtnClick:camera:)]) {
                [self.delegate switchCammerBtnClick:self camera:self.switchCammerBtn.selected];
            }
        }];
    }
    
    return _switchCammerBtn;
}

-(UIImageView *)safeImageView
{
    if (!_safeImageView) {
        _safeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"小-3"]];
    }
    
    return _safeImageView;
}

-(UIView *)signBottomView
{
    if (!_signBottomView) {
        _signBottomView = [UIView new];
        _signBottomView.backgroundColor = [UIColor clearColor];
    }
    
    return _signBottomView;
}

-(UIButton *)exitBtn
{
    if (!_exitBtn) {
        _exitBtn = [[UIButton alloc] init];
        _exitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_exitBtn setTitle:@"结束会议" forState:(UIControlStateNormal)];
        [_exitBtn setTitleColor:EZSaaSRTCUIColorHex(0xFB4848) forState:(UIControlStateNormal)];
        
        @WeakObj(self);
        [[_exitBtn ysrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            @StrongObj(self);
            if ([self.delegate respondsToSelector:@selector(exitMeetingBtnClick:)]) {
                [self.delegate exitMeetingBtnClick:self];
            }
        }];
    }
    
    return _exitBtn;
}

//- (UIButton *)showScreenShareButton {
//    if (!_showScreenShareButton) {
//        _showScreenShareButton = [[UIButton alloc] init];
//        _showScreenShareButton.titleLabel.font = [UIFont systemFontOfSize:14];
//        [_showScreenShareButton setTitle:@"观看屏幕共享" forState:(UIControlStateNormal)];
//        [_showScreenShareButton setTitle:@"取消屏幕共享" forState:(UIControlStateSelected)];
//        [_showScreenShareButton setTitleColor:EZSaaSRTCUIColorHex(0xFB4848) forState:(UIControlStateNormal)];
//        _showScreenShareButton.hidden = YES;
//        @WeakObj(self);
//        [[_showScreenShareButton ysrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
//            @StrongObj(self);
//            self.showScreenShareButton.selected = !self.showScreenShareButton.selected;
//            if ([self.delegate respondsToSelector:@selector(showScreenShareButtonClick:)]) {
//                [self.delegate showScreenShareButtonClick:self.showScreenShareButton.selected];
//            }
//        }];
//    }
//    return _showScreenShareButton;
//}

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

@end
