//
//  WXLlcRoomViewController.m
//  ZYWXOnlineSchool
//
//  Created by mac on 2019/8/26.
//  Copyright © 2019年 JackMac. All rights reserved.
//

#import "Orientation.h"
#import "WXLlcRoomViewController.h"

@interface WXLlcRoomViewController () <BJLRoomVCDelegate>

@end

@implementation WXLlcRoomViewController


- (void)dealloc {
    [Orientation setOrientation:(UIInterfaceOrientationMaskPortrait)];
    if (self.exitBackBlock) {
        self.exitBackBlock();
    }
    NSLog(@"%@+++++++++++++++++++++++dealloc", @"WXLlcRoomViewController");
}

/**
 通过参加码创建教室，如果sdk没有集成type对应的uisdk，返回nil
 #param type  教室类型
 #param roomCode 参加码对象
 #return 教室
 */
+ (__kindof instancetype)instanceWithRoomType:(BJLRoomVCType)type roomCode:(BJLRoomCode *)roomCode {
    [Orientation setOrientation:(UIInterfaceOrientationMaskLandscape)];
    return [super instanceWithRoomType:type roomCode:roomCode];
}

/**
 通过roomID创建教室，如果sdk没有集成type对应的uisdk，返回nil
 #param type  教室类型
 #param roomID roomID对象
 #return 教室
 */
+ (__kindof instancetype)instanceWithRoomType:(BJLRoomVCType)type roomID:(BJLRoomID *)roomID {
    [Orientation setOrientation:(UIInterfaceOrientationMaskLandscape)];
    return [super instanceWithRoomType:type roomID:roomID];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

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

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

- (void)exitWithCompletion:(void (^)(void))completion {
    [super exitWithCompletion:completion];
}


/** 进入教室 - 成功/失败 */
- (void)roomViewControllerEnterRoomSuccess:(BJLRoomViewController *)roomViewController {
    NSLog(@"roomViewControllerEnterRoomSuccess");
}
/**
 退出教室 - 正常/异常
 正常退出 `error` 为 `nil`，否则为异常退出
 参考 `BJLErrorCode` */
- (void)roomViewController:(BJLRoomViewController *)roomViewController willExitWithError:(nullable BJLError *)error {
    [self bjl_stopAllKeyValueObserving];
    if (@available(iOS 16.0, *)) {
#if defined(__IPHONE_16_0)
        [Orientation bjy_lockToPortrait];
#endif
    }
    [self dismissViewControllerAnimated:NO
                             completion:^{
                                 if (@available(iOS 16.0, *)) {
                                 }
                                 else {
                                     [Orientation bjy_lockToPortrait];
                                 }
                             }];
}

- (void)roomViewController:(BJLRoomViewController *)roomViewController didExitWithError:(nullable BJLError *)error {

}

- (nullable UIViewController *)roomViewControllerToShare:(BJLRoomViewController *)roomViewController {
    return [self currentViewController];
}

//获取Window当前显示的ViewController
- (UIViewController *)currentViewController {
    //获得当前活动窗口的根视图
    UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1) {
        //根据不同的页面切换方式，逐步取得最上层的viewController
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController *)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController *)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }
        else {
            break;
        }
    }
    return vc;
}

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

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

// 支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

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

@end
