//
//  PUMainViewController.m
//  BJHL-VideoPlayer-UI
//
//  Created by DLM on 2017/4/26.
//  Copyright © 2017年 杨健. All rights reserved.
//

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

#import "PUPlayViewController.h"
#import "BJPViewController.h"
#import "BJVDownloadViewController.h"
#import "PUVideoOptionsViewController.h"
#import "PUPlayOptionsManager.h"

@interface PUMainViewController ()
@property (strong, nonatomic) UISegmentedControl *deploySegment;
@property (strong, nonatomic) UITextField *vidTextField, *tokenTextField;
@property (strong, nonatomic) UIButton *settingButton;
@property (strong, nonatomic) UIButton *avplayButton;
@property (strong, nonatomic) UIButton *ijkplayButton;
@property (strong, nonatomic) UIButton *downloadButton;
@property (strong, nonatomic) UIButton *playbackButton;

@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIView *contentView;

@end

@implementation PUMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    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];
    self.vidTextField.text = @"10980630";
    self.tokenTextField.text = @"MsCwnFJMvKDJcBFd3X6ScnjAzs3HnyxUwIJvmDD9g9gmf64cSgCaCQ";

    //线上videoID
    self.vidTextField.text = @"12057983";
    self.tokenTextField.text = @"IfMH65F6Vs4-eHlkNx1Yl2SDt1EOmiUXLgznNiwRBh4LjSMBaGipUg";
}

#pragma mark - subViews

- (void)setupSubviews {
    self.scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollView];
    [self.scrollView bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.contentView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentView];
    [self.contentView bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.right.equalTo(@[self.view, self.scrollView]);
        make.top.bottom.equalTo(self.scrollView);
    }];
    
#if DEBUG
    [self.contentView addSubview:self.deploySegment];
    [self.deploySegment bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(20.f);
        make.right.equalTo(self.contentView).offset(-20.f);
        make.height.equalTo(@30);
    }];
#endif
    
    [self.contentView bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.view);
    }];
    
    [self.contentView addSubview:self.vidTextField];
    [self.vidTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.height.equalTo(@30);
        make.left.equalTo(self.contentView).offset(30);
        make.right.equalTo(self.contentView).offset(-30);
        make.top.equalTo(self.contentView).offset(70);
    }];
    
    [self.contentView addSubview:self.tokenTextField];
    [self.tokenTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.height.equalTo(@30);
        make.left.right.equalTo(self.vidTextField);
        make.top.equalTo(self.vidTextField.bjl_bottom).offset(30);
    }];
    
    [self.contentView addSubview:self.settingButton];
    [self.settingButton bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(self.tokenTextField.bjl_bottom).offset(30.0);
        make.left.right.equalTo(self.vidTextField);
        make.height.equalTo(@30);
    }];
    
    [self.contentView addSubview:self.avplayButton];
    [self.avplayButton bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(self.settingButton.bjl_bottom).offset(30.0);
        make.left.right.equalTo(self.vidTextField);
        make.height.equalTo(@30);
    }];
    
    [self.contentView addSubview:self.ijkplayButton];
    [self.ijkplayButton bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(self.avplayButton.bjl_bottom).offset(30.0);
        make.left.right.equalTo(self.vidTextField);
        make.height.equalTo(@30);
    }];
    
    [self.contentView addSubview:self.downloadButton];
    [self.downloadButton bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.height.equalTo(@30);
        make.left.right.equalTo(self.vidTextField);
        make.top.equalTo(self.ijkplayButton.bjl_bottom).offset(30);
    }];
    
    [self.contentView addSubview:self.playbackButton];
    [self.playbackButton bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.height.equalTo(@30);
        make.left.right.equalTo(self.vidTextField);
        make.top.equalTo(self.downloadButton.bjl_bottom).offset(30);
        make.bottom.equalTo(self.contentView);
    }];
}

#pragma mark - action

- (void)showAVPlayerView {
    [self showPlayerViewWithType:BJVPlayerType_AVPlayer];
}

- (void)showIJKPlayerView {
    [self showPlayerViewWithType:BJVPlayerType_IJKPlayer];
}

- (void)showPlayerViewWithType:(BJVPlayerType)playerType {
    BJPUVideoOptions *videoOptions = [PUPlayOptionsManager getVideoOptions];
    videoOptions.playerType = playerType;
    [PUPlayOptionsManager setVideoOptions:videoOptions];
    [self entryPlayControl];
}

