//
//  PUVideoOptionsViewController.m
//  BJPlayerManagerUI_Example
//
//  Created by HuangJie on 2018/4/23.
//  Copyright © 2018年 oushizishu. All rights reserved.
//

#import "PUVideoOptionsViewController.h"
#import <BJVideoPlayerUI/BJVideoPlayerUI.h>
#import <BJLiveBase/BJLiveBase+UIKit.h>
#import <BJPlaybackUI/BJPlaybackUI.h>

#import "PUPlayOptionsManager.h"

static NSInteger initialTag = 1000;

@interface PUVideoOptionsViewController () <UITextFieldDelegate>

@property (strong, nonatomic) UITextField *initialTimeTextField, *privatePrefixTextField, *nameTextField, *numberTextField, *selectDefinitionTextField;
@property (strong, nonatomic) UILabel *adLabel, *sliderLabel, *autoPlayLabel, *encryptLabel, *memoryLabel, *backgroundPlayLabel, *initialTimeLabel, *privatePrefixLabel, *nameLabel, *numberLabel, *definitionLabel, *pptAnimationLabel;
@property (strong, nonatomic) UISwitch *adSwitch, *sliderSwitch, *autoPlaySwitch, *encryptSwitch, *memorySwitch, *backgroundPlaySwitch, *pptAnimationSwitch;
@property (strong, nonatomic) NSArray<NSString *> *definitionKeys;
@property (strong, nonatomic) NSArray<UIButton *> *definitionButtons;
@property (strong, nonatomic) NSMutableArray<NSString *> *preferredDefinitions;
@property (strong, nonatomic) UIButton *confirmButton;

@end

