#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA678_F31E_F31J_X31J_P31J_SU1E_AU1E_AU2E_AU3E
void F7078_SortFileNames(REGISTER char* P3751_pc_FileNamesList, unsigned long P3752_ul_FileCount, unsigned long P3753_ul_FileNameMaximumLength, int16_t (*P3754_pfi_StringCompare)())
{
#ifdef MEDIA671_F31E_F31J_P31J
        unsigned long L4006_ul_;
        unsigned long L4007_ul_;
        unsigned long L4008_ul_;
#endif
#ifdef MEDIA700_X31J_SU1E_AU1E_AU2E_AU3E
        register unsigned long L4006_ul_;
        register unsigned long L4007_ul_;
        register unsigned long L4008_ul_;
#endif
        REGISTER char L4009_c_;


        for (L4006_ul_ = 0; L4006_ul_ < P3752_ul_FileCount * P3753_ul_FileNameMaximumLength; L4006_ul_ += P3753_ul_FileNameMaximumLength) {
                for (L4007_ul_ = L4006_ul_ + P3753_ul_FileNameMaximumLength; L4007_ul_ < P3752_ul_FileCount * P3753_ul_FileNameMaximumLength; L4007_ul_ += P3753_ul_FileNameMaximumLength) {
                        if ((*P3754_pfi_StringCompare)(P3751_pc_FileNamesList + L4007_ul_, P3751_pc_FileNamesList + L4006_ul_) < 0) { /* If file names are not sorted alphabetically */
                                for (L4008_ul_ = 0; L4008_ul_ < P3753_ul_FileNameMaximumLength; L4008_ul_++) { /* Swap file names */
                                        L4009_c_ = P3751_pc_FileNamesList[L4006_ul_ + L4008_ul_];
                                        P3751_pc_FileNamesList[L4006_ul_ + L4008_ul_] = P3751_pc_FileNamesList[L4007_ul_ + L4008_ul_];
                                        P3751_pc_FileNamesList[L4007_ul_ + L4008_ul_] = L4009_c_;
                                }
                        }
                }
        }
}

