//
//  ERTCMeetingSettingViewController.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/9/20.
//

#import "ERTCMeetingSettingViewController.h"
#import "EZSaaSVideoCMineVoiceExcitationTableViewCell.h"
#import "ERTCVideoMeetingManager.h"

@interface ERTCMeetingSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray<NSMutableArray<NSMutableDictionary *> *> *dataSource;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ERTCMeetingSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    
    self.title = @"设置";
    [self setUpUI];
    [self resetDataSource];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self resetDataSource];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

#pragma mark - DataSource
-(void)resetDataSource {
    self.dataSource = [NSMutableArray array];
    
    //段一
    NSMutableArray *array = [NSMutableArray array];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];

    [dict setObject:@"语音激励" forKey:@"name"];
    [dict setObject:@([ERTCVideoMeetingManager sharedInstance].enableAudioExcitation) forKey:@"value"];
    [dict setObject:[EZSaaSVideoCMineVoiceExcitationTableViewCell cellReuseId] forKey:@"cellId"];
    [array addObject:dict];
    [self.dataSource addObject:array];
    
    //段二
    array = [NSMutableArray array];
    dict = [NSMutableDictionary dictionary];

    
    dict = [NSMutableDictionary dictionary];
    [dict setObject:@"语音模式" forKey:@"name"];
    [dict setObject:@([ERTCVideoMeetingManager sharedInstance].enableAudioMode) forKey:@"value"];
    [dict setObject:[EZSaaSVideoCMineVoiceExcitationTableViewCell cellReuseId] forKey:@"cellId"];
    [array addObject:dict];
    
    [self.dataSource addObject:array];
    [self.tableView reloadData];
}

#pragma mark - Private Method
-(void)setUpUI {
    [self.view addSubview:self.tableView];
    [self.tableView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(kSafeMasBottom(self.view));
    }];
    
    UILabel *footerLabel = [[UILabel alloc] initWithFrame:(CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50))];
    footerLabel.text = @"当前版本V1.1.11(1.1.86)";
    footerLabel.textColor = [UIColor YSLabelColorPrimary];
    footerLabel.font = [UIFont systemFontOfSize:15];
    footerLabel.textAlignment = NSTextAlignmentCenter;
    [footerLabel layoutIfNeeded];
    
    self.tableView.tableFooterView = footerLabel;
}

#pragma mark - UITableViewDelegate UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *rows = self.dataSource[indexPath.section];
    NSDictionary *dict = rows[indexPath.row];
    NSString *name = dict[@"name"];
    NSString *cellId = dict[@"cellId"];
    BOOL value = [dict[@"value"] boolValue];
    BOOL showLine = [dict[@"showLine"] boolValue];
    
    UITableViewCell *viewcell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    viewcell.selectionStyle = UITableViewCellSelectionStyleNone;
    if([viewcell isKindOfClass:[EZSaaSVideoCMineVoiceExcitationTableViewCell class]]) {
        EZSaaSVideoCMineVoiceExcitationTableViewCell *cell = (EZSaaSVideoCMineVoiceExcitationTableViewCell *)viewcell;
        [cell configCell:name select:value showLine:showLine];
        
        @WeakObj(self);
        void (^extractedExpr)(BOOL) = ^(BOOL select) {
            @StrongObj(self);
            if ([name isEqualToString:@"语音激励"]) {
                [ERTCVideoMeetingManager sharedInstance].enableAudioExcitation = select;
            }
            else if ([name isEqualToString:@"语音模式"]) {
                [ERTCVideoMeetingManager sharedInstance].enableAudioMode = select;
                if (select == YES) {
                    [EZSaaSRTCProgressHUD showSuccessWithStatus:@"已开启语音模式"];
                } else {
                    [EZSaaSRTCProgressHUD showSuccessWithStatus:@"已关闭语音模式"];
                }
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ERTCSeetingAudioModelNotification" object:self];
            }
            
            [self resetDataSource];
        };
        cell.resultBlock = extractedExpr;
    }
    
    return viewcell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *rows = self.dataSource[section];
    return rows.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UILabel *headerLabel = [[UILabel alloc] init];
        headerLabel.backgroundColor = [UIColor clearColor];
        NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        style.alignment = NSTextAlignmentLeft;
        style.firstLineHeadIndent = 16;
        NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:@"个人设置" attributes:@{NSParagraphStyleAttributeName:style}];
        headerLabel.attributedText = attrText;
        headerLabel.font = [UIFont systemFontOfSize:12];
        headerLabel.textColor = [UIColor YSLabelColorTertiary];

        return headerLabel;
    }
    else{
        return [UIView new];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 30;
    }
    else if (section == 3) {
        return 30;
    }
    else
    {
        return 10;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    if (section == 0) {
        view.backgroundColor = [UIColor clearColor];
        UILabel *headerLabel = [[UILabel alloc] init];
        headerLabel.backgroundColor = [UIColor clearColor];
        headerLabel.font = [UIFont systemFontOfSize:12];
        headerLabel.textColor = [UIColor YSLabelColorTertiary];
        headerLabel.numberOfLines = 0;
        [view addSubview:headerLabel];
        
        [headerLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.top.equalTo(view).with.offset(5);
            make.left.equalTo(view).with.offset(15);
            make.right.equalTo(view).with.offset(-15);
        }];
        
        headerLabel.text = @"开启语音激励后，会将正在说话的参会成员显示为大图。";
    } else if (section == 1) {
        view.backgroundColor = [UIColor clearColor];
        UILabel *headerLabel = [[UILabel alloc] init];
        headerLabel.backgroundColor = [UIColor clearColor];
        headerLabel.font = [UIFont systemFontOfSize:12];
        headerLabel.textColor = [UIColor YSLabelColorTertiary];
        headerLabel.numberOfLines = 0;
        [view addSubview:headerLabel];
        
        [headerLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
            make.top.equalTo(view).with.offset(5);
            make.left.equalTo(view).with.offset(15);
            make.right.equalTo(view).with.offset(-15);
        }];
        
        headerLabel.text = @"停止接收其他成员视频，仅保留共享屏幕.";
    }
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 30;
    }
    else if (section == 1) {
        return 30;
    }
    return 10;
}

#pragma mark - Getter Method
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [_tableView registerClass:[EZSaaSVideoCMineVoiceExcitationTableViewCell class]
           forCellReuseIdentifier:[EZSaaSVideoCMineVoiceExcitationTableViewCell cellReuseId]];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 66;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    
    return _tableView;
}



@end