@implementation PUVideoOptionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 130000) // __IPHONE_13_0
    if (@available(iOS 13.0, *)) {
        self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
#endif
    [self setupSubviews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 点播设置
    BJPUVideoOptions *options = [PUPlayOptionsManager getVideoOptions];
    self.adSwitch.on = options.advertisementEnabled;
    self.autoPlaySwitch.on = options.autoplay;
    self.sliderSwitch.on = options.sliderDragEnabled;
    self.memorySwitch.on = options.playTimeRecordEnabled;
    self.encryptSwitch.on = options.encryptEnabled;
    self.backgroundPlaySwitch.on = options.backgroundAudioEnabled;
    self.initialTimeTextField.text = [NSString stringWithFormat:@"%.3f", options.initialPlayTime];
    self.nameTextField.text = options.userName;
    self.numberTextField.text = [NSString stringWithFormat:@"%ti", options.userNumber];
    
    // 回放设置
    BJPPlaybackOptions *playbackOptions = [PUPlayOptionsManager getPlaybackOptions];
    self.pptAnimationSwitch.on = playbackOptions.disablePPTAnimation;
    
    // 专属域名
    self.privatePrefixTextField.text = [PUPlayOptionsManager getPrivatePrefix];
//    self.privatePrefixTextField.text = @"e70993339";
    
    // 偏好清晰度
    self.preferredDefinitions = [options.preferredDefinitionList mutableCopy];
    [self updateDefinitionControls];
}

#pragma mark - subViews

- (void)setupSubviews {
    // 确认按钮
    self.confirmButton = ({
        UIButton *button = [[UIButton alloc] init];
        button.backgroundColor = [UIColor redColor];
        button.layer.cornerRadius = 5.0;
        [button setTitle:@"确认" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        [button addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [self.view addSubview:self.confirmButton];
    [self.confirmButton bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.bottom.equalTo(self.view.bjl_bottom).offset(-30.0);
        make.left.equalTo(self.view).offset(30.0);
        make.right.equalTo(self.view).offset(-30.0);
        make.height.equalTo(@30.0);
    }];
    
    // scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    [scrollView bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.confirmButton.bjl_top).offset(-30.0);
    }];
    
    UIView *contentView = [[UIView alloc] init];
    [scrollView addSubview:contentView];
    [contentView bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.edges.equalTo(scrollView);
    }];
    
    CGSize labelSize = CGSizeMake(180.0, 30.0);
    
    [contentView addSubview:self.adLabel];
    [self.adLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(contentView).offset(20.0);
        make.left.equalTo(contentView).offset(30.0);
//        make.size.equal.sizeOffset(labelSize);
        make.size.equal.sizeOffset(CGSizeZero);
    }];
    
//    [self.view addSubview:self.adSwitch];
//    [self.adSwitch bjl_makeConstraints:^(BJLConstraintMaker *make) {
//        make.left.equalTo(self.adLabel.bjl_right).offset(20.0);
//        make.top.bottom.equalTo(self.adLabel);
//    }];
    
    [contentView addSubview:self.sliderLabel];
    [self.sliderLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.adLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.sliderSwitch];
    [self.sliderSwitch bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.sliderLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.sliderLabel);
    }];
    
    [contentView addSubview:self.autoPlayLabel];
    [self.autoPlayLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.sliderLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.autoPlaySwitch];
    [self.autoPlaySwitch bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.autoPlayLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.autoPlayLabel);
    }];
    
    [contentView addSubview:self.encryptLabel];
    [self.encryptLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.autoPlayLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.encryptSwitch];
    [self.encryptSwitch bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.encryptLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.encryptLabel);
    }];
    
    [contentView addSubview:self.memoryLabel];
    [self.memoryLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.encryptLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.memorySwitch];
    [self.memorySwitch bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.memoryLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.memoryLabel);
    }];
    
    [contentView addSubview:self.backgroundPlayLabel];
    [self.backgroundPlayLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.memoryLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.backgroundPlaySwitch];
    [self.backgroundPlaySwitch bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.backgroundPlayLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.backgroundPlayLabel);
    }];
    
    [contentView addSubview:self.initialTimeLabel];
    [self.initialTimeLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.backgroundPlayLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.initialTimeTextField];
    [self.initialTimeTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.initialTimeLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.initialTimeLabel);
        make.width.equalTo(@(100.0));
    }];
    
    [contentView addSubview:self.privatePrefixLabel];
    [self.privatePrefixLabel bjl_makeConstraints:^(BJLConstraintMaker * _Nonnull make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.initialTimeLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.privatePrefixTextField];
    [self.privatePrefixTextField bjl_makeConstraints:^(BJLConstraintMaker * _Nonnull make) {
        make.left.equalTo(self.privatePrefixLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.privatePrefixLabel);
        make.width.equalTo(@(100.0));
    }];
    
    [contentView addSubview:self.nameLabel];
    [self.nameLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.privatePrefixLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.nameTextField];
    [self.nameTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.nameLabel);
        make.width.equalTo(@(100.0));
    }];
    
    [contentView addSubview:self.numberLabel];
    [self.numberLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.nameLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.numberTextField];
    [self.numberTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.numberLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.numberLabel);
        make.width.equalTo(@(100));
    }];
    
    [contentView addSubview:self.pptAnimationLabel];
    [self.pptAnimationLabel bjl_makeConstraints:^(BJLConstraintMaker * make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.numberLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(labelSize);
    }];
    
    [contentView addSubview:self.pptAnimationSwitch];
    [self.pptAnimationSwitch bjl_makeConstraints:^(BJLConstraintMaker * make) {
        make.left.equalTo(self.pptAnimationLabel.bjl_right).offset(20.0);
        make.top.bottom.equalTo(self.pptAnimationLabel);
    }];
    
    [contentView addSubview:self.definitionLabel];
    [self.definitionLabel bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.adLabel);
        make.top.equalTo(self.pptAnimationLabel.bjl_bottom).offset(30.0);
        make.size.equal.sizeOffset(CGSizeMake(100.0, 30.0));
    }];
    
    [contentView addSubview:self.selectDefinitionTextField];
    [self.selectDefinitionTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.equalTo(self.definitionLabel.bjl_right).offset(5.0);
        make.top.bottom.equalTo(self.definitionLabel);
        make.width.greaterThanOrEqualTo(@(80.0));
    }];
    
    CGFloat leftOffset = 0.0;
    CGSize buttonSize = CGSizeMake(50.0, 25.0);
    for (UIButton *button in self.definitionButtons) {
        [contentView addSubview:button];
        [button bjl_makeConstraints:^(BJLConstraintMaker *make) {
            make.top.equalTo(self.definitionLabel.bjl_bottom).offset(5.0);
            make.left.equalTo(self.definitionLabel).offset(leftOffset);
            make.bottom.equalTo(contentView);
            make.size.equal.sizeOffset(buttonSize);
            if (button == self.definitionButtons.lastObject) {
                make.right.equalTo(contentView).offset(-5.0);
            }
        }];
        leftOffset += buttonSize.width + 5;
    }
}

