//
//  MKRViewController.m
//  MKRRadioManager
//
//  Created by yyqxiaoyin on 04/15/2019.
//  Copyright (c) 2019 yyqxiaoyin. All rights reserved.
//

#import "MKRViewController.h"
#import "MKRPlayControlViewController.h"
#import <MKRRadioManager/MKRAVPlayer.h>
#import <YYCategory/NSString+Utilities.h>
#import <NSHashTable+MKRRadioManagerAdd.h>
#import "MKRTestObject.h"

@interface MKRViewController ()

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *dataSource;

@property (nonatomic, strong) MKRAVPlayer *player;

@property (nonatomic, strong) NSHashTable *hashTable;

@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation MKRViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self.hashTable makeObjectsPerformSelectorWithObjectsOnMainThread:@selector(testMethod:),@"hello"];
    });
}

- (IBAction)refresh:(UIBarButtonItem *)sender {
    [self.dataSource removeAllObjects];
    [self searchDevice];
}

- (void)searchDevice{
    [_MKRWIFIDeviceManager searchDeviceWithSearchTime:20 timeBlock:^(NSInteger time) {
        [self.tableView reloadData];
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    for (NSInteger i = 0; i<10; i++) {
        MKRTestObject *obj = [[MKRTestObject alloc] init];
        [self.array addObject:obj];
        [self.hashTable addObject:obj];
    }
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    MKRUPnPDevice *device = self.dataSource[indexPath.row];
    cell.textLabel.text = device.deviceName;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MKRPlayControlViewController *vc = [[MKRPlayControlViewController alloc] initWithDevice:self.dataSource[indexPath.row]];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)testMethod{
    MKRLog(@"测试方法");
    
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSHashTable *)hashTable{
    if (!_hashTable) {
        _hashTable = [NSHashTable weakObjectsHashTable];
    }
    return _hashTable;
}

- (NSMutableArray *)array{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

@end
