#import "IMediaTable.h"
#import "IMediaD.h"
#import <strings.h>
#import <appkit/Panel.h>
#import "errdebug.h"

#import "Localization.h"

#define LoStr(key)      doLocalString(NULL,key,NULL)

// This exists as a class only there are no instances.  It maps file
// extensions and RTF control words to media Classes and vice versa.

extern const char *mediaClassNameTable[];
extern Class MediaClassTable[];

@implementation IMediaTable

static unsigned int count;

+ initialize
// initialize from mediaClassNameTable.  mediaClassNameTable is the only
// source file that directly references a media Class.  At execute time
// a media Class is always referenced indirectly through this class.
{
    const char **mediaClassNamePtr;
    Class *MediaClassPtr;

    for (mediaClassNamePtr = mediaClassNameTable,
        MediaClassPtr = MediaClassTable; *mediaClassNamePtr != NULL;
        ++mediaClassNamePtr, ++MediaClassPtr) {
        if ((*MediaClassPtr = (Class)objc_getClass(*mediaClassNamePtr))
            == Nil) {
            NXRunAlertPanel(LoStr("NewsBase"),LoStr("Media Class for %s is not linked in NewsBase."),NULL,NULL,NULL, *mediaClassNamePtr);
        }
    }
    count = MediaClassPtr - MediaClassTable;
    *MediaClassPtr = Nil;
    return(self);
}

+ (Class)mediaClassAt:(unsigned int)index
{
    return(MediaClassTable[index]);
}

+ (unsigned int)count
{
    return(count);
}

+ (Class)mediaClassForRTFControlWord:(NXAtom)controlWord
// map RTF control word to media Class.
{
    Class *MediaClassPtr;

    for (MediaClassPtr = MediaClassTable; *MediaClassPtr != Nil;
        ++MediaClassPtr) {
        if (controlWord == [(id)*MediaClassPtr rtfControlWord]) {
            return(*MediaClassPtr);
        }
    }
    return(Nil);
}

+ (Class)mediaClassForFileExtension:(NXAtom)fileExtension
// map file extension to media Class.
{
    Class *MediaClassPtr;

    fileExtension = NXUniqueString(fileExtension);
    for (MediaClassPtr = MediaClassTable; *MediaClassPtr != Nil;
        ++MediaClassPtr) {
        if (fileExtension == [(id)*MediaClassPtr fileExtension]) {
            return(*MediaClassPtr);
        }
    }
    return(Nil);
}

+ (Class)mediaClassForPasteboardType:(NXAtom)pasteboardType
{
    Class *MediaClassPtr;

    for (MediaClassPtr = MediaClassTable; *MediaClassPtr != Nil;
        ++MediaClassPtr) {
        if (pasteboardType == [(id)*MediaClassPtr pasteboardType]) {
            return(*MediaClassPtr);
        }
    }
    return(Nil);
}

@end;
