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


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

#import "PUPlayViewController.h"
#import "PUAppDelegate.h"
#import "PUPlayOptionsManager.h"

@interface PUPlayViewController ()

@property (strong, nonatomic) NSString *vid;
@property (strong, nonatomic) NSString *token;

@property (strong, nonatomic) BJPUViewController *playerUIVC;

@end

@implementation PUPlayViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithVid:(NSString *)vid token:(NSString *)token {
    self = [super init];
    if (self) {
        _vid = vid;
        _token = token;
    }
    return self;
}

- (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
    
    // subview
    [self setupSubView];
    
    // play
    [self.playerUIVC playWithVid:_vid token:_token];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.view setNeedsUpdateConstraints];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
        [self.view setNeedsUpdateConstraints];
    } completion:nil];
}

#pragma mark - subViews

- (void)setupSubView {
    // playerView
    self.playerUIVC = [[BJPUViewController alloc] initWithVideoOptions:[PUPlayOptionsManager getVideoOptions]];
    [self addChildViewController:self.playerUIVC];
    [self.playerUIVC didMoveToParentViewController:self];
    __weak typeof(self) weakSelf = self;
    // 退出回调
    [self.playerUIVC setCancelCallback:^{
        __strong __typeof__(weakSelf) strongSelf = weakSelf;
        [strongSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    // 锁屏回调
    [self.playerUIVC setScreenLockCallback:^(BOOL locked) {
        PUAppDelegate *appDelegate = (PUAppDelegate *)[UIApplication sharedApplication].delegate;
        appDelegate.allowRotation = !locked;
    }];
    [self.view addSubview:self.playerUIVC.view];
}

#pragma mark - constraints

- (void)updateViewConstraints {
    CGSize size = self.view.bounds.size;
    BOOL isHorizontal = size.width > size.height;
    [self updateConstraintsForHorizontal:isHorizontal];
    [super updateViewConstraints];
}

- (void)updateConstraintsForHorizontal:(BOOL)isHorizontal {
    if (isHorizontal) {
        [self.playerUIVC.view bjl_remakeConstraints:^(BJLConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    else {
        [self.playerUIVC.view bjl_remakeConstraints:^(BJLConstraintMaker *make) {
            if (@available(iOS 11, *)) {
                make.top.equalTo(self.view.bjl_safeAreaLayoutGuide ?: self.view);
            }
            else {
                make.top.equalTo(self.view).offset(20.0); // status bar
            }
            make.left.right.equalTo(self.view.bjl_safeAreaLayoutGuide ?: self.view);
            make.height.equalTo(self.playerUIVC.view.bjl_width).multipliedBy(9.0/16.0);
        }];
    }
}

@end