#pragma mark - action

- (void)confirmAction {
    BJPUVideoOptions *options = [PUPlayOptionsManager getVideoOptions];
    options.advertisementEnabled = self.adSwitch.on;
    options.autoplay = self.autoPlaySwitch.on;
    options.sliderDragEnabled = self.sliderSwitch.on;
    options.playTimeRecordEnabled = self.memorySwitch.on;
    options.encryptEnabled = self.encryptSwitch.on;
    options.backgroundAudioEnabled = self.backgroundPlaySwitch.on;
    options.initialPlayTime = [self.initialTimeTextField.text doubleValue];
    options.userName = self.nameTextField.text;
    options.userNumber = [self.numberTextField.text integerValue];
    // 偏好清晰度
    options.preferredDefinitionList = [NSArray arrayWithArray:self.preferredDefinitions];
    
    // 更新, demo 中回放和点播在这里统一设置
    id jsonObject = [options bjlyy_modelToJSONObject];
    BJPPlaybackOptions *playbackOptions = [BJPPlaybackOptions bjlyy_modelWithJSON:jsonObject];
    playbackOptions.disablePPTAnimation = self.pptAnimationSwitch.on;
    [PUPlayOptionsManager setVideoOptions:options];
    [PUPlayOptionsManager setPlaybackOptions:playbackOptions];
    
    // 专属域名
    NSString *privatePrefix = self.privatePrefixTextField.text.length ? self.privatePrefixTextField.text : nil;
    [PUPlayOptionsManager setPrivatePrefix:privatePrefix];
    [[BJVAppConfig sharedInstance] setPrivateDomainPrefix:privatePrefix];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)selectionButtonOnClick:(UIButton *)button {
    NSInteger index = button.tag - initialTag;
    if (button.selected) {
        // 取消
        if ([self.preferredDefinitions containsObject:[self.definitionKeys bjl_objectAtIndex:index]]) {
            [self.preferredDefinitions removeObject:[self.definitionKeys bjl_objectAtIndex:index]];
        }
        else {
            return;
        }
    }
    else {
        // 选中
        if ([self.preferredDefinitions containsObject:[self.definitionKeys bjl_objectAtIndex:index]]) {
            return;
        }
        else {
            [self.preferredDefinitions addObject:[self.definitionKeys bjl_objectAtIndex:index]];
        }
    }
    [self updateDefinitionControls];
}

#pragma mark - getters

- (UILabel *)adLabel {
    if (!_adLabel) {
        _adLabel = [self labelWithText:@"片头片尾广告:(无效) "];
    }
    return _adLabel;
}

- (UISwitch *)adSwitch {
    if (!_adSwitch) {
        _adSwitch = [[UISwitch alloc] init];
    }
    return _adSwitch;
}

- (UILabel *)sliderLabel {
    if (!_sliderLabel) {
        _sliderLabel = [self labelWithText:@"拖拽进度条: "];
    }
    return _sliderLabel;
}

- (UISwitch *)sliderSwitch {
    if (!_sliderSwitch) {
        _sliderSwitch = [[UISwitch alloc] init];
        _sliderSwitch.on = YES;
    }
    return _sliderSwitch;
}

