//
//  TrackPlayerBridge.mm
//  TrackPlayer
//
//  Created by David Chavez on 7/26/24.
//

#import "TrackPlayerBridge.h"
#import <React/RCTViewManager.h>

@interface RCT_EXTERN_MODULE(AirPlayButtonManager, RCTViewManager)
@end

@implementation TrackPlayer (RCTExternModule)

RCT_EXPORT_MODULE_NO_LOAD(TrackPlayer, TrackPlayer)

// Setup
RCT_EXTERN_METHOD(setupPlayer:(NSDictionary *)data);

// Playback
RCT_EXTERN_METHOD(play);
RCT_EXTERN_METHOD(pause);
RCT_EXTERN_METHOD(stop);
RCT_EXTERN_METHOD(seekTo:(double)position);
RCT_EXTERN_METHOD(seekBy:(double)offset);
RCT_EXTERN_METHOD(skipToNext);
RCT_EXTERN_METHOD(skipToPrevious);
RCT_EXTERN_METHOD(skipToIndex:(double)index);
RCT_EXTERN_METHOD(retry);
RCT_EXTERN_METHOD(clearCache);
RCT_EXTERN_METHOD(preload:(NSDictionary *)item duration:(double)duration);
RCT_EXTERN_METHOD(cancelPreload:(NSDictionary *)item);
RCT_EXTERN_METHOD(setPlaybackSpeed:(double)speed);
RCT_EXTERN_METHOD(setVolume:(double)volume);

// Queue - set
RCT_EXTERN_METHOD(setMediaItem:(NSDictionary *)data);
RCT_EXTERN_METHOD(setMediaItems:(NSArray *)data);

// Queue - add/insert
RCT_EXTERN_METHOD(addMediaItem:(NSDictionary *)data);
RCT_EXTERN_METHOD(addMediaItems:(NSArray *)data);
RCT_EXTERN_METHOD(insertMediaItem:(double)index mediaItem:(NSDictionary *)data);
RCT_EXTERN_METHOD(insertMediaItems:(double)index mediaItems:(NSArray *)data);

// Queue - remove
RCT_EXTERN_METHOD(removeMediaItem:(double)index);
RCT_EXTERN_METHOD(removeMediaItems:(double)fromIndex toIndex:(double)toIndex);
RCT_EXTERN_METHOD(clear);

// Queue - replace & reorder
RCT_EXTERN_METHOD(replaceMediaItem:(double)index mediaItem:(NSDictionary *)data);
RCT_EXTERN_METHOD(moveMediaItem:(double)fromIndex toIndex:(double)toIndex);

// State getters (sync)
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(getPlaybackState);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(isPlaying);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(getProgress);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(getPlaybackSpeed);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(getVolume);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(getActiveMediaItem);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(getActiveMediaItemIndex);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(getQueue);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(getRepeatMode);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(isShuffleEnabled);

// Queue - update metadata
RCT_EXTERN_METHOD(updateMetadata:(double)index metadata:(NSDictionary *)metadata);

// Player options
RCT_EXTERN_METHOD(setCommands:(NSDictionary *)commands);
RCT_EXTERN_METHOD(setRepeatMode:(NSString *)mode);
RCT_EXTERN_METHOD(setShuffleEnabled:(BOOL)enabled);

// Browse Tree
RCT_EXTERN_METHOD(setBrowseTree:(NSArray *)categories);

// Sleep Timer
RCT_EXTERN_METHOD(sleepAfterTime:(double)seconds fadeOutSeconds:(double)fadeOutSeconds);
RCT_EXTERN_METHOD(sleepAfterMediaItemAtIndex:(double)index);
RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(getSleepTimer);
RCT_EXTERN_METHOD(cancelSleepTimer);

// Destroy
RCT_EXTERN_METHOD(destroy);

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
    return std::make_shared<facebook::react::NativeTrackPlayerSpecJSI>(params);
}

@end
