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

#import "ERTCVideoSettingtViewController.h"
#import "ERTCVideoSettingCell.h"
#import "ERTCVideoSettingtModel.h"
#import "ERTCVideoMeetingManager.h"
#import "EZConfConfigSelectViewController.h"
#import "EZConfGlobalConfig.h"

@interface ERTCVideoSettingtViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<NSString *> *sectionTitles;
@property (nonatomic ,strong) NSMutableArray *dataArray;
@end

@implementation ERTCVideoSettingtViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"视频参数设置";
    [self.view addSubview:self.tableView];
    [self.tableView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(kSafeMasBottom(self.view));
    }];
    [self setDataSource];
    [self.tableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    ERTCVideoSettingtModel *bigVideoModel = self.dataArray[0];
    ERTCVideoSettingtModel *smallVideoModel = self.dataArray[1];
    [EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoFps = bigVideoModel.fps;
    [EZConfGlobalConfig sharedInstance].smallVideoEncParam.videoFps = smallVideoModel.fps;
    NSLog(@"大码流帧率:%ld 小码流帧率:%ld",bigVideoModel.fps,smallVideoModel.fps);
}

- (void)setDataSource {
    self.dataArray = [NSMutableArray array];
    ERTCVideoSettingtModel *model1 = [ERTCVideoSettingtModel new];
    model1.resolution = [EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoResolution;
    model1.fps = [EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoFps;
    model1.videoBitrate = [ERTCVideoEncParam recommendedBitrateForRes:model1.resolution];
    [self.dataArray addObject:model1];
    ERTCVideoSettingtModel *model2 = [ERTCVideoSettingtModel new];
    model2.resolution = [EZConfGlobalConfig sharedInstance].smallVideoEncParam.videoResolution;
    model2.fps = [EZConfGlobalConfig sharedInstance].smallVideoEncParam.videoFps;
    model2.videoBitrate = [ERTCVideoEncParam recommendedBitrateForRes:model2.resolution];
    [self.dataArray addObject:model2];
    ERTCVideoSettingtModel *model3 = [ERTCVideoSettingtModel new];
    model3.resolution = [EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoResolution;
    model3.fps = [EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoFps;
    model3.videoBitrate = [ERTCVideoEncParam recommendedBitrateForRes:model3.resolution];
    [self.dataArray addObject:model3];
    self.sectionTitles = @[@"大码流参数设置",@"小码流参数设置",@"屏幕共享参数设置"];
}

#pragma mark - UITableViewDelegate UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    ERTCVideoSettingCell *viewcell = [tableView dequeueReusableCellWithIdentifier:[ERTCVideoSettingCell cellReuseId]
                                                                                 forIndexPath:indexPath];
    ERTCVideoSettingtModel *model = self.dataArray[indexPath.section];
    [viewcell setData:model];
    return viewcell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [ERTCVideoSettingCell heightForCell];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    UILabel *headerLabel = [[UILabel alloc] init];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    headerLabel.textColor = [UIColor blackColor];

    [headerView addSubview:headerLabel];
    [headerLabel ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.centerY.equalTo(headerView);
        make.left.equalTo(kSafeMasLeft(headerView));//.with.offset(20);
    }];
    
    headerLabel.text = self.sectionTitles[section];
    
    return headerView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    EZConfConfigSelectViewController *vc = [[EZConfConfigSelectViewController alloc] initWithNibName:@"EZConfConfigSelectViewController" bundle:nil];
    vc.type = ConfigSelectType_Res;
    __weak __typeof(self)weakSelf = self;
    vc.selectResolutionBlock = ^(ERTCVideoResolution resolution) {
        ERTCVideoSettingtModel *model = weakSelf.dataArray[indexPath.section];
        model.resolution = resolution;
        model.videoBitrate = [ERTCVideoEncParam recommendedBitrateForRes:resolution];
        if (indexPath.section == 0) {
            [EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoResolution = resolution;
            [EZConfGlobalConfig sharedInstance].bigVideoEncParam.videoBitrate = [ERTCVideoEncParam recommendedBitrateForRes:resolution];
        } else if (indexPath.section == 1) {
            [EZConfGlobalConfig sharedInstance].smallVideoEncParam.videoResolution = resolution;
            [EZConfGlobalConfig sharedInstance].smallVideoEncParam.videoBitrate = [ERTCVideoEncParam recommendedBitrateForRes:resolution];
        }
        [weakSelf.tableView reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark-getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.backgroundColor = [UIColor lightGrayColor];
        [_tableView registerClass:[ERTCVideoSettingCell class]
           forCellReuseIdentifier:[ERTCVideoSettingCell cellReuseId]];
        [_tableView registerNib:[UINib nibWithNibName:@"ERTCVideoSettingCell" bundle:nil] forCellReuseIdentifier:[ERTCVideoSettingCell cellReuseId]];
    }
    return _tableView;
}

@end
