//
//  RCTAudio.m
//  RCTOMPlayer
//
//  Created by lcg on 2017/7/13.
//  Copyright © 2017年 Anren. All rights reserved.
//

#import "RCTAudio.h"
#import "RCTAudioEvent.h"
#import "RCTMediaPlayerDelegate.h"

@interface RCTAudio()<RCTMediaPlayerDelegate>

@end

@implementation RCTAudio

- (instancetype)init
{
    if (self = [super init])
    {
        self.player = [[RCTMediaPlayer alloc] init];
        self.player.delegate = self;
    }
    
    return self;
}

- (void)dealloc
{
    
}

- (void)releaseResource
{
    [self.player stop];
    self.player.delegate = nil;
    self.player = nil;
}

- (NSDictionary*)getEventBody:(NSDictionary*)info
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    if (info)
    {
        [dic setDictionary:info];
    }
    
    [dic setValue:[NSNumber numberWithUnsignedLongLong:self.playerId] forKey:@"id"];
    
    return dic;
}

#pragma mark - RCTMediaPlayerDelegate

- (void)OnLoadStart:(NSDictionary*)info
{
    NSDictionary *dic = [self getEventBody:info];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_LOAD_START withBody:dic];
    }
}

- (void)OnLoad:(NSDictionary*)info
{
    NSDictionary *dic = [self getEventBody:info];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_LOAD withBody:dic];
    }
}

- (void)OnLoadStalled
{
    NSDictionary *dic = [self getEventBody:nil];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_LOAD_STALLED withBody:dic];
    }
}

- (void)OnLoadResume
{
    NSDictionary *dic = [self getEventBody:nil];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_LOAD_RESUME withBody:dic];
    }
}

- (void)OnError:(NSDictionary*)info
{
    NSDictionary *dic = [self getEventBody:info];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_ERROR withBody:dic];
    }
}

- (void)OnProgress:(NSDictionary*)info
{
    NSDictionary *dic = [self getEventBody:info];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_PROGRESS withBody:dic];
    }
}

- (void)OnSeek:(NSDictionary*)info
{
    NSDictionary *dic = [self getEventBody:info];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_SEEK withBody:dic];
    }
}

- (void)OnEnd
{
    NSDictionary *dic = [self getEventBody:nil];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_END withBody:dic];
    }
}

- (void)OnReayForPlay
{
    NSDictionary *dic = [self getEventBody:nil];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_READY_FOR_PLAY withBody:dic];
    }
}

- (void)OnPlaybackStalled
{
    NSDictionary *dic = [self getEventBody:nil];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_STALLED withBody:dic];
    }
}

- (void)OnPlaybackResume
{
    NSDictionary *dic = [self getEventBody:nil];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_RESUME withBody:dic];
    }
}

- (void)OnPlaybackRateChange:(NSDictionary*)info
{
    NSDictionary *dic = [self getEventBody:info];
    if (self.delegate)
    {
        [self.delegate sendEvent:EVENT_OM_AUDIO_RATE_CHANGE withBody:dic];
    }
}

@end
