//
//  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>

@interface MKRViewController ()

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *dataSource;

@property (nonatomic, strong) MKRAVPlayer *player;

@end

@implementation MKRViewController

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

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

- (void)viewDidLoad
{
    [super viewDidLoad];
//    http://openod.sign.qingting.fm/mp3/3235228_128.mp3?sign=47d740e207069a86c539d2c78528f9c2&t=5d8d2d55&clientID=OTRmNmQyZDgtNWZiMi0xMWU3LTkyM2YtMDAxNjNlMDAyMGFk
//    NSString *urlString = [@"http://radio1964-zhibo.oss-cn-shenzhen.aliyuncs.com/audio_data/180112/榜单bandcamp第一期20181月12.mp3" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *string1 = [@"http://openod.sign.qingting.fm/mp3/3235228_128.mp3?sign=47d740e207069a86c539d2c78528f9c2&t=5d8d2d55&clientID=OTRmNmQyZDgtNWZiMi0xMWU3LTkyM2YtMDAxNjNlMDAyMGFk" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    NSString *realString = [[urlString urlEncodeString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:string1];
    
//    self.player = [[MKRAVPlayer alloc]initWithContentURL:url];
//    self.player.shouldAutoPlay = YES;
//    [self.player play];
//    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
//    [self.view addSubview:self.tableView];
//    [[MKRWIFIDeviceManager shareManager] startUpnp];
//    [self searchDevice];
}

- (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];
}

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

@end
