//
//  ProBjyRNBJSellViewController.m
//  BjyProWxApp
//
//  Created by JackMac on 2021/4/20.
//  Copyright © 2021 JackMac. All rights reserved.
//

#import "Orientation.h"
#import "ProRNBJSellViewController.h"
#import "bjyHelper.h"

@interface ProRNBJSellViewController () <BJLSellViewControllerDelegate>

@property (nonatomic, strong) BJLSellListViewController *sellListViewController;

@end

@implementation ProRNBJSellViewController

- (void)dealloc {
    [Orientation setOrientation:(UIInterfaceOrientationMaskPortrait)];
    if (self.exitBackBlock) {
        self.exitBackBlock();
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

/// 通过参加码创建教室
/// @param roomSecret  教室参加码，初始化时的属性未标记可为空的都需要有值，且字符值长度不能为0
/// @param userName       用户名
/// @param userAvatar  用户头像 URL, 可以为空
+ (__kindof instancetype)instanceWithSecret:(NSString *)roomSecret
                                   userName:(NSString *)userName
                                 userAvatar:(nullable NSString *)userAvatar {
    [Orientation setOrientation:(UIInterfaceOrientationMaskAllButUpsideDown)];
    return [super instanceWithSecret:roomSecret userName:userName userAvatar:userAvatar];
}

/// 通过 ID 创建教室
/// @param roomID 房间id
/// @param apiSign api sign
/// @param user 用户
+ (__kindof instancetype)instanceWithID:(NSString *)roomID
                                apiSign:(NSString *)apiSign
                                   user:(BJLUser *)user {
    [Orientation setOrientation:(UIInterfaceOrientationMaskAllButUpsideDown)];
    return [super instanceWithID:roomID apiSign:apiSign user:user];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    bjl_weakify(self);
    self.shareActionCallback = ^(BJLSShareItem *_Nonnull item) {
        bjl_strongify(self);
        [self getShareContentWithIndex:item.itemId];
    };
}

- (void)getShareContentWithIndex:(int)index {
    ///!!!:标示处代码需要注意
    bjl_weakify(self);
    [self getShareContentWithShareId:self.course_id
                     completionBlock:^(NSDictionary *shareInfo) {
                         bjl_strongify(self);
                         [self configMessageInfoWithCourseInfo:shareInfo index:index];
                     }];
}

- (void)getShareContentWithShareId:(NSString *)shareId completionBlock:(void (^__nullable)(NSDictionary *shareInfo))completionHandler {
}

- (void)configMessageInfoWithCourseInfo:(NSDictionary *)info index:(int)index {
}

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

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

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

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}


#pragma mark - BJLSellViewControllerDelegate
/// 点击购物车按钮, 展示商品列表
/// @param sellViewController sellViewController
/// @param superview 商品列表的父view
/// @param closeCallback 关闭商品列表vc的回调
- (void)roomViewController:(BJLSellViewController *)sellViewController openListFromView:(UIView *)superview closeCallback:(BJLSCloseCallback)closeCallback {
    if (!self.sellListViewController) {
        self.sellListViewController = [[BJLSellListViewController alloc] initWithRoom:sellViewController.room];
        self.sellListViewController.showBuyDetailViewCallback = ^(BJLSellItem *_Nonnull item) {
            NSURL *jumpURL = [NSURL URLWithString:item.buyURL];
            if (@available(iOS 10.0, *)) {
                NSDictionary *options = @{UIApplicationOpenURLOptionUniversalLinksOnly: @NO};
                [[UIApplication sharedApplication] openURL:jumpURL options:options completionHandler:nil];
            }
            else {
                [[UIApplication sharedApplication] openURL:jumpURL];
            }
        };
    }
    [sellViewController bjl_addChildViewController:self.sellListViewController
                                         superview:superview];
    [self.sellListViewController.view bjl_makeConstraints:^(BJLConstraintMaker *_Nonnull make) {
        make.left.right.bottom.equalTo(superview);
        make.height.equalTo(superview).multipliedBy(0.5);
    }];
    NSLog(@"[%@ %@ %@]", NSStringFromSelector(_cmd), sellViewController, superview);
}

/// 点击购物车中的商品，或者正在讲解的商品
/// @param sellViewController sellViewController
/// @param item 点击的商品的信息
- (void)roomViewController:(BJLSellViewController *)sellViewController openSellItem:(BJLSellItem *)item {
}

/** 进入教室 - 成功 */
- (void)roomViewControllerEnterRoomSuccess:(BJLSellViewController *)roomViewController {
    NSLog(@"[%@ %@]", NSStringFromSelector(_cmd), roomViewController);
}
/** 进入教室 - 失败 */
- (void)roomViewController:(BJLSellViewController *)roomViewController
    enterRoomFailureWithError:(BJLError *)error {
    NSLog(@"[%@ %@, %@]", NSStringFromSelector(_cmd), roomViewController, error);
}
/**
即将退出教室 - 正常/异常
正常退出 `error` 为 `nil`，否则为异常退出
参考 `BJLErrorCode` */
- (void)roomViewController:(BJLSellViewController *)roomViewController
         willExitWithError:(nullable BJLError *)error {
    NSLog(@"[%@ %@, %@]", NSStringFromSelector(_cmd), roomViewController, error);
}

/**
 退出教室 - 正常/异常
 正常退出 `error` 为 `nil`，否则为异常退出
 参考 `BJLErrorCode` */
- (void)roomViewController:(BJLSellViewController *)roomViewController
          didExitWithError:(nullable BJLError *)error {
    NSLog(@"[%@ %@, %@]", NSStringFromSelector(_cmd), roomViewController, error);
    [self bjl_stopAllKeyValueObserving];
    self.delegate = nil;
    [Orientation bjy_lockToPortrait];
}

@end
