//
//  ERTCVideoMembersViewController.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/5/18.
//

#import "ERTCVideoMembersViewController.h"
#import "ERTCVideoMeetingManager.h"
#import "ERTCPlayerViewManager.h"

@interface ERTCVideoMembersViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *muteAllRemoteVideoStreamsButton;

@property (nonatomic ,strong) NSArray *membersArray;

@end

@implementation ERTCVideoMembersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"与会成员";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    self.tableView.backgroundView = nil;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0];
    self.membersArray = [[ERTCVideoMeetingManager sharedInstance] getRemoteUserList];
    [self.muteAllRemoteVideoStreamsButton setTitle:@"接收所有人视频流" forState:UIControlStateSelected];
//    NSArray *subscribeArray = [[ERTCVideoMeetingManager sharedInstance] getSubscribeList];
//    if (subscribeArray.count == 1) {
//        self.muteAllRemoteVideoStreamsButton.selected = YES;
//    } else if (subscribeArray.count == 0) {
//        self.muteAllRemoteVideoStreamsButton.hidden = YES;
//    }
}

- (void)viewWillAppear:(BOOL)animated {
    //禁用侧滑手势方法
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

-(void)viewWillDisappear:(BOOL)animated {
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

- (IBAction)clickButton:(id)sender {
//    self.muteAllRemoteVideoStreamsButton.selected = !self.muteAllRemoteVideoStreamsButton.selected;
//    if (self.muteAllRemoteVideoStreamsButton.selected) {
//        [[ERTCPlayerViewManager sharedInstance].engine muteAllRemoteVideoStreams:YES];
//        for (ERTCUserInfo *user in self.membersArray) {
//            [[ERTCVideoMeetingManager sharedInstance] unregisterUser:user.uid];
//            if (self.cancelSubscribeBlock != nil) {
//                self.cancelSubscribeBlock(user.uid);
//            }
//        }
//    } else {
//        [[ERTCPlayerViewManager sharedInstance].engine muteAllRemoteVideoStreams:NO];
//        for (ERTCUserInfo *user in self.membersArray) {
//            [[ERTCVideoMeetingManager sharedInstance] registerUser:user.uid];
//            if (self.subscribeBlock != nil) {
//                self.subscribeBlock();
//            }
//        }
//    }
//    [self.tableView reloadData];
//    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.membersArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    ERTCUserInfo *info = self.membersArray[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"[UID:%@] [视频:%@] [音频:%@]",info.uid,info.isVideoAvailable?@"开":@"关",info.isAudioAvailable?@"开":@"关"];
    NSArray *subscribeArray = self.membersArray;
    if ([subscribeArray containsObject:info]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    //订阅或者取消订阅
    ERTCUserInfo *info = self.membersArray[indexPath.row];
    
    if (info.isVideoAvailable == NO) {
        [self showAlert:@"该参与者未开启视频" withAction:nil];
        return;
    }
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        //已订阅
        cell.accessoryType = UITableViewCellAccessoryNone;
//        [[ERTCVideoMeetingManager sharedInstance] unregisterUser:info.uid];
        if (self.cancelSubscribeBlock != nil) {
            self.cancelSubscribeBlock(info.uid);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        
//        [[ERTCVideoMeetingManager sharedInstance] registerUser:info.uid];
        if (self.subscribeBlock != nil) {
            self.subscribeBlock();
        }
        
    }
}

- (void)showAlert:(NSString *)alertStr withAction:(dispatch_block_t)block
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertStr
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的"
                                                       style:UIAlertActionStyleDestructive
                                                     handler:^(UIAlertAction * _Nonnull action) {
        if (block) {
            block();
        }
    }];
    [alertController addAction:okAction];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:alertController animated:YES completion:nil];
    });

}


@end
