//
//  WXPlayBackRoomViewController.m
//  WXOnlineSchool
//
//  Created by MacOnline on 2019/8/14.
//  Copyright © 2019 apple. All rights reserved.
//

#import "Orientation.h"
#import "WXPlayBackRoomViewController.h"

@interface WXPlayBackRoomViewController ()

@end

@implementation WXPlayBackRoomViewController

- (void)dealloc {
    [self.room bjl_stopAllKeyValueObserving];
    [Orientation setOrientation:(UIInterfaceOrientationMaskPortrait)];
}

- (void)viewDidLoad {
    [Orientation setOrientation:(UIInterfaceOrientationMaskAllButUpsideDown)];
    bjl_weakify(self);
    self.room.playerManager.backgroundAudioEnabled = YES;
    [self.room bjl_kvo:BJLMakeProperty(self.room.playerManager, playStatus)
        filter:^BOOL(NSNumber *_Nullable now, NSNumber *_Nullable old, BJLPropertyChange *_Nullable change) {
            return (old == nil) || (now.integerValue != old.integerValue);
        }
        observer:^BOOL(NSNumber *_Nullable now, NSNumber *_Nullable old, BJLPropertyChange *_Nullable change) {
            bjl_strongify(self);
            BJVPlayerStatus status = self.room.playerManager.playStatus;

            // debug info
            switch (status) {
                case BJVPlayerStatus_reachEnd:
                    NSLog(@"播放状态变化：BJVPlayerStatus_ReachEnd");
                    [self.room.playerManager play];
                    break;
                case BJVPlayerStatus_failed:
                    NSLog(@"播放状态变化：BJVPlayerStatus_Failed");
                    break;

                default:
                    break;
            }

            return YES;
        }];

    [super viewDidLoad];
}


- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
    [coordinator
        animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
            [self setNeedsStatusBarAppearanceUpdate];
            if (@available(iOS 11.0, *)) {
                [self setNeedsUpdateOfHomeIndicatorAutoHidden];
            }
            [self.view setNeedsUpdateConstraints];
        }
                        completion:nil];
}

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

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //[Orientation setOrientation:(UIInterfaceOrientationPortrait)];
}

- (void)exit {
    [Orientation bjy_lockToPortrait];
    [super exit];
    [self.room bjl_stopAllKeyValueObserving];
    if (self.exitBackBlock) {
        self.exitBackBlock();
    }
}

#pragma mark - 屏幕方向控制
//  是否支持自动转屏
- (BOOL)shouldAutorotate {
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden {
    return YES;
}


// 支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (self.room.isInteractiveClass) {
        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }
    else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

// 页面展示的时候默认屏幕方向（当前ViewController必须是通过模态ViewController（模态带导航的无效）方式展现出来的，才会调用这个方法）
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    if (self.room.isInteractiveClass) {
        return UIInterfaceOrientationLandscapeRight;
    }
    else {
        return UIInterfaceOrientationPortrait;
    }
}


@end