- (UILabel *)autoPlayLabel {
    if (!_autoPlayLabel) {
        _autoPlayLabel = [self labelWithText:@"自动播放: "];
    }
    return _autoPlayLabel;
}

- (UISwitch *)autoPlaySwitch {
    if (!_autoPlaySwitch) {
        _autoPlaySwitch = [[UISwitch alloc] init];
    }
    return _autoPlaySwitch;
}

- (UILabel *)encryptLabel {
    if (!_encryptLabel) {
        _encryptLabel = [self labelWithText:@"视频加密: "];
    }
    return _encryptLabel;
}

- (UISwitch *)encryptSwitch {
    if (!_encryptSwitch) {
        _encryptSwitch = [[UISwitch alloc] init];
    }
    return _encryptSwitch;
}

- (UILabel *)memoryLabel {
    if (!_memoryLabel) {
        _memoryLabel = [self labelWithText:@"记忆播放: "];
    }
    return _memoryLabel;
}

- (UISwitch *)memorySwitch {
    if (!_memorySwitch) {
        _memorySwitch = [[UISwitch alloc] init];
    }
    return _memorySwitch;
}

- (UILabel *)backgroundPlayLabel {
    if (!_backgroundPlayLabel) {
        _backgroundPlayLabel = [self labelWithText:@"后台播放: "];
    }
    return _backgroundPlayLabel;
}

-  (UISwitch *)backgroundPlaySwitch {
    if (!_backgroundPlaySwitch) {
        _backgroundPlaySwitch = [[UISwitch alloc] init];
    }
    return _backgroundPlaySwitch;
}

- (UILabel *)initialTimeLabel {
    if (!_initialTimeLabel) {
        _initialTimeLabel = [self labelWithText:@"初始播放时间: "];
    }
    return _initialTimeLabel;
}

- (UITextField *)initialTimeTextField {
    if (!_initialTimeTextField) {
        _initialTimeTextField = ({
            UITextField *textField = [self textFieldWithPlaceholder:@" 默认 0 秒"];
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.returnKeyType = UIReturnKeyDone;
            textField.delegate = self;
            textField;
        });
    }
    return _initialTimeTextField;
}

- (UILabel *)privatePrefixLabel {
    if (!_privatePrefixLabel) {
        _privatePrefixLabel = [self labelWithText:@"专属域名"];
    }
    return _privatePrefixLabel;
}

- (UITextField *)privatePrefixTextField {
    if (!_privatePrefixTextField) {
        _privatePrefixTextField = ({
            UITextField *textField = [self textFieldWithPlaceholder:@"影响全局"];
            textField.returnKeyType = UIReturnKeyDone;
            textField.delegate = self;
            textField;
        });
    }
    return _privatePrefixTextField;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [self labelWithText:@"用户名"];
    }
    return _nameLabel;
}

- (UITextField *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = ({
            UITextField *textField = [self textFieldWithPlaceholder:@"上报统计"];
            textField.returnKeyType = UIReturnKeyDone;
            textField.delegate = self;
            textField;
        });
    }
    return _nameTextField;
}

- (UILabel *)numberLabel {
    if (!_numberLabel) {
        _numberLabel = [self labelWithText:@"用户编号"];
    }
    return _numberLabel;
}

- (UITextField *)numberTextField {
    if (!_numberTextField) {
        _numberTextField = ({
            UITextField *textField = [self textFieldWithPlaceholder:@"上报统计"];
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.returnKeyType = UIReturnKeyDone;
            textField.delegate = self;
            textField;
        });
    }
    return _numberTextField;
}

-  (UILabel *)definitionLabel {
    if (!_definitionLabel) {
        _definitionLabel = [self labelWithText:@"偏好清晰度"];
    }
    return _definitionLabel;
}

- (UITextField *)selectDefinitionTextField {
    if (!_selectDefinitionTextField) {
        _selectDefinitionTextField = ({
            UITextField *textField = [self textFieldWithPlaceholder:@" 优先级递减"];
            textField.font = [UIFont systemFontOfSize:14.0];
            textField.enabled = NO;
            textField;
        });
    }
    return _selectDefinitionTextField;
}

