//
//  NvSetUpViewController.m
//  SDKDemo
//
//  Created by ms20180425 on 2018/5/24.
//  Copyright © 2018年 刘东旭. All rights reserved.
//

#import "NvSetUpViewController.h"
#import "NvsStreamingContext.h"
#import "CrashListViewController.h"
#import "MSSysAlertUtil.h"

@interface NvSetUpViewController ()<UITextFieldDelegate>

//type: 0 拍摄分辨率  1输出分辨率
@property (nonatomic, copy) void(^btnClickBlock)(UIButton *btn, NSInteger type);

//硬件编码器支持
@property (nonatomic, strong) NvSwitchView *hardwareSwitch;

//背景模糊填充
@property (nonatomic, strong) NvSwitchView *fillSwitch;

//码率
@property (nonatomic, strong) UITextField *bitField;

@property (nonatomic, assign) int64_t advice;

//点击版本号的个数
@property (nonatomic ,assign) int bugCount;

@end

@implementation NvSetUpViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"%s",__func__);
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    
    [self addSubViews];
    
    // Do any additional setup after loading the view.
}

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

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

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.bitField resignFirstResponder];
}

#pragma mark UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return [self validateNumberByRegExp:string];
}

//限制只能输入数字
- (BOOL)validateNumberByRegExp:(NSString*)string {
    BOOL isValid = YES;
    NSUInteger len = string.length;
    if (len > 0) {
        NSString *numberRegex = @"^[0-9.]*$";
        NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex];
        isValid = [numberPredicate evaluateWithObject:string];
    }
    return isValid;
}

#pragma mark 拍摄1080，720选择点击事件
- (void)SResolutionClick:(UIButton *)btn{
    btn.selected = YES;
    self.btnClickBlock(btn, 0);
}

#pragma mark 输出4k，1080，720，540选择点击事件
- (void)OResolutionClick:(UIButton *)btn{
    
    if ([btn.titleLabel.text isEqualToString:@"4K"]) {
        NSSet *set = [NSSet setWithObjects:@"iPhone 4S",@"iPhone 5",@"iPhone 5c",@"iPhone 5s",@"iPhone 6 Plus",@"iPhone 6",@"iPhone 6s",@"iPhone 6s Plus",@"iPhone SE", nil];
        if ([set containsObject:[NvUtils iphoneType]]) {
            [MSSysAlertUtil presentAlertViewWithTitle:@"提示" message:@"iphone7以下设备不支持4k生成" confirmTitle:@"OK" handler:NULL];
            return;
        }
    }
    btn.selected = YES;
    self.btnClickBlock(btn, 1);
}

#pragma mark 硬件编码器支持
-(void)switchAction:(NvSwitchView* )sender
{
    
    if (sender.selected) {
        sender.sliderView.backgroundColor = UIColorWithRGBA(84, 89, 95);
        sender.selected = NO;
        [UIView animateWithDuration:0.1 animations:^{
            sender.sliderView.frame = CGRectMake(2, 2, (54 * SCREANSCALE - 4)/2, 21 * SCREANSCALE - 4);
        }];
    }else {
        sender.sliderView.backgroundColor = UIColorWithRGBA(61, 138, 233);
        sender.selected = YES;
        [UIView animateWithDuration:0.1 animations:^{
            sender.sliderView.frame = CGRectMake(2 + (54 * SCREANSCALE - 4)/2, 2,(54 * SCREANSCALE - 4)/2, 21 * SCREANSCALE - 4);
        }];
    }
    
    NSNumber *choose = sender.selected?@1:@0;
    if (sender.tag == 1010) {
//        [[NSUserDefaults standardUserDefaults] setValue:choose forKey:@"NvUseHardwareCodec"];
    }else if(sender.tag == 1011){
        [[NSUserDefaults standardUserDefaults] setValue:choose forKey:@"NvBackgroudBlurFilled"];
    }
    
}

