//
//  EZConfConfigViewController.m
//  ERTCDemo
//
//  Created by Harper Kan on 2021/11/2.
//

#import "EZConfConfigViewController.h"
#import "EZConfGlobalConfig.h"
#import "EZConfConfigSelectViewController.h"
#import "UIView+Toast.h"

#define RES_TAG  (1000)
#define FPS_TAG  (1001)
#define BPS_TAG  (1002)
#define FILL_MODE_TAG (1003)
#define Quality_Interval (1004)
#define HardDecodePriority (1005)
#define showNewView (1006)
@interface EZConfConfigViewController ()
@property (nonatomic, strong) NSArray *configsTag;
@end

@implementation EZConfConfigViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"参数配置";
    // Uncomment the following line to preserve selection between presentations.
//     self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    self.configsTag = @[@(RES_TAG),@(FPS_TAG),@(BPS_TAG),@(FILL_MODE_TAG),@(Quality_Interval),@(HardDecodePriority)];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}


- (void)showBPSInutView {
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"输入码率，不小于100（100kbps)"
                                                                           message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

        [textField setText:[NSString stringWithFormat:@"%d",g_videoEncParam.videoBitrate]];
       
    }];
    UIAlertAction *action=[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        int bps = [alertController.textFields firstObject].text.intValue;
        if (bps >= 100) {
            g_videoEncParam.videoBitrate = bps;
            [self.tableView reloadData];
        }else {
            [self.view makeToast:@"输入非法" duration:2.0 position:CSToastPositionCenter];
        }
    }];
    UIAlertAction *action_cancel=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertController addAction:action];
    [alertController addAction:action_cancel];
    [self presentViewController:alertController animated:YES completion:nil];
}


- (void)showModeInputView {
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"显示模式"
                                                                           message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1=[UIAlertAction actionWithTitle:@"Fill模式,填充,画面可能被裁剪" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        g_fillMode = ERTCVideoFillMode_Fill;
        [self.tableView reloadData];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction *action2=[UIAlertAction actionWithTitle:@"Fit模式,等比例缩放,画面可能有黑边" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        g_fillMode = ERTCVideoFillMode_Fit;
        [self.tableView reloadData];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction *action3=[UIAlertAction actionWithTitle:@"填充模式,保持宽高比,画面可能被裁剪" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        g_fillMode = ERTCVideoFillMode_AspectFill;
        [self.tableView reloadData];
        [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:action3];
    [alertController addAction:action_cancel];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)selectHardDecodePriority {
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"是否优先硬解"
                                                                           message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1=[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [EZConfGlobalConfig sharedInstance].hardDecodePriority = YES;
        [self.tableView reloadData];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction *action2=[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [EZConfGlobalConfig sharedInstance].hardDecodePriority = false;
        [self.tableView reloadData];
        [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];
}

- (void)selectShowNewView {
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"是否显示改版后的页面"
                                                                           message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1=[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [EZConfGlobalConfig sharedInstance].isEZRTC = YES;
        [self.tableView reloadData];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction *action2=[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [EZConfGlobalConfig sharedInstance].isEZRTC = false;
        [self.tableView reloadData];
        [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];
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.configsTag.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: @"Cell"];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    // Configure the cell...

    cell.tag = [self.configsTag[indexPath.row] integerValue];
    if (cell.tag == RES_TAG) {
        cell.textLabel.text = @"分辨率";
        cell.detailTextLabel.text = [EZConfGlobalConfig desForRes:g_videoEncParam.videoResolution];
    }else if (cell.tag == FPS_TAG) {
        cell.textLabel.text = @"帧率";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%lu帧",(unsigned long)g_videoEncParam.videoFps];
    }else if (cell.tag == BPS_TAG) {
        cell.textLabel.text = @"码率(单位kbps)";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",g_videoEncParam.videoBitrate];
    }else if (cell.tag == FILL_MODE_TAG) {
        cell.textLabel.text = @"画面显示模式";
        if (g_fillMode == ERTCVideoFillMode_Fill) {
            cell.detailTextLabel.text =  @"Fill模式,填充,画面可能被裁剪";
        } else if (g_fillMode == ERTCVideoFillMode_Fit) {
            cell.detailTextLabel.text = @"Fit模式,等比例缩放,画面可能有黑边";
        } else {
            cell.detailTextLabel.text =  @"保持宽高比,画面可能被裁剪";
        }
       
    } else if (cell.tag == Quality_Interval){
        cell.textLabel.text = @"质量回调时间间隔";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%ds",[EZConfGlobalConfig sharedInstance].qualityInterval];
    } else if (cell.tag == HardDecodePriority) {
        cell.textLabel.text = @"是否优先硬解";
        cell.detailTextLabel.text = [EZConfGlobalConfig sharedInstance].hardDecodePriority ? @"是":@"否";
    } else if (cell.tag == showNewView) {
        cell.textLabel.text = @"是否显示改版后的页面";
        cell.detailTextLabel.text = [EZConfGlobalConfig sharedInstance].isEZRTC ? @"是":@"否";
    }
    
    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 {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    EZConfConfigSelectViewController *vc = [[EZConfConfigSelectViewController alloc] initWithNibName:@"EZConfConfigSelectViewController" bundle:nil];
    switch (cell.tag) {
        case RES_TAG:
        {
            vc.type = ConfigSelectType_Res;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case FPS_TAG:
        {
            vc.type = ConfigSelectType_FPS;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case FILL_MODE_TAG:
        {
            [self showModeInputView];
        }
            break;
        case BPS_TAG:
        {
            [self showBPSInutView];
        }
            break;
        case Quality_Interval:
        {
            vc.type = ConfigSelectType_Quality;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case HardDecodePriority:
        {
            [self selectHardDecodePriority];
        }
            break;
        case showNewView:
        {
            [self selectShowNewView];
        }
            break;
        default:
            break;
    }
    
}

@end