- (NSArray<UIButton *> *)definitionButtons {
    if (!_definitionButtons) {
        _definitionButtons = ({
            NSMutableArray *array = [NSMutableArray array];
            for (int i = 0; i < self.definitionKeys.count; i ++) {
                NSString *title = [self definitionTitleWithKey:[self.definitionKeys bjl_objectAtIndex:i]];
                UIButton *definitionButton = [self selectionButtonWithTitle:title];
                definitionButton.tag = initialTag + i;
                [array addObject:definitionButton];
            }
            array;
        });
    }
    return _definitionButtons;
}

- (NSMutableArray<NSString *> *)preferredDefinitions {
    if (!_preferredDefinitions) {
        _preferredDefinitions = [NSMutableArray array];
    }
    return _preferredDefinitions;
}

- (NSArray<NSString *> *)definitionKeys {
    if (!_definitionKeys) {
        _definitionKeys = @[@"low", @"high", @"superHD", @"720p", @"1080p", @"audio"];
    }
    return _definitionKeys;
}

- (UILabel *)pptAnimationLabel {
    if (!_pptAnimationLabel) {
        _pptAnimationLabel = [self labelWithText:@"回放禁用 PPT 动效"];
    }
    return _pptAnimationLabel;
}

- (UISwitch *)pptAnimationSwitch {
    if (!_pptAnimationSwitch) {
        _pptAnimationSwitch = [[UISwitch alloc] init];
    }
    return _pptAnimationSwitch;
}

#pragma mark - <UITextFieldDelegate>

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - private

- (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder {
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = placeholder;
    textField.layer.borderColor = [UIColor grayColor].CGColor;
    textField.layer.borderWidth = 0.5;
    textField.layer.cornerRadius = 5.f;
    return textField;
}

- (UILabel *)labelWithText:(NSString *)text {
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.layer.borderColor = [UIColor blackColor].CGColor;
    label.layer.cornerRadius = 3.0;
    label.layer.borderWidth = 0.5;
    return label;
}

- (UIButton *)selectionButtonWithTitle:(NSString *)title {
    UIButton *button = [[UIButton alloc] init];
    button.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    button.layer.borderWidth = 1.0;
    button.layer.borderColor = [UIColor lightGrayColor].CGColor;
    button.layer.cornerRadius = 3.0;
    [button addTarget:self action:@selector(selectionButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)updateDefinitionControls {
    for (UIButton *button in self.definitionButtons) {
        button.selected = NO;
        button.layer.borderColor = [UIColor grayColor].CGColor;
    }
    
    NSString *definitionString = self.preferredDefinitions.count? @" " : nil;
    for (NSString *definitionKey in self.preferredDefinitions) {
        NSInteger selectedIndex = [self.definitionKeys indexOfObject:definitionKey];
        if (selectedIndex >= 0 && selectedIndex < self.definitionKeys.count) {
            // 清晰度描述
            NSString *title = [self definitionTitleWithKey:definitionKey];
            definitionString = [NSString stringWithFormat:@"%@ %@", definitionString, title];
            
            // 按钮选中状态
            UIButton *button = [self.definitionButtons bjl_objectAtIndex:selectedIndex];
            button.selected = YES;
            button.layer.borderColor = [UIColor blueColor].CGColor;
        }
    }
    self.selectDefinitionTextField.text = definitionString;
}

- (NSString *)definitionTitleWithKey:(NSString *)key {
    if ([key isEqualToString:@"low"]) {
        return @"标清";
    }
    else if ([key isEqualToString:@"high"]) {
        return @"高清";
    }
    else if ([key isEqualToString:@"superHD"]) {
        return @"超清";
    }
    else if ([key isEqualToString:@"720p"]) {
        return @"720p";
    }
    else if ([key isEqualToString:@"1080p"]) {
        return @"1080p";
    }
    else if ([key isEqualToString:@"audio"]) {
        return @"音频";
    }
    return @"未知";
}

@end