#pragma mark 添加子视图
- (void)addSubViews{
    UILabel *shootingLabel = [UILabel new];
    shootingLabel.text = @"拍摄设置";
    shootingLabel.textColor = UIColorFromRGB(0xffffff);
    shootingLabel.font = [UIFont systemFontOfSize:12 * SCREANSCALE];
    
    UIView *shootingView = [UIView new];
    shootingView.backgroundColor = UIColorWithRGBA(54, 55, 56);
    
    UILabel *SResolutionLabel = [UILabel new];
    SResolutionLabel.text = @"拍摄分辨率";
    SResolutionLabel.textColor = UIColorFromRGB(0xffffff);
    SResolutionLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    
    UIButton *SResolution1080Btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [SResolution1080Btn setImage:[NvUtils imageWithName:@"Oval 2 Copy"] forState:UIControlStateNormal];
    [SResolution1080Btn setImage:[NvUtils imageWithName:@"NvSelect"] forState:UIControlStateSelected];
    SResolution1080Btn.titleEdgeInsets = UIEdgeInsetsMake(0, 6 * SCREANSCALE, 0, 0);
    [SResolution1080Btn setTitle:@"1080" forState:UIControlStateNormal];
    SResolution1080Btn.titleLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    [SResolution1080Btn addTarget:self action:@selector(SResolutionClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *SResolution720Btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [SResolution720Btn setImage:[NvUtils imageWithName:@"Oval 2 Copy"] forState:UIControlStateNormal];
    [SResolution720Btn setImage:[NvUtils imageWithName:@"NvSelect"] forState:UIControlStateSelected];
    SResolution720Btn.titleEdgeInsets = UIEdgeInsetsMake(0, 6 * SCREANSCALE, 0, 0);
    [SResolution720Btn setTitle:@"720" forState:UIControlStateNormal];
    SResolution720Btn.titleLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    [SResolution720Btn addTarget:self action:@selector(SResolutionClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:shootingLabel];
    [self.view addSubview:shootingView];
    [shootingView addSubview:SResolutionLabel];
    [shootingView addSubview:SResolution1080Btn];
    [shootingView addSubview:SResolution720Btn];
    
    CGFloat left = 12 * SCREANSCALE;
    CGFloat top = 13 * SCREANSCALE;
    CGFloat space = 25 * SCREANSCALE;
    if (SCREEN_WDITH == 320) {
        space = 10 * SCREANSCALE;
    }
    
    [shootingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(20 * SCREANSCALE);
        make.left.equalTo(self.view.mas_left).offset(left * SCREANSCALE);
    }];
    
    [shootingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(shootingLabel.mas_bottom).offset(5 * SCREANSCALE);
        make.width.offset(SCREEN_WDITH);
        make.height.offset(45 * SCREANSCALE);
    }];
    
    [SResolutionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(left * SCREANSCALE);
        make.centerY.equalTo(shootingView.mas_centerY);
    }];
    
    [SResolution1080Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SResolutionLabel.mas_right).offset(space);
        make.centerY.equalTo(shootingView.mas_centerY);
        make.width.offset(70 * SCREANSCALE);
    }];
    
    [SResolution720Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SResolution1080Btn.mas_right).offset(14 * SCREANSCALE);
        make.centerY.equalTo(shootingView.mas_centerY);
        make.width.offset(70 * SCREANSCALE);
    }];
    
    UILabel *outputLabel = [UILabel new];
    outputLabel.text = @"输出设置";
    outputLabel.textColor = UIColorFromRGB(0xffffff);
    outputLabel.font = [UIFont systemFontOfSize:12 * SCREANSCALE];
    
    UIView *outputView = [UIView new];
    outputView.backgroundColor = UIColorWithRGBA(54, 55, 56);
    
    UILabel *OResolutionLabel = [UILabel new];
    OResolutionLabel.text = @"输出分辨率";
    OResolutionLabel.textColor = UIColorFromRGB(0xffffff);
    OResolutionLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    
    UIButton *OResolution4KBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [OResolution4KBtn setImage:[NvUtils imageWithName:@"Oval 2 Copy"] forState:UIControlStateNormal];
    [OResolution4KBtn setImage:[NvUtils imageWithName:@"NvSelect"] forState:UIControlStateSelected];
    OResolution4KBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 6 * SCREANSCALE, 0, 0);
    [OResolution4KBtn setTitle:@"4K" forState:UIControlStateNormal];
    OResolution4KBtn.titleLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    [OResolution4KBtn addTarget:self action:@selector(OResolutionClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *OResolution1080PBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [OResolution1080PBtn setImage:[NvUtils imageWithName:@"Oval 2 Copy"] forState:UIControlStateNormal];
    [OResolution1080PBtn setImage:[NvUtils imageWithName:@"NvSelect"] forState:UIControlStateSelected];
    OResolution1080PBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 6 * SCREANSCALE, 0, 0);
    [OResolution1080PBtn setTitle:@"1080" forState:UIControlStateNormal];
    OResolution1080PBtn.titleLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    [OResolution1080PBtn addTarget:self action:@selector(OResolutionClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *OResolution720Btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [OResolution720Btn setImage:[NvUtils imageWithName:@"Oval 2 Copy"] forState:UIControlStateNormal];
    [OResolution720Btn setImage:[NvUtils imageWithName:@"NvSelect"] forState:UIControlStateSelected];
    OResolution720Btn.titleEdgeInsets = UIEdgeInsetsMake(0, 6 * SCREANSCALE, 0, 0);
    [OResolution720Btn setTitle:@"720" forState:UIControlStateNormal];
    OResolution720Btn.titleLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    [OResolution720Btn addTarget:self action:@selector(OResolutionClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *OResolution540Btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [OResolution540Btn setImage:[NvUtils imageWithName:@"Oval 2 Copy"] forState:UIControlStateNormal];
    [OResolution540Btn setImage:[NvUtils imageWithName:@"NvSelect"] forState:UIControlStateSelected];
    OResolution540Btn.titleEdgeInsets = UIEdgeInsetsMake(0, 6 * SCREANSCALE, 0, 0);
    [OResolution540Btn setTitle:@"540" forState:UIControlStateNormal];
    OResolution540Btn.titleLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    [OResolution540Btn addTarget:self action:@selector(OResolutionClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *line = [UILabel new];
    line.backgroundColor = UIColor.whiteColor;
    
    UILabel *bitLabel = [UILabel new];
    bitLabel.text = @"码率";
    bitLabel.textColor = UIColorFromRGB(0xffffff);
    bitLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    
    self.bitField = [UITextField new];
    self.bitField.delegate = self;
    NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"  建议6Mbps，最大500Mbps" attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0xA8A2A2),NSFontAttributeName:[UIFont systemFontOfSize:12 * SCREANSCALE]}];
    self.bitField.attributedPlaceholder = string;
    self.bitField.backgroundColor = UIColorFromRGB(0xD8D8D8);
    self.bitField.textColor = UIColor.blackColor;
    self.bitField.font = [UIFont systemFontOfSize:12 * SCREANSCALE];
    self.bitField.keyboardType = UIKeyboardTypeNumberPad;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidChangeValue:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:_bitField];
    UILabel *Mlabel = [UILabel new];
    Mlabel.text = @"Mbps";
    Mlabel.textColor = UIColorFromRGB(0xffffff);
    Mlabel.font = [UIFont systemFontOfSize:12 * SCREANSCALE];
    
    UILabel *line1 = [UILabel new];
    line1.backgroundColor = UIColor.whiteColor;
    line1.hidden = YES;
    
    UILabel *hardwareLabel = [UILabel new];
    hardwareLabel.hidden = YES;
    hardwareLabel.text = @"硬件编码器支持";
    hardwareLabel.textColor = UIColorFromRGB(0xffffff);
    hardwareLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    
    self.hardwareSwitch = [NvSwitchView buttonWithType:UIButtonTypeCustom];
    self.hardwareSwitch.hidden = YES;
    self.hardwareSwitch.tag = 1010;
    [self.hardwareSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:outputLabel];
    [self.view addSubview:outputView];
    [outputView addSubview:OResolutionLabel];
    [outputView addSubview:OResolution4KBtn];
    [outputView addSubview:OResolution1080PBtn];
    [outputView addSubview:OResolution720Btn];
    [outputView addSubview:OResolution540Btn];
    [outputView addSubview:line];
    [outputView addSubview:bitLabel];
    [outputView addSubview:_bitField];
    [outputView addSubview:Mlabel];
    [outputView addSubview:line1];
    [outputView addSubview:hardwareLabel];
    [outputView addSubview:_hardwareSwitch];
    
    [outputLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(shootingView.mas_bottom).offset(20 * SCREANSCALE);
        make.left.equalTo(self.view.mas_left).offset(left * SCREANSCALE);
    }];
    
    [outputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(outputLabel.mas_bottom).offset(5 * SCREANSCALE);
        make.width.offset(SCREEN_WDITH);
        make.height.offset(90 * SCREANSCALE);
    }];
    
    [OResolutionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(left * SCREANSCALE);
        make.top.equalTo(outputView.mas_top).offset(top * SCREANSCALE);
    }];
    
    [OResolution4KBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(OResolutionLabel.mas_right).offset(space);
        make.centerY.equalTo(OResolutionLabel.mas_centerY);
        make.width.offset(45 * SCREANSCALE);
    }];
    
    [OResolution1080PBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(OResolution4KBtn.mas_right).offset(13 * SCREANSCALE);
        make.centerY.equalTo(OResolutionLabel.mas_centerY);
        make.width.offset(70 * SCREANSCALE);
    }];
    
    [OResolution720Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(OResolution1080PBtn.mas_right).offset(13 * SCREANSCALE);
        make.centerY.equalTo(OResolutionLabel.mas_centerY);
        if (SCREEN_WDITH == 320) {
            make.width.offset(55 * SCREANSCALE);
        }else{
            make.width.offset(50 * SCREANSCALE);
        }
        
    }];
    
    [OResolution540Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(OResolution720Btn.mas_right).offset(13 * SCREANSCALE);
        make.centerY.equalTo(OResolutionLabel.mas_centerY);
        if (SCREEN_WDITH == 320) {
            make.width.offset(55 * SCREANSCALE);
        }else{
            make.width.offset(50 * SCREANSCALE);
        }
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(OResolutionLabel.mas_bottom).offset(top * SCREANSCALE);
        make.width.offset(SCREEN_WDITH);
        make.height.offset(0.5);
    }];
    
    [bitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_top).offset(top * SCREANSCALE);
        make.leading.equalTo(OResolutionLabel.mas_leading);
    }];
    
    [_bitField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bitLabel.mas_right).offset(70 * SCREANSCALE);
        make.centerY.equalTo(bitLabel.mas_centerY);
        make.width.offset(180 * SCREANSCALE);
        make.height.offset(21 * SCREANSCALE);
    }];
    
    [Mlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->_bitField.mas_right).offset(10 * SCREANSCALE);
        make.centerY.equalTo(self->_bitField.mas_centerY);
    }];
    
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bitLabel.mas_bottom).offset(top * SCREANSCALE);
        make.width.offset(SCREEN_WDITH);
        make.height.offset(0.5);
    }];
    
    [hardwareLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line1.mas_bottom).offset(top * SCREANSCALE);
        make.leading.equalTo(OResolutionLabel.mas_leading);
    }];
    
    [_hardwareSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(outputView.mas_right).offset(-left * SCREANSCALE);
        make.centerY.equalTo(hardwareLabel.mas_centerY);
        make.width.offset(54 * SCREANSCALE);
        make.height.offset(21 * SCREANSCALE);
    }];

    
    UILabel *otherLabel = [UILabel new];
    otherLabel.text = @"其他";
    otherLabel.textColor = UIColorFromRGB(0xffffff);
    otherLabel.font = [UIFont systemFontOfSize:12];
    
    UIView *otherView = [UIView new];
    otherView.backgroundColor = UIColorWithRGBA(54, 55, 56);
    
    UILabel *fillLabel = [UILabel new];
    fillLabel.text = @"背景模糊填充";
    fillLabel.textColor = UIColorFromRGB(0xffffff);
    fillLabel.font = [UIFont systemFontOfSize:15 * SCREANSCALE];
    
    self.fillSwitch = [NvSwitchView buttonWithType:UIButtonTypeCustom];
    self.fillSwitch.tag = 1011;
    [self.fillSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:otherLabel];
    [self.view addSubview:otherView];
    [otherView addSubview:fillLabel];
    [otherView addSubview:_fillSwitch];
    
    [otherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(outputView.mas_bottom).offset(20 * SCREANSCALE);
        make.leading.equalTo(outputLabel.mas_leading);
    }];
    
    [otherView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(otherLabel.mas_bottom).offset(5 * SCREANSCALE);
        make.width.offset(SCREEN_WDITH);
        make.height.offset(45 * SCREANSCALE);
    }];
    
    [fillLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(otherLabel.mas_leading);
        make.centerY.equalTo(otherView.mas_centerY);
    }];
    
    [_fillSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(outputView.mas_right).offset(-left * SCREANSCALE);
        make.centerY.equalTo(fillLabel.mas_centerY);
        make.width.offset(54 * SCREANSCALE);
        make.height.offset(21 * SCREANSCALE);
    }];
    
    __weak typeof(self) weakSelf = self;
    self.btnClickBlock = ^(UIButton *btn, NSInteger type) {
        if (type == 0) {
            NSNumber *num;
            if ([btn.titleLabel.text isEqualToString:@"1080"]) {
                SResolution720Btn.selected = NO;
                num = @1080;
            }else{
                SResolution1080Btn.selected = NO;
                num = @720;
            }
            [[NSUserDefaults standardUserDefaults] setValue:num forKey:@"NvRecordResolution"];
        }else{
            NSNumber *num;
            NSNumber *num1;
            int64_t numt;
            if ([btn.titleLabel.text isEqualToString:@"4K"]) {
                weakSelf.advice = 60;
                OResolution1080PBtn.selected = NO;
                OResolution720Btn.selected = NO;
                OResolution540Btn.selected = NO;
                num = @2160;
                numt = 60 * NV_TIME_BASE;
                num1 = @(numt);
            }else if ([btn.titleLabel.text isEqualToString:@"1080"]){
                OResolution4KBtn.selected = NO;
                OResolution720Btn.selected = NO;
                OResolution540Btn.selected = NO;
                weakSelf.advice = 15;
                num = @1080;
                numt = 15 * NV_TIME_BASE;
                num1 = @(numt);
            }else if ([btn.titleLabel.text isEqualToString:@"720"]){
                OResolution1080PBtn.selected = NO;
                OResolution540Btn.selected = NO;
                OResolution4KBtn.selected = NO;
                weakSelf.advice = 7;
                num = @720;
                numt = 7 * NV_TIME_BASE;
                num1 = @(numt);
            }else{
                OResolution1080PBtn.selected = NO;
                OResolution720Btn.selected = NO;
                OResolution4KBtn.selected = NO;
                weakSelf.advice = 4;
                num = @540;
                numt = 4 * NV_TIME_BASE;
                num1 = @(numt);
            }
            weakSelf.bitField.text = 0;
            [[NSUserDefaults standardUserDefaults] setValue:num1 forKey:@"NvCompileBitrate"];
            [[NSUserDefaults standardUserDefaults] setValue:num forKey:@"NvCompileResolution"];
        }
    };
