//
//  EZConfConfigSelectViewController.m
//  ERTCDemo
//
//  Created by Harper Kan on 2021/11/3.
//

#import "EZConfConfigSelectViewController.h"
#import <ERTC/ERTC.h>
#import "EZConfGlobalConfig.h"
#import "UIView+Toast.h"

@interface EZConfConfigSelectViewController ()
@property (nonatomic, strong) NSArray *resList;
@property (nonatomic, strong) NSArray *fpsList;
@property (nonatomic, strong) NSArray *qualityIntervalList;
@end

@implementation EZConfConfigSelectViewController

 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    self.resList = @[@(ERTCVideoResolution_120_120),
                     
                     ///宽高比 1:1 分辨率 160x160；建议码率（VideoCall）100kbps; 建议码率（LIVE）150kbps。
                     @(ERTCVideoResolution_160_160),

                     ///宽高比 1:1；分辨率 270x270；建议码率（VideoCall）200kbps; 建议码率（LIVE）300kbps。
                     @(ERTCVideoResolution_270_270),

                     ///宽高比 1:1；分辨率 480x480；建议码率（VideoCall）350kbps; 建议码率（LIVE）500kbps。
                     @(ERTCVideoResolution_480_480),

                     ///宽高比4:3；分辨率 160x120；建议码率（VideoCall）100kbps; 建议码率（LIVE）150kbps。
                     @(ERTCVideoResolution_160_120),

                     ///宽高比 4:3；分辨率 240x180；建议码率（VideoCall）150kbps; 建议码率（LIVE）250kbps。
                     @(ERTCVideoResolution_240_180),

                     ///宽高比 4:3；分辨率 280x210；建议码率（VideoCall）200kbps; 建议码率（LIVE）300kbps。
                     @(ERTCVideoResolution_280_210),

                     ///宽高比 4:3；分辨率 320x240；建议码率（VideoCall）250kbps; 建议码率（LIVE）375kbps。
                     @(ERTCVideoResolution_320_240),

                     ///宽高比 4:3；分辨率 400x300；建议码率（VideoCall）300kbps; 建议码率（LIVE）450kbps。
                     @(ERTCVideoResolution_400_300),

                     ///宽高比 4:3；分辨率 480x360；建议码率（VideoCall）400kbps; 建议码率（LIVE）600kbps。
                     @(ERTCVideoResolution_480_360),

                     ///宽高比 4:3；分辨率 640x480；建议码率（VideoCall）600kbps; 建议码率（LIVE）900kbps。
                     @(ERTCVideoResolution_640_480),

                     ///宽高比 4:3；分辨率 960x720；建议码率（VideoCall）1000kbps; 建议码率（LIVE）1500kbps。
                     @(ERTCVideoResolution_960_720),

                     ///宽高比 16:9；分辨率 160x90；建议码率（VideoCall）150kbps; 建议码率（LIVE）250kbps。
                     @(ERTCVideoResolution_160_90),

                     ///宽高比 16:9；分辨率 256x144；建议码率（VideoCall）200kbps; 建议码率（LIVE）300kbps。
                     @(ERTCVideoResolution_256_144),

                     ///宽高比 16:9；分辨率 320x180；建议码率（VideoCall）250kbps; 建议码率（LIVE）400kbps。
                     @(ERTCVideoResolution_320_180),

                     ///宽高比 16:9；分辨率 480x270；建议码率（VideoCall）350kbps; 建议码率（LIVE）550kbps。
                     @(ERTCVideoResolution_480_270),

                     ///宽高比 16:9；分辨率 640x360；建议码率（VideoCall）500kbps; 建议码率（LIVE）900kbps。
                     @(ERTCVideoResolution_640_360),

                     ///宽高比 16:9；分辨率 960x540；建议码率（VideoCall）850kbps; 建议码率（LIVE）1300kbps。
                     @(ERTCVideoResolution_960_540),

                     ///宽高比 16:9；分辨率 1280x720；建议码率（VideoCall）1200kbps; 建议码率（LIVE）1800kbps。
                     @(ERTCVideoResolution_1280_720),

                     ///宽高比 16:9；分辨率 1920x1080；建议码率（VideoCall）2000kbps; 建议码率（LIVE）3000kbps。
                     @(ERTCVideoResolution_1920_1080),
    ];
    self.fpsList = @[@(ERTCVideoFPS_7),@(ERTCVideoFPS_10),@(ERTCVideoFPS_15),@(ERTCVideoFPS_20)];
    self.qualityIntervalList = @[@(1),@(2),@(3),@(4),@(5),@(6),@(7),@(8),@(9),@(10)];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.type == ConfigSelectType_Res) {
        return self.resList.count;
    }else if (self.type == ConfigSelectType_FPS) {
        return self.fpsList.count;
    }else if (self.type == ConfigSelectType_Quality) {
        return self.qualityIntervalList.count;
    }
    else {
        return 0;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                                      reuseIdentifier: @"Cell"];
    }
    if (self.type == ConfigSelectType_Res) {
        ERTCVideoResolution res = [self.resList[indexPath.row] unsignedLongValue];
        cell.textLabel.text = [EZConfGlobalConfig desForRes:res];
        if (res == g_videoEncParam.videoResolution) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }else if (self.type == ConfigSelectType_FPS) {
        int fps = [self.fpsList[indexPath.row] intValue];
        cell.textLabel.text = [NSString stringWithFormat:@"%d帧",fps];
        if (fps == g_videoEncParam.videoFps) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    } else if (self.type == ConfigSelectType_Quality) {
        int interval = [self.qualityIntervalList[indexPath.row] intValue];
        cell.textLabel.text = [NSString stringWithFormat:@"%d秒",interval];
        if (interval == [EZConfGlobalConfig sharedInstance].qualityInterval) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    
    return cell;
}

#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.type == ConfigSelectType_Res) {
        ERTCVideoResolution res = [self.resList[indexPath.row] unsignedLongValue];
        g_videoEncParam.videoResolution = res;
        g_videoEncParam.videoBitrate = [ERTCVideoEncParam recommendedBitrateForRes:res];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.view.window makeToast:@"配置分辨率后，码率会重置为相应的推荐设置" duration:3.0 position:CSToastPositionCenter];
        });
        
    }else if (self.type == ConfigSelectType_FPS) {
        int fps = [self.fpsList[indexPath.row] intValue];
        g_videoEncParam.videoFps = fps;
    }else if (self.type == ConfigSelectType_Quality) {
        int interval = [self.qualityIntervalList[indexPath.row] intValue];
        [EZConfGlobalConfig sharedInstance].qualityInterval = interval;
    }
    
    [self.tableView reloadData];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