#ifdef MEDIA773_SU1E_AU1E
int16_t F7316_GetFileNamesArray(
#ifdef MEDIA772_SU1E
char* P4109_pc_FileNames)
#endif
#ifdef MEDIA762_AU1E
char (*P4109_pc_FileNames)[9])
#endif
{
#ifdef MEDIA772_SU1E
        register char* L5824_;
        register int16_t L4641_i_;
        register int16_t L4642_i_;
        char L4643_ac_[50];
        register int16_t L5794_;
        register char L4648_c_;
        DTA* L5793_ps_;
        DTA L5792_s_;


        L5824_ = P4109_pc_FileNames;
        F0088_strcpy(L4643_ac_, G3771_);
        F0086_strcat(L4643_ac_, "*");
        F0086_strcat(L4643_ac_, G3803_);
        F1928_ForceMediaChangeDetection(G3770_);
        L5793_ps_ = Fgetdta();
        Fsetdta(&L5792_s_);
        L4641_i_ = 0;
        L4642_i_ = Fsfirst(L4643_ac_, 0);
        while ((L4642_i_ == 0) && (L4641_i_ < 150)) {
                L5794_ = 0;
                while ((L4648_c_ = L5792_s_.d_fname[L5794_]) && (L4648_c_ != '.')) {
                        L5824_[L5794_++] = L4648_c_;
                }
                if (L5794_) {
                        L5824_[L5794_] = '\0';
                        L5824_ += 9;
                        L4641_i_++;
#endif
#ifdef MEDIA762_AU1E
        register char* L6181_;
        register int16_t L6182_;
        register int16_t L6183_;
        int16_t L6180_;
        int16_t L4641_i_;


        L6180_ = F2100_GetChildFiles(G3771_, G2300_FileNamesList);
        L4641_i_ = 0;
        for (L6183_ = 0; L6183_ < L6180_; L6183_++) {
                L6181_ = G2300_FileNamesList[L6183_];
                L6182_ = 0;
                while (L6182_ < 9) {
                        if (*L6181_ == '\0') {
                                break;
                        }
                        if (*L6181_ == '.') {
                                if (F0087_strcmp(L6181_, G3803_)) {
                                        break;
                                }
                                *L6181_ = '\0';
                                F0088_strcpy(P4109_pc_FileNames + L4641_i_++, G2300_FileNamesList[L6183_]);
                                break;
                        }
                        L6182_++;
                        L6181_++;
#endif
                }
#ifdef MEDIA772_SU1E
                L4642_i_ = Fsnext();
#endif
        }
        if (L4641_i_ > 1) {
                F7078_SortFileNames(P4109_pc_FileNames, (long)L4641_i_, 9L, F0087_strcmp);
        }
#ifdef MEDIA772_SU1E
        Fsetdta(L5793_ps_);
#endif
        return L4641_i_;
}
#endif

#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
void F7079_ConvertFileNameToUpperCase(char* P3755_pc_, char* P3756_pc_)
{
        unsigned int16_t L4011_ui_;
        unsigned char L4010_uc_;


        for (L4011_ui_ = 0; L4011_ui_ < G4187_FilePickerFileName.MaximumLength; L4011_ui_++) {
                L4010_uc_ = P3755_pc_[L4011_ui_];
                if (((L4010_uc_ >= 'A') && (L4010_uc_ <= 'Z')) || ((L4010_uc_ >= 'a') && (L4010_uc_ <= 'z'))) {
                        L4010_uc_ = ((L4010_uc_ < 'a') || (L4010_uc_ > 'z')) ? L4010_uc_ : L4010_uc_ + 'A' - 'a';
                }
                P3756_pc_[L4011_ui_] = L4010_uc_;
        }
        P3756_pc_[L4011_ui_] = '\0';

}

void F7080_DrawFilePickerFileList(
)
{
        unsigned int16_t L4014_ui_;
        char* L4012_pc_;
        unsigned int16_t L4015_i_;
        unsigned char L4013_auc_FileName[10];


        L4015_i_ = G4186_s_FilePicker.FileList->FileNameMaximumLength;
        F7261_ClearBitmap(G4186_s_FilePicker.FileListBitmap, G4186_s_FilePicker.FileListWidth, G4186_s_FilePicker.FileListHeight);
        for (L4014_ui_ = 0; (L4014_ui_ < G7048_FilePickerFileListVisibleFileNameCount) && (L4014_ui_ < G4186_s_FilePicker.FileList->FileCount); L4014_ui_++) {
                L4012_pc_ = &G4186_s_FilePicker.FileList->FileNamesList[(G4186_s_FilePicker.FirstFileNameIndex + L4014_ui_) * L4015_i_];
                F7079_ConvertFileNameToUpperCase(L4012_pc_, M772_CAST_PC(L4013_auc_FileName));
                F7341_DrawText(G4186_s_FilePicker.FileListBitmap, G4186_s_FilePicker.FileListWidth, G7050_FilePickerFileListLeft, (L4014_ui_ * G7049_FilePickerFileListLineHeight) + G7051_FilePickerFileListTop, C15_COLOR_WHITE, C00_COLOR_BLACK, L4013_auc_FileName, G4186_s_FilePicker.FileListHeight);
        }
        F7259_DrawBitmapOnScreen(G4186_s_FilePicker.FileListBitmap, G4186_s_FilePicker.Box_FileList, G4186_s_FilePicker.FileListWidth, CM1_COLOR_NO_TRANSPARENCY, G4186_s_FilePicker.FileListHeight);
}

void F7081_SortFileNamesIfPossible(
)
{
        if (G4186_s_FilePicker.FileList->StringCompare) {
                F7078_SortFileNames((char*)G4186_s_FilePicker.FileList->FileNamesList, (long)G4186_s_FilePicker.FileList->FileCount, (long)G4186_s_FilePicker.FileList->FileNameMaximumLength, G4186_s_FilePicker.FileList->StringCompare);
        }
}
#endif

#ifdef MEDIA773_SU1E_AU1E
int16_t F7082_DrawFilePickerDialog(char* P3757_pc_) /* May be F7084_ instead ! */
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
int16_t F7082_DrawFilePickerDialog(FILE_LIST* P3757_ps_)
#endif
{
#ifdef MEDIA773_SU1E_AU1E
        register char* L5795_;
        register int16_t* L5796_;
        register unsigned int16_t L4016_i_;
        register int16_t L5797_;
        register int16_t L5798_;
        register int16_t L5799_;
        char* L5800_;
        int16_t L5801_;
        int16_t L5802_;
        int16_t L5803_;
        int16_t L5804_;
        char (*L5805_)[150][9];
        UD_COMMAND L5807_;
        INPUT_INFO** L5808_;
        INPUT_INFO* L5809_aps_FilePickerInputInfoList[6];
        int16_t L5812_[4];


#ifdef MEDIA772_SU1E
        L5805_ = F7023_Alloc((long)(150 * 9));
#endif
#ifdef MEDIA762_AU1E
        L5805_ = F2095_AllocateMemory((long)(150 * 9));
#endif
        L5797_ = F7316_GetFileNamesArray(L5805_);
        if (L5797_ == 0) {
#ifdef MEDIA772_SU1E
                F7072_GetDialogChoiceOrdinal(G3789_THERE_ARE_NO_PORTRAITS_ON_THIS_DISK, C08_COLOR_RED | MASK0x8000_INVERT_BOX, G7060_OK, &G2263_s_Input_Dialog1Choice);
#endif
#ifdef MEDIA762_AU1E
                F7074_DrawDialogErrorMessage(G3789_THERE_ARE_NO_PORTRAITS_ON_THIS_DISK, C08_COLOR_RED | MASK0x8000_UNKNOWN);
#endif
                L4016_i_ = 0;
        } else {
                F7072_GetDialogChoiceOrdinal(NULL, C00_COLOR_BLACK | MASK0x8000_INVERT_BOX, NULL, NULL);
                F7070_DrawDialogTitle(G3785_SELECT_A_CHAMPION_PORTRAIT, 202, 66, C15_COLOR_WHITE);
                F7030_DrawFilledBox(G2285_as_FilePickerDialogButtons[0].Box, 1, C02_COLOR_LIGHT_GRAY, C00_COLOR_BLACK);
#ifdef MEDIA772_SU1E
                F7259_DrawBitmapOnScreen(G7116_Bitmap_FilePickerArrows, G2284_Box_FilePickerArrows, 16, C01_COLOR_DARK_GRAY);
#endif
#ifdef MEDIA762_AU1E
                F0132_VIDEO_Blit(G7116_Bitmap_FilePickerArrows, G0348_Bitmap_Screen, G2284_Box_FilePickerArrows, 0, 0, 32, 320, C01_COLOR_DARK_GRAY, 75, 200);
#endif
                F7034_DrawButton(&G2285_as_FilePickerDialogButtons[1], G7066_NEW_DISK, 2);
                F7034_DrawButton(&G2285_as_FilePickerDialogButtons[2], G7067_CANCEL, 2);
                F0008_MAIN_ClearBytes(L5809_aps_FilePickerInputInfoList, (long)sizeof(L5809_aps_FilePickerInputInfoList));
                L5809_aps_FilePickerInputInfoList[0] = &G2287_s_InputInfo_FilePickerDialog;
                L5808_ = F7014_SetCurrentInputInfoList(L5809_aps_FilePickerInputInfoList);
                L5804_ = M770_BOX_TOP(G2285_as_FilePickerDialogButtons[0].Box) - 1;
                L5799_ = 0;
                for (L5798_ = 0; (L5798_ < 9) && (L5798_ < L5797_); L5798_++) {
                        F7339_PrintTextToScreen(80, 69 + (L5798_ * 8), 15, 0, (*L5805_)[L5798_]);
                }
                if (L5797_ > 9) {
#ifdef MEDIA772_SU1E
                        L5800_ = F7023_Alloc(2080L);
#endif
#ifdef MEDIA762_AU1E
                        L5800_ = F7023_Alloc(2080L, MASK0x0008_MEMREQ);
#endif
                }
                L4016_i_ = 0;
                do {
#ifdef MEDIA772_SU1E
                        if (!(G3506_ & 1) || ((L4016_i_ >= 4) && (!F7006_IsPointInBox(G2285_as_FilePickerDialogButtons[L4016_i_ - 1].Box, G3503_i_MouseX, G3504_i_MouseY)))) {
#endif
#ifdef MEDIA762_AU1E
                        if (!F2008_IsLeftMouseButtonDown() || ((L4016_i_ >= 4) && (!F7006_IsPointInBox(G2285_as_FilePickerDialogButtons[L4016_i_ - 1].Box, F2009_GetMouseX(), F2010_GetMouseY())))) {
#endif
                                if (L4016_i_ >= 4) {
                                        F7248_InvertBox(L5812_);
                                }
                                L4016_i_ = 0;
                        }
                        F0078_MOUSE_DisableScreenUpdate();
                        do {
                                if (!(L5803_ = (L4016_i_ >= 4))) {
                                        while ((L4016_i_ = F7015_ProcessNextUDCommand(&L5807_)) == 0);
                                }
                                F7317_MAIN_CopyBytes_Alt(G2285_as_FilePickerDialogButtons[L4016_i_ - 1].Box, L5812_, MASK0x0400_MEMREQ, MASK0x0400_MEMREQ, (long)sizeof(L5812_));
                                if (L4016_i_ != 1) {
                                        goto loc_2316;
                                }
                                L5798_ = (L5807_.Y - L5804_) / 8;
                                if (L5798_ > 8) {
                                        L5798_ = 8;
                                }
                        } while (L5798_ >= L5797_);
                        L5812_[2] += L5798_ * 8;
                        L5812_[3] = L5812_[2] + 8;
                        F0088_strcpy(P3757_pc_, G3771_);
                        F0086_strcat(P3757_pc_, (*L5805_)[L5798_ + L5799_]);
                        F0086_strcat(P3757_pc_, G3803_);
                        loc_2316:
                        if (L5803_ == 0) {
                                F0077_MOUSE_EnableScreenUpdate_CPSE();
                                F7248_InvertBox(L5812_);
                        }
                        if (L4016_i_ <= 3) {
                                continue;
                        }
                        if (((L4016_i_ == 4) && (L5799_ > 0)) || ((L4016_i_ == 5) && (L5797_ > L5799_ + 9))) {
                                if (L4016_i_ == 4) {
                                        L5802_ = 63;
                                        L5795_ = G3809_;
                                        L5796_ = G3810_;
                                        L5801_ = L5796_[3] - 1;
                                        L5799_--;
                                        L5798_ = L5799_;
                                } else {
                                        L5802_ = 71;
                                        L5795_ = G3811_;
                                        L5796_ = G3812_;
                                        L5801_ = L5796_[3] - 2;
                                        L5798_ = L5799_ + 9;
                                        L5799_++;
                                }
#ifdef MEDIA772_SU1E
                                M526_WaitVerticalBlank();
#endif
#ifdef MEDIA762_AU1E
                                F7323_WaitVerticalBlank();
#endif
                                if (L5803_) {
                                        F0077_MOUSE_EnableScreenUpdate_CPSE();
                                }
#ifdef MEDIA772_SU1E
                                G0578_B_UseByteBoxCoordinates = C0_FALSE;
                                F0132_VIDEO_Blit(G0348_Bitmap_Screen, L5800_, &G3808_, 77, L5802_, C160_BYTE_WIDTH_SCREEN, 32, CM1_COLOR_NO_TRANSPARENCY);
                                F7259_DrawBitmapOnScreen(L5800_, L5795_, 32, CM1_COLOR_NO_TRANSPARENCY);
#endif
#ifdef MEDIA762_AU1E
                                F0132_VIDEO_Blit(G0348_Bitmap_Screen, L5800_, &G3808_, 77, L5802_, 320, 64, CM1_COLOR_NO_TRANSPARENCY, 200, 65);
                                F0132_VIDEO_Blit(L5800_, G0348_Bitmap_Screen, L5795_, 0, 0, 64, 320, CM1_COLOR_NO_TRANSPARENCY, 65, 200);
#endif
                                F7260_FillScreenBox(L5796_, C00_COLOR_BLACK);
                                F7339_PrintTextToScreen(80, L5801_, C15_COLOR_WHITE, 0, (*L5805_)[L5798_]);
                                continue;
                        }
                        if (L5803_) {
#ifdef MEDIA772_SU1E
                                M526_WaitVerticalBlank();
#endif
#ifdef MEDIA762_AU1E
                                F7323_WaitVerticalBlank();
#endif
                                F0077_MOUSE_EnableScreenUpdate_CPSE();
                        }
                } while (L4016_i_ > 3);
                if (L5797_ > 9) {
                        F7024_Free(L5800_);
                }
                F7014_SetCurrentInputInfoList(L5808_);
                if (L4016_i_ == 3) {
                        L4016_i_ = 0;
                } else {
                        L4016_i_ = 1;
                }
        }
        F7024_Free(L5805_);
        return L4016_i_;
#endif
#ifdef MEDIA677_F31E_F31J_X31J_P31J_AU2E_AU3E
        int16_t L4016_i_;
        REGISTER long L6031_l_Unreferenced;


        if (P3757_ps_->pfv_04) {
                (*P3757_ps_->pfv_04)(P3757_ps_);
        }
        if (G4186_s_FilePicker.OnScreen & 1) {
                L4016_i_ = -1;
        } else {
                G4186_s_FilePicker.FileListBitmap = NULL;
                G4186_s_FilePicker.FileList = P3757_ps_;
                G4187_FilePickerFileName.MaximumLength = 8;
                G4187_FilePickerFileName.InvertSelectedBoxRequested = C0_FALSE;
                F7317_MAIN_CopyBytes_Alt((char*)G2285_as_FilePickerDialogButtons[0].Box, (char*)G4186_s_FilePicker.Box_FileList, MASK0x0400_MEMREQ, MASK0x0400_MEMREQ, 8);
                G4186_s_FilePicker.FileListWidth = M769_BOX_RIGHT(G4186_s_FilePicker.Box_FileList) - M768_BOX_LEFT(G4186_s_FilePicker.Box_FileList) + 1;
                G4186_s_FilePicker.FileListHeight = M771_BOX_BOTTOM(G4186_s_FilePicker.Box_FileList) - M770_BOX_TOP(G4186_s_FilePicker.Box_FileList) + 1;
                G4186_s_FilePicker.FileListBitmapByteCount = F7249_GetBitmapByteCount(G4186_s_FilePicker.FileListWidth, G4186_s_FilePicker.FileListHeight);
                G4186_s_FilePicker.FileListBitmap = F7023_Alloc(G4186_s_FilePicker.FileListBitmapByteCount, MASK0x0008_MEMREQ);
                F7072_GetDialogChoiceOrdinal(NULL, C00_COLOR_BLACK | MASK0x8000_INVERT_BOX, NULL, NULL);
                F7070_DrawDialogTitle(G4186_s_FilePicker.FileList->Title, G7033_FilePickerDialogTitleX, G7034_FilePickerDialogTitleY, C15_COLOR_WHITE);
                F7030_DrawFilledBox(G4186_s_FilePicker.Box_FileList, 1, C02_COLOR_LIGHT_GRAY, C00_COLOR_BLACK);
                F7259_DrawBitmapOnScreen(G7116_Bitmap_FilePickerArrows, G2284_Box_FilePickerArrows, G7046_FilePickerArrowsWidth, C01_COLOR_DARK_GRAY, G7047_FilePickerArrowsHeight);
                F7034_DrawButton(&G2285_as_FilePickerDialogButtons[1], G7066_NEW_DISK, C02_COLOR_LIGHT_GRAY);
                F7034_DrawButton(&G2285_as_FilePickerDialogButtons[2], G7067_CANCEL, C02_COLOR_LIGHT_GRAY);
                G4186_s_FilePicker.FileListTopOnScreen = M770_BOX_TOP(G2285_as_FilePickerDialogButtons[0].Box) - 1;
                G4186_s_FilePicker.FirstFileNameIndex = 0;
                F7081_SortFileNamesIfPossible();
                F7080_DrawFilePickerFileList();
                G4186_s_FilePicker.OnScreen = 1;
                F0008_MAIN_ClearBytes(M772_CAST_PC(G4186_s_FilePicker.InputInfoList), (long)sizeof(G4186_s_FilePicker.InputInfoList));
                G4186_s_FilePicker.InputInfoList[0] = &G2287_s_InputInfo_FilePickerDialog;
                G4186_s_FilePicker.InputInfo = F7014_SetCurrentInputInfoList(G4186_s_FilePicker.InputInfoList);
                L4016_i_ = 0;
        }
        if (L4016_i_);
        return L4016_i_;
#endif
}
#endif
