//
//  ERTCJoinRoomViewController.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/10/22.
//

#import "ERTCJoinRoomViewController.h"
#import "EZConfGlobalConfig.h"
#import "ERTCPlayerViewManager.h"
#import "ERTCVideoSettingtViewController.h"
#import "ERTCVideoMeetingViewController.h"

@interface ERTCJoinRoomViewController ()

@property (nonatomic, strong) UIButton *saveBtn;
@property (nonatomic, strong) UITextField *pastextField;
@property (nonatomic, strong) UITextField *memberNameTextField;
@property (nonatomic, strong) UIView *historyTapView;
@property (nonatomic, strong) UISwitch *audioSwitchBtn;
@property (nonatomic, strong) UISwitch *videoSwitchBtn;
@property (nonatomic, strong) UIImageView *moreImageView;

@property (nonatomic, assign) NSInteger maxLength;
@property (nonatomic, copy) NSString *password;
@property (nonatomic, strong) UILabel *lineIDContentLabel;
@end

@implementation ERTCJoinRoomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.maxLength = 100;
    self.password = @"";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpSubViews];
}

-(void)setUpSubViews {
    self.title = @"加入会议";
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scrollView];
    [scrollView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    UIView *contentView = [UIView new];
    contentView.backgroundColor = [UIColor clearColor];
    [scrollView addSubview:contentView];
    [contentView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    
    @WeakObj(self);
    //会议名称视图
    UIView *meetingIdView = [UIView new];
    meetingIdView.backgroundColor = [UIColor YSSystemBackgroundWhiteColor];
    [contentView addSubview:meetingIdView];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.text = @"会议ID";
    nameLabel.font = [UIFont systemFontOfSize:16];
    nameLabel.textColor = [UIColor YSSystemBlackColor];
    [meetingIdView addSubview:nameLabel];
    
    UITextField *textField = [[UITextField alloc] init];
    textField.textColor = [UIColor YSLabelColorSecondary];
    textField.placeholder = @"请输入会议ID";
    textField.font = [UIFont systemFontOfSize:16];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.textAlignment = NSTextAlignmentRight;
    [meetingIdView addSubview:textField];
    self.pastextField = textField;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor YSInputColor];
    [meetingIdView addSubview:lineView];
    
    [meetingIdView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).with.offset(10);
        make.height.ysmas_equalTo(50);
    }];
    
    [nameLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(meetingIdView).with.offset(16);
        make.centerY.equalTo(meetingIdView);
    }];
    
    [textField ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(nameLabel.ysmas_right).with.offset(5);
        make.top.bottom.equalTo(meetingIdView);
        make.right.equalTo(meetingIdView).with.offset(-35);
    }];
    
    [lineView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(meetingIdView).with.offset(16);
        make.right.equalTo(meetingIdView);
        make.bottom.equalTo(meetingIdView);
        make.height.ysmas_equalTo(0.5);
    }];
    
    [nameLabel setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
    [nameLabel setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
    
    //会议参会人名称视图
    UIView *meetingMemberView = [UIView new];
    meetingMemberView.backgroundColor = [UIColor YSSystemBackgroundWhiteColor];
    [contentView addSubview:meetingMemberView];
    
    UILabel *memberNameLabel = [[UILabel alloc] init];
    memberNameLabel.text = @"用户昵称";
    memberNameLabel.font = [UIFont systemFontOfSize:16];
    memberNameLabel.textColor = [UIColor YSSystemBlackColor];
    [meetingMemberView addSubview:memberNameLabel];
    
    textField = [[UITextField alloc] init];
    textField.textColor = [UIColor YSLabelColorSecondary];
    textField.placeholder = @"请输入您的昵称";
    textField.font = [UIFont systemFontOfSize:16];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.textAlignment = NSTextAlignmentRight;
    [meetingMemberView addSubview:textField];
    self.memberNameTextField = textField;
    
    [meetingMemberView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(meetingIdView.ysmas_bottom);
        make.height.ysmas_equalTo(50);
    }];
    
    [memberNameLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(meetingMemberView).with.offset(16);
        make.centerY.equalTo(meetingMemberView);
    }];
    
    [textField ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(memberNameLabel.ysmas_right).with.offset(5);
        make.top.bottom.equalTo(meetingMemberView);
        make.right.equalTo(meetingMemberView).with.offset(-35);
    }];
    
    [memberNameLabel setContentHuggingPriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
    [memberNameLabel setContentCompressionResistancePriority:(UILayoutPriorityRequired) forAxis:(UILayoutConstraintAxisHorizontal)];
    
    //加入会议按钮
    self.saveBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.saveBtn setTitle:@"加入会议" forState:(UIControlStateNormal)];
    [self.saveBtn setTitleColor:[UIColor YSSystemWhiteColor] forState:(UIControlStateNormal)];
    self.saveBtn.enabled = NO;
    self.saveBtn.backgroundColor = [UIColor colorWithRed:(0xB4/255.f) green:(0xD4/255.f) blue:(0xFD/255.f) alpha:1];
    self.saveBtn.layer.cornerRadius = 3;
    
    [self.saveBtn addTarget:self action:@selector(enterMeetingBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [contentView addSubview:self.saveBtn];
    
    [self.saveBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(24);
        make.right.equalTo(self.view).with.offset(-24);
        make.top.equalTo(meetingMemberView.ysmas_bottom).with.offset(24);
        make.height.ysmas_equalTo(49);
    }];
    
    //功能区
    UILabel *functionTitleLabel = [[UILabel alloc] init];
    functionTitleLabel.text = @"入会选项";
    functionTitleLabel.font = [UIFont systemFontOfSize:14];
    functionTitleLabel.textColor = [UIColor YSLabelColorTertiary];
    [contentView addSubview:functionTitleLabel];
    
    [functionTitleLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(16);
        make.top.equalTo(self.saveBtn.ysmas_bottom).with.offset(24);
    }];
    
    //音频按钮
    UIView *meetingAudioView = [UIView new];
    meetingAudioView.backgroundColor = [UIColor YSSystemBackgroundWhiteColor];
    [contentView addSubview:meetingAudioView];
    
    UILabel *audioLabel = [[UILabel alloc] init];
    audioLabel.text = @"自动连接语音";
    audioLabel.font = [UIFont systemFontOfSize:16];
    audioLabel.textColor = [UIColor YSSystemBlackColor];
    [meetingAudioView addSubview:audioLabel];
    
    UISwitch *switchBtn = [[UISwitch alloc] init];
    switchBtn.onTintColor = [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1];
    [switchBtn addTarget:self action:@selector(audioValueChanged:) forControlEvents:(UIControlEventValueChanged)];
    [meetingAudioView addSubview:switchBtn];
    
    switchBtn.on = YES;
    self.audioSwitchBtn = switchBtn;
    
    lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor YSInputColor];
    [meetingAudioView addSubview:lineView];
    
    [meetingAudioView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(functionTitleLabel.ysmas_bottom).with.offset(8);
        make.height.ysmas_equalTo(50);
    }];
    
    [audioLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(meetingAudioView).with.offset(16);
        make.centerY.equalTo(meetingAudioView);
    }];
    
    [switchBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.centerY.equalTo(meetingAudioView);
        make.right.equalTo(meetingAudioView).with.offset(-19);
    }];
    
    [lineView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(meetingAudioView).with.offset(16);
        make.right.equalTo(meetingAudioView);
        make.bottom.equalTo(meetingAudioView);
        make.height.ysmas_equalTo(0.5);
    }];
    
    //视频按钮
    UIView *meetingVideoView = [UIView new];
    meetingVideoView.backgroundColor = [UIColor YSSystemBackgroundWhiteColor];
    [contentView addSubview:meetingVideoView];
    
    UILabel *videoLabel = [[UILabel alloc] init];
    videoLabel.text = @"入会开启摄像头";
    videoLabel.font = [UIFont systemFontOfSize:16];
    videoLabel.textColor = [UIColor YSSystemBlackColor];
    [meetingVideoView addSubview:videoLabel];
    
    switchBtn = [[UISwitch alloc] init];
    switchBtn.onTintColor = [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1];
    [switchBtn addTarget:self action:@selector(videoValueChanged:) forControlEvents:(UIControlEventValueChanged)];
    [meetingVideoView addSubview:switchBtn];
    switchBtn.on = YES;
    self.videoSwitchBtn = switchBtn;
    
    lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor YSInputColor];
    [meetingVideoView addSubview:lineView];
    
    [meetingVideoView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(meetingAudioView.ysmas_bottom);
        make.height.ysmas_equalTo(50);
//        make.bottom.equalTo(contentView);
    }];
    
    [videoLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(meetingVideoView).with.offset(16);
        make.centerY.equalTo(meetingVideoView);
    }];
    
    [switchBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.centerY.equalTo(meetingVideoView);
        make.right.equalTo(meetingVideoView).with.offset(-19);
    }];
    
    [lineView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(meetingVideoView).with.offset(16);
        make.right.equalTo(meetingVideoView);
        make.bottom.equalTo(meetingVideoView);
        make.height.ysmas_equalTo(0.5);
    }];
    
    //输入框控制逻辑
    [self.pastextField.ysrac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @StrongObj(self);
        NSString *toBeString = self.pastextField.text;
        NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage;
        if ([lang isEqualToString:@"zh-Hans"])
        {
            // 中文输入
            UITextRange *selectedRange = [self.pastextField markedTextRange];
            
            // 获取高亮部分
            UITextPosition *position = [self.pastextField positionFromPosition:selectedRange.start
                                                                        offset:0];
            if (!position)
            {// 没有高亮选择的字，则对已输入的文字进行字数统计和限制
                if (toBeString.length > self.maxLength) {
                    self.pastextField.text = [toBeString substringToIndex:self.maxLength];
                }
            }
            else
            {//有高亮选择的字符串，则暂不对文字进行统计和限制
                
            }
        }
        else
        {//中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
            if (toBeString.length > self.maxLength) {
                self.pastextField.text = [toBeString substringToIndex:self.maxLength];
            }
        }
        
        [self updateEnterBtnStatus];
    }];
    
    [self.memberNameTextField.ysrac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @StrongObj(self);
        NSString *toBeString = self.memberNameTextField.text;
        NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage;
        if ([lang isEqualToString:@"zh-Hans"])
        {
            // 中文输入
            UITextRange *selectedRange = [self.memberNameTextField markedTextRange];
            
            // 获取高亮部分
            UITextPosition *position = [self.memberNameTextField positionFromPosition:selectedRange.start
                                                                        offset:0];
            if (!position)
            {// 没有高亮选择的字，则对已输入的文字进行字数统计和限制
                if (toBeString.length > self.maxLength) {
                    self.memberNameTextField.text = [toBeString substringToIndex:self.maxLength];
                }
            }
            else
            {//有高亮选择的字符串，则暂不对文字进行统计和限制
                
            }
        }
        else
        {//中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
            if (toBeString.length > self.maxLength) {
                self.memberNameTextField.text = [toBeString substringToIndex:self.maxLength];
            }
        }
        
        [self updateEnterBtnStatus];
    }];
    [self.pastextField becomeFirstResponder];
    
    //会议设置
    UIView *beautyVideoView = [UIView new];
    beautyVideoView.backgroundColor = [UIColor YSSystemBackgroundWhiteColor];
    [contentView addSubview:beautyVideoView];
    
    UILabel *beautyLabel = [[UILabel alloc] init];
    beautyLabel.text = @"会议设置";
    beautyLabel.font = [UIFont systemFontOfSize:16];
    beautyLabel.textColor = [UIColor YSSystemBlackColor];
    [beautyVideoView addSubview:beautyLabel];
    
    UIButton *beautyBtn = [[UIButton alloc] init];
    beautyBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    beautyBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [beautyBtn setTitle:@"点击设置" forState:(UIControlStateNormal)];
    [beautyBtn setTitleColor:[UIColor YSLabelColorTertiary] forState:(UIControlStateNormal)];
    [beautyBtn addTarget:self action:@selector(settingBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [beautyVideoView addSubview:beautyBtn];
    
    [beautyVideoView addSubview:self.moreImageView];
    
    lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor YSInputColor];
    [beautyVideoView addSubview:lineView];
    
    [beautyVideoView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(meetingVideoView.ysmas_bottom);
        make.height.ysmas_equalTo(50);
    }];
    
    [beautyLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(beautyVideoView).with.offset(16);
        make.centerY.equalTo(beautyVideoView);
    }];
    
    [beautyBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.centerY.equalTo(beautyVideoView);
        make.right.equalTo(self.moreImageView.ysmas_left).with.offset(-5);
    }];
    
    [self.moreImageView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.centerY.equalTo(beautyVideoView);
        make.right.equalTo(beautyVideoView).with.offset(-19);
    }];
    
    [lineView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(beautyVideoView).with.offset(16);
        make.right.equalTo(beautyVideoView);
        make.bottom.equalTo(beautyVideoView);
        make.height.ysmas_equalTo(0.5);
    }];
    
    //线路选择
    //会议设置
    UIView *lineIDView = [UIView new];
    beautyVideoView.backgroundColor = [UIColor YSSystemBackgroundWhiteColor];
    [contentView addSubview:lineIDView];
    
    UILabel *lineIDLabel = [[UILabel alloc] init];
    lineIDLabel.text = @"线路选择";
    lineIDLabel.font = [UIFont systemFontOfSize:16];
    lineIDLabel.textColor = [UIColor YSSystemBlackColor];
    [lineIDView addSubview:lineIDLabel];
    
    UIButton *lineIDBtn = [[UIButton alloc] init];
    lineIDBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    lineIDBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
    [lineIDBtn setTitle:@"点击设置" forState:(UIControlStateNormal)];
    [lineIDBtn setTitleColor:[UIColor YSLabelColorTertiary] forState:(UIControlStateNormal)];
    [lineIDBtn addTarget:self action:@selector(lineIDBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [lineIDView addSubview:lineIDBtn];
    
    UILabel *lineIDContentLabel = [[UILabel alloc] init];
    self.lineIDContentLabel = lineIDContentLabel;
    lineIDContentLabel.text = @"(1)";
    lineIDContentLabel.font = [UIFont systemFontOfSize:16];
    lineIDContentLabel.textColor = [UIColor YSSystemBlackColor];
    [lineIDView addSubview:lineIDContentLabel];
    
    [lineIDView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(beautyVideoView.ysmas_bottom);
        make.height.ysmas_equalTo(50);
        make.bottom.equalTo(contentView);
    }];
    
    [lineIDLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.left.equalTo(lineIDView).with.offset(16);
        make.centerY.equalTo(lineIDView);
    }];
    
    [lineIDBtn ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.centerY.equalTo(lineIDView);
        make.right.equalTo(lineIDView).with.offset(-19);
        
    }];
    
    [self.lineIDContentLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.centerY.equalTo(lineIDView);
        make.left.equalTo(lineIDLabel.ysmas_right).with.offset(5);
    }];
    
    self.pastextField.text = @"";
    self.memberNameTextField.text = @"";
    
}