- (void)entryPlayControl {
    NSString *vid = self.vidTextField.text;
    NSString *token = self.tokenTextField.text;
    PUPlayViewController *playerVC = [[PUPlayViewController alloc] initWithVid:vid token:token];
    [self.navigationController pushViewController:playerVC animated:YES];
}

- (void)downloadAction {
    //BJDownload
//    PUDownloadViewController *downloadVC = [PUDownloadViewController new];
//    [self.navigationController pushViewController:downloadVC animated:YES];
    
    //PMDownload
    BJVDownloadViewController *vc = [[BJVDownloadViewController alloc] init];
    vc.navigationItem.title = @"download 2.0";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)showSettingView {
    PUVideoOptionsViewController *optionsVC = [[PUVideoOptionsViewController alloc] init];
    [self bjl_presentFullScreenViewController:optionsVC animated:YES completion:nil];
}

- (void)playbackAction {
    [self.navigationController pushViewController:[BJPViewController new] animated:YES];
}

#pragma MARK - 切换环境
- (void)alertDeplayTypeChange:(BJVDeployType)type {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"切换环境" message:@"必须重新启动app设置才生效" preferredStyle:UIAlertControllerStyleAlert];
    [alertController bjl_addActionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[NSUserDefaults standardUserDefaults] setInteger:type forKey:@"developType"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        exit(0);
    }];
    [self presentViewController:alertController
                       animated:YES
                     completion:nil];
}

+ (void)loadDeployType {
    BJVDeployType deployType = (BJVDeployType)[[NSUserDefaults standardUserDefaults] integerForKey:@"developType"];
    [BJVAppConfig sharedInstance].deployType = MIN(MAX(BJVDeployType_www, deployType), BJVDeployType_test);
}

#pragma mark - set get

- (UISegmentedControl *)deploySegment {
    if (!_deploySegment) {
        _deploySegment = [[UISegmentedControl alloc] initWithItems:@[@"www", @"beta", @"test"]];
        _deploySegment.selectedSegmentIndex = [BJVAppConfig sharedInstance].deployType;
        
        bjl_weakify(self);
        [_deploySegment bjl_addHandler:^(__kindof UISegmentedControl * _Nullable segment, UIControlEvents event) {
            bjl_strongify(self);
            [self alertDeplayTypeChange:(BJVDeployType)segment.selectedSegmentIndex];
        } forControlEvents:UIControlEventValueChanged];
    }
    return _deploySegment;
}

- (UITextField *)vidTextField {
    if(!_vidTextField) {
        _vidTextField = [self textFieldWithPlaceholder:@"视频 ID"];
    }
    return _vidTextField;
}

- (UITextField *)tokenTextField {
    if(!_tokenTextField) {
        _tokenTextField = [self textFieldWithPlaceholder:@"token"];
    }
    return _tokenTextField;
}

- (UIButton *)settingButton {
    if (!_settingButton) {
        _settingButton = [self buttonWithTitle:@"播放设置"];
        [_settingButton addTarget:self action:@selector(showSettingView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _settingButton;
}

- (UIButton *)avplayButton {
    if (!_avplayButton) {
        _avplayButton = [self buttonWithTitle:@"使用 AVPlayer 播放"];
        [_avplayButton addTarget:self action:@selector(showAVPlayerView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _avplayButton;
}

- (UIButton *)ijkplayButton {
    if (!_ijkplayButton) {
        _ijkplayButton = [self buttonWithTitle:@"使用 IJKPlayer 播放"];
        [_ijkplayButton addTarget:self action:@selector(showIJKPlayerView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _ijkplayButton;
}

- (UIButton *)downloadButton {
    if (!_downloadButton) {
        _downloadButton = [self buttonWithTitle:@"下载"];
        [_downloadButton addTarget:self action:@selector(downloadAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _downloadButton;
}

- (UIButton *)playbackButton {
    if (!_playbackButton) {
        _playbackButton = [self buttonWithTitle:@"回放视频播放"];
        [_playbackButton addTarget:self action:@selector(playbackAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playbackButton;
}

#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.f;
    label.layer.borderWidth = 0.5;
    return label;
}

- (UIButton *)buttonWithTitle:(NSString *)title {
    UIButton *button = [[UIButton alloc] init];
    button.backgroundColor = [UIColor redColor];
    button.layer.cornerRadius = 5.f;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
    return button;
}

@end