//    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
//    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    UILabel *version = [UILabel new];
    int large,minor,revision;
    [NvsStreamingContext getSdkVersion:&large minorVersion:&minor revisionNumber:&revision];
    version.text = [NSString stringWithFormat:@"%@ V%d.%d.%d",@"HWSVSDK",large,minor,revision];
    version.textColor = UIColorFromRGB(0xffffff);
    version.font = [UIFont systemFontOfSize:12 * SCREANSCALE ];
    [self.view addSubview:version];
    
    version.userInteractionEnabled = YES;
    [version addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bug:)]];
    
    [version mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-34 * SCREANSCALE);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    
    NSNumber * SResolutionNum = NV_UserInfo(@"NvRecordResolution");
    NSNumber * OResolutionNum = NV_UserInfo(@"NvCompileResolution");
//    NSNumber * hardwareNum = NV_UserInfo(@"NvUseHardwareCodec");
    NSNumber * fillNum = NV_UserInfo(@"NvBackgroudBlurFilled");
    NSNumber * bitNum = NV_UserInfo(@"NvCompileBitrate");
    
    if (SResolutionNum!=nil) {
        if (SResolutionNum.intValue == 1080) {
            SResolution1080Btn.selected = YES;
        }else if (SResolutionNum.intValue == 720){
            SResolution720Btn.selected = YES;
        }
    }
    
    if (OResolutionNum!=nil) {
        if (OResolutionNum.intValue == 2160) {
            OResolution4KBtn.selected = YES;
        }else if (OResolutionNum.intValue == 1080){
            OResolution1080PBtn.selected = YES;
        }else if (OResolutionNum.intValue == 720){
            OResolution720Btn.selected = YES;
        }else if (OResolutionNum.intValue == 540){
            OResolution540Btn.selected = YES;
        }
    }
