//
//  BJPViewController.m
//  BJPlaybackUI
//
//  Created by oushizishu on 08/22/2017.
//  Copyright (c) 2017 oushizishu. All rights reserved.
//

#import <BJPlaybackUI/BJPlaybackUI.h>
#import <BJVideoPlayerUI/BJVideoPlayerUI.h>

#import "BJPViewController.h"
#import "PUPlayOptionsManager.h"

@interface BJPViewController ()<UIActionSheetDelegate, UIAlertViewDelegate>

@property (nonatomic) UITextField *classIDTextField, *tokenTextField, *sessionIDTextField,
                                  *userInfoTextField, *userNumaberTextField;
@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic, readwrite) UIView *contentView;

@end

@implementation BJPViewController

- (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 setupViews];
}

- (void)setupViews {
    [self setupScrollView];
    [self setupClassIDTextField];
    [self setupSessionIDTextField];
    [self setuptokenTextField];
    [self setupUserInfoTextField];
    [self setupUserNumberTextField];
    [self setupButton];
}

- (void)setupScrollView {
    self.contentView = [UIView new];
    self.scrollView = [UIScrollView new];
    self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.frame.size.height+50);
    
    [self.scrollView addSubview:self.contentView];
    [self.view addSubview:self.scrollView];
    
    [self.contentView bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.height.equalTo(self.view).multipliedBy(1.3);
        make.top.left.equalTo(self.scrollView);
    }];
    [self.scrollView bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)setupClassIDTextField {
    NSString *classID = @"17121135334473";
    classID = @"18121856439323";
    self.classIDTextField = [self textFieldwithContent:classID leftLabelText:@"教室id: "];
    [self.contentView addSubview:self.classIDTextField];
    
    [self.classIDTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        if(self.deploySegment) {
            make.top.equalTo(self.deploySegment.bjl_bottom).offset(20.f);
        }
        else {
            make.top.equalTo(self.contentView).offset(50.f);
        }
        make.left.equalTo(self.view).offset(30.f);
        make.right.equalTo(self.view).offset(-30.f);
        make.height.equalTo(@35);
    }];
}

- (void)setupSessionIDTextField {
    NSString *sessionString = @"201712110";
    sessionString = @"201812251";
    self.sessionIDTextField = [self textFieldwithContent:sessionString leftLabelText:@"sessionId:"];
    [self.contentView addSubview:self.sessionIDTextField];
    [self.sessionIDTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(self.classIDTextField.bjl_bottom).offset(20.f);
        make.left.right.height.equalTo(self.classIDTextField);
    }];
}

- (void)setuptokenTextField {
    NSString *token = @"YBNpl56vh_n6GBtNSJkwA6Bm9iXSfd0pI5Z19POKRiip3A5MMO7adA";
    self.tokenTextField = [self textFieldwithContent:token leftLabelText:@"token: "];
    
    [self.contentView addSubview:self.tokenTextField];
    [self.tokenTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(self.sessionIDTextField.bjl_bottom).offset(20.f);
        make.left.right.height.equalTo(self.classIDTextField);
    }];
}

- (void)setupUserInfoTextField {
    NSString *userName = @"李四-正式环境";
    self.userInfoTextField = [self textFieldwithContent:userName leftLabelText:@"userName: "];
    [self.contentView addSubview:self.userInfoTextField];
    
    [self.userInfoTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(self.tokenTextField.bjl_bottom).offset(20.f);
        make.left.right.height.equalTo(self.classIDTextField);
    }];
}

- (void)setupUserNumberTextField {
    NSString *userNo = @"123456";
    self.userNumaberTextField = [self textFieldwithContent:userNo leftLabelText:@"userNumber: "];
    [self.contentView addSubview:self.userNumaberTextField];
    [self.userNumaberTextField bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(self.userInfoTextField.bjl_bottom).offset(20.f);
        make.left.right.height.equalTo(self.classIDTextField);
    }];
}


- (void)setupButton {
    UIButton *avPlayerBtn = [self playButtonWithTitle:@"AVPlayer 播放"];
    [avPlayerBtn addTarget:self action:@selector(enterRoomWithAVPlayer) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:avPlayerBtn];
    [avPlayerBtn bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(self.userNumaberTextField.bjl_bottom).offset(30);
        make.height.equalTo(@35);
        make.width.equalTo(@160);
        make.centerX.equalTo(self.view);
    }];
    
    UIButton *ijkPlayerBtn = [self playButtonWithTitle:@"IJKPlayer 播放"];
    [ijkPlayerBtn addTarget:self action:@selector(enterRoomWithIJKPlayer) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:ijkPlayerBtn];
    [ijkPlayerBtn bjl_makeConstraints:^(BJLConstraintMaker *make) {
        make.top.equalTo(avPlayerBtn.bjl_bottom).offset(30);
        make.height.equalTo(@35);
        make.width.equalTo(@160);
        make.centerX.equalTo(self.view);
    }];
}

- (UIButton *)playButtonWithTitle:(NSString *)title {
    UIButton *button = [[UIButton alloc] init];
    [button setTitle:title forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor blueColor]];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 3.0;
    return button;
}

#pragma mark - action

- (void)enterRoomWithAVPlayer {
    [self enterRoomWithPlayerType:BJVPlayerType_AVPlayer];
}

- (void)enterRoomWithIJKPlayer {
    [self enterRoomWithPlayerType:BJVPlayerType_IJKPlayer];
}

- (void)enterRoomWithPlayerType:(BJVPlayerType)playerType {
    BJPPlaybackOptions *playbackOptions = [PUPlayOptionsManager getPlaybackOptions];
    playbackOptions.playerType = playerType;
    [PUPlayOptionsManager setPlaybackOptions:playbackOptions];
    BJPRoomViewController *vc = [BJPRoomViewController onlinePlaybackRoomWithClassID:self.classIDTextField.text
                                                                           sessionID:self.sessionIDTextField.text
                                                                               token:self.tokenTextField.text
                                                                           accessKey:nil
                                                                             options:[PUPlayOptionsManager getPlaybackOptions]];
    [self bjl_presentFullScreenViewController:vc animated:YES completion:nil];
}

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

#pragma mark - private

- (UITextField *)textFieldwithContent:(NSString *)content leftLabelText:(NSString *)text {
    UITextField *textField = [UITextField new];
    textField.layer.borderWidth = 1.f;
    textField.layer.borderColor = [UIColor grayColor].CGColor;
    textField.text = content;
    textField.leftViewMode = UITextFieldViewModeAlways;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 90, 42)];
    label.text = text;
    textField.leftView = label;
    return textField;
}

@end
