//
// Generated by dtk
// Translation Unit: JKRFileFinder.cpp
//

#include "JSystem/JKernel/JKRFileFinder.h"
#include "JSystem/JKernel/JKRArchive.h"
#include "dolphin/types.h"

/* 802B6CB8-802B6D44       .text __ct__12JKRArcFinderFP10JKRArchivell */
JKRArcFinder::JKRArcFinder(JKRArchive* archive, s32 startIndex, s32 numEntries) {
    mArchive = archive;
    mIsAvailable = numEntries > 0;
    mStartIndex = startIndex;
    mEndIndex = startIndex + numEntries - 1;
    mNextIndex = mStartIndex;
    findNextFile();
}

/* 802B6D44-802B6DF0       .text findNextFile__12JKRArcFinderFv */
bool JKRArcFinder::findNextFile() {
    JKRArchive::SDirEntry entry;

    if (mIsAvailable) {
        mIsAvailable = !(mNextIndex > mEndIndex);
        if (mIsAvailable) {
            mIsAvailable = mArchive->getDirEntry(&entry, mNextIndex);
            mEntryName = entry.name;
            mEntryFileIndex = mNextIndex;
            mEntryId = entry.id;
            mEntryTypeFlags = entry.flags;
            mIsFileOrDirectory = (mEntryTypeFlags >> 1) & 1;
            mNextIndex++;
        }
    }
    return mIsAvailable;
}

/* 802B6DF0-802B6E74       .text __ct__12JKRDvdFinderFPCc */
JKRDvdFinder::JKRDvdFinder(const char* directory) {
    mDvdIsOpen = DVDOpenDir(directory, &mDvdDirectory);
    mIsAvailable = mDvdIsOpen;
    findNextFile();
}

/* 802B6E74-802B6EF4       .text __dt__12JKRDvdFinderFv */
JKRDvdFinder::~JKRDvdFinder() {
    if (mDvdIsOpen) {
        DVDCloseDir(&mDvdDirectory);
    }
}

/* 802B6EF4-802B6F90       .text findNextFile__12JKRDvdFinderFv */
bool JKRDvdFinder::findNextFile() {
    if (mIsAvailable) {
        DVDDirectoryEntry directoryEntry;
        mIsAvailable = DVDReadDir(&mDvdDirectory, &directoryEntry);

        if (mIsAvailable) {
            mIsFileOrDirectory = directoryEntry.is_directory != 0;
            mEntryName = directoryEntry.name;
            mEntryFileIndex = directoryEntry.entry_number;
            mEntryId = 0;

            // only matches with enum
            // TODO: placeholder
            enum EntryTypeFlags {
                EntryTypeFlags1 = 1,
                EntryTypeFlags2 = 2,
            };
            mEntryTypeFlags = mIsFileOrDirectory ? EntryTypeFlags2 : EntryTypeFlags1;
        }
    }

    return mIsAvailable;
}