//
//    if (hardwareNum) {
//        if (hardwareNum.intValue == 1) {
//            _hardwareSwitch.selected = YES;
//            _hardwareSwitch.sliderView.backgroundColor = UIColorWithRGBA(61, 138, 233);
//            _hardwareSwitch.sliderView.frame = CGRectMake(2 + (54 * SCREANSCALE - 4)/2, 2, (54 * SCREANSCALE - 4)/2, 21 * SCREANSCALE - 4);
//        }else{
//            _hardwareSwitch.selected = NO;
//            _hardwareSwitch.sliderView.backgroundColor = UIColorWithRGBA(84, 89, 95);
//            _hardwareSwitch.sliderView.frame = CGRectMake(2, 2, (54 * SCREANSCALE - 4)/2, 21 * SCREANSCALE - 4);
//        }
//    }
    
    if (fillNum!=nil) {
        if (fillNum.intValue == 1) {
            _fillSwitch.selected = YES;
            _fillSwitch.sliderView.backgroundColor = UIColorWithRGBA(61, 138, 233);
             _fillSwitch.sliderView.frame = CGRectMake(2 + (54 * SCREANSCALE - 4)/2, 2, (54 * SCREANSCALE - 4)/2, 21 * SCREANSCALE - 4);
        }else{
            _fillSwitch.selected = NO;
            _fillSwitch.sliderView.backgroundColor = UIColorWithRGBA(84, 89, 95);
             _fillSwitch.sliderView.frame = CGRectMake(2, 2, (54 * SCREANSCALE - 4)/2, 21 * SCREANSCALE - 4);
        }
    }
    
    if (bitNum.longLongValue/NV_TIME_BASE == 15 || bitNum.longLongValue/NV_TIME_BASE == 7 || bitNum.longLongValue/NV_TIME_BASE == 4 || bitNum.longLongValue/NV_TIME_BASE == 60) {
        self.advice = bitNum.longLongValue/NV_TIME_BASE;
    }else{
        self.bitField.text = [NSString stringWithFormat:@"%lld",bitNum.longLongValue/NV_TIME_BASE];
    }
}