-(void)updateEnterBtnStatus {
    if (self.pastextField.text.length > 0 && self.memberNameTextField.text.length > 0) {
        self.saveBtn.enabled = YES;
        self.saveBtn.backgroundColor = [UIColor colorWithRed:(0x3E/255.f) green:(0x80/255.f) blue:(0xFF/255.f) alpha:1];
    } else {
        self.saveBtn.enabled = NO;
        self.saveBtn.backgroundColor = [UIColor colorWithRed:(0xB4/255.f) green:(0xD4/255.f) blue:(0xFD/255.f) alpha:1];
    }
}

-(void)enterMeetingBtnClick:(UIButton *)btn {
    ERTCLogI(@"大码流---分辨率:%@ 大码流帧率:%ld 大码流码率:%d",[EZConfGlobalConfig desForRes:[EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoResolution],[EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoFps,[EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoBitrate);
    ERTCLogI(@"小码流---分辨率:%@ 大码流帧率:%ld 大码流码率:%d",[EZConfGlobalConfig desForRes:[EZConfGlobalConfig sharedInstance].smallVideoEncParam.videoResolution],[EZConfGlobalConfig sharedInstance].smallVideoEncParam.videoFps,[EZConfGlobalConfig sharedInstance].smallVideoEncParam.videoBitrate);
    [ERTCPlayerViewManager sharedInstance].videoState = self.videoSwitchBtn.on;
    [ERTCPlayerViewManager sharedInstance].audioState = self.audioSwitchBtn.on;
    ERTCVideoMeetingViewController *vc = [ERTCVideoMeetingViewController new];
    vc.roomId = self.pastextField.text;
    vc.userId = self.memberNameTextField.text;
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - UISwitch Event
-(void)audioValueChanged:(UISwitch *)switchBtn {

}

-(void)videoValueChanged:(UISwitch *)switchBtn {

}

-(void)settingBtnClick:(UIButton *)btn {
    ERTCVideoSettingtViewController *settingVC= [ERTCVideoSettingtViewController new];
    [self.navigationController pushViewController:settingVC animated:YES];
}

- (void)lineIDBtnClick:(UIButton *)btn {
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"线路选择"
                                                                           message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1=[UIAlertAction actionWithTitle:@"外网" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.lineIDContentLabel.text = @"(1)";
       
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction *action2=[UIAlertAction actionWithTitle:@"内网" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.lineIDContentLabel.text = @"(2)";
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction *action_cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertController addAction:action1];
    [alertController addAction:action2];
    
    [alertController addAction:action_cancel];
    [self presentViewController:alertController animated:YES completion:nil];
}

-(UIImageView *)moreImageView {
    if (!_moreImageView) {
        UIImage *image = [UIImage imageNamed:@"icon_right"];
        _moreImageView = [[UIImageView alloc] initWithImage:image];
    }
    
    return _moreImageView;
}
@end