- (void)setAdvice:(int64_t)advice{
    _advice = advice;
    NSAttributedString * string = [[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"  建议%lldMbps，最大500Mbps",advice] attributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0xA8A2A2),NSFontAttributeName:[UIFont systemFontOfSize:12 * SCREANSCALE]}];
    self.bitField.attributedPlaceholder = string;
}

- (void)textFieldDidChangeValue:(NSNotification *)notification
{
    UITextField *sender = (UITextField *)[notification object];
    if (sender.text.length == 0) {
        NSNumber *num;
        int64_t numt;
        NSNumber * OResolutionNum = NV_UserInfo(@"NvCompileResolution");
        if (OResolutionNum!=nil) {
            if (OResolutionNum.intValue == 2160) {
                self.advice = 60;
                numt = 60 * NV_TIME_BASE;
                num = @(numt);
            }else if (OResolutionNum.intValue == 1080){
                self.advice = 15;
                numt = 15 * NV_TIME_BASE;
                num = @(numt);
            }else if (OResolutionNum.intValue == 720){
                self.advice = 7;
                numt = 7 * NV_TIME_BASE;
                num = @(numt);
            }else if (OResolutionNum.intValue == 540){
                self.advice = 4;
                numt = 4 * NV_TIME_BASE;
                num = @(numt);
            }
        }
         [[NSUserDefaults standardUserDefaults] setValue:num forKey:@"NvCompileBitrate"];
    }else{
        NSNumber *num;
        double i = sender.text.doubleValue;
        int64_t j;
        if (i >= 500) {
            j = 500 * NV_TIME_BASE;
        }else{
            j = i * NV_TIME_BASE;
        }
        num = @(j);
        [[NSUserDefaults standardUserDefaults] setValue:num forKey:@"NvCompileBitrate"];
    }
}

- (void)bug:(UITapGestureRecognizer *)tap {
    self.bugCount ++;
    int tapCount = 4;
    if (self.bugCount > tapCount) {
        self.bugCount = 0;
        UIStoryboard *st = [UIStoryboard storyboardWithName:@"CrashStoryboard" bundle:[NSBundle bundleForClass:self.class]];
        CrashListViewController *listVC = [st instantiateViewControllerWithIdentifier:@"CrashListViewController"];
        listVC.delegate = self;
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:listVC];
        
        [self presentViewController:nav animated:YES completion:NULL];
    }
}

- (void)backClick {
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#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
