#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"
#include <stdarg.h>

/*
320 Perform autorefresh
330 Remove autorefresh songs
331 Remove non-autorefresh songs
340 Delayed autorefresh
360 Abort directory reading
*/

HINSTANCE hInst;
HWND OutInfoList;

void debugf(char *format, ...)
{
    char buf[4090];
    va_list marker;

    va_start(marker, format);
    vsprintf(buf, format, marker);
    OutputDebugString(buf);
    printf(buf);
    printf("\n");
    //printf()
    //vscanf()
}

void addlistboxf(HWND hwndListBox, char const *format, ...)
{
    char buf[4090];
    va_list marker;

    va_start(marker, format);
    vsprintf(buf, format, marker);
    SendMessage(hwndListBox, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) buf);
}

void OutOnOffSetting(char const *StrMode, UINT ResultCode)
{
    if (ResultCode)
        addlistboxf(OutInfoList, StrMode, "on");
    else
        addlistboxf(OutInfoList, StrMode, "off");
}

void ControlClick(UINT ControlID)
{
    HWND hWndCalaym;
    UINT ControlCode;
    UINT Result;

    if (!ControlID)
        return;
    if (ControlID == 60000)
        return;

    // For Dialog control IDs, Calaym control codes are used
    ControlCode = ControlID;

    SendMessage(OutInfoList, LB_RESETCONTENT, 0, 0);
    addlistboxf(OutInfoList, "This is control code %i", ControlCode);

    hWndCalaym = FindWindow("CalaymInterMsgClass101", "Calaym");
    if (!hWndCalaym)
    {
        addlistboxf(OutInfoList, "Could not detect Calaym.");
        return;
    }
    addlistboxf(OutInfoList, "Calaym IPC window detected: %i (0x%x)", hWndCalaym, hWndCalaym);
    Result = SendMessage(hWndCalaym, WM_COMMAND, ControlCode, 0);
    addlistboxf(OutInfoList, "Return value: %i (0x%x)", Result, Result);
    if (((ControlCode >= 200) && (ControlCode <= 205)) || (ControlCode == 209))
    {
        switch (Result)
        {
            case 0: addlistboxf(OutInfoList, "Playstate: stopped"); break;
            case 1: addlistboxf(OutInfoList, "Playstate: playing"); break;
            case 2: addlistboxf(OutInfoList, "Playstate: paused");  break;
        }
    }
    else if ((ControlCode == 222) || (ControlCode == 223) || (ControlCode == 224) || ((ControlCode >= 230) && (ControlCode <= 240)))
        addlistboxf(OutInfoList, "Volume: %i/255", Result);
    else if ((ControlCode >= 390) && (ControlCode <= 393))
        OutOnOffSetting("MP3 Infos are %s", Result);
    else if ((ControlCode >= 400) && (ControlCode <= 404))
    {
        switch (Result)
        {
            case 0: addlistboxf(OutInfoList, "Repeat mode: Single"); break;
            case 1: addlistboxf(OutInfoList, "Repeat mode: All"); break;
            case 2: addlistboxf(OutInfoList, "Repeat mode: Random");  break;
        }
    }
    else if ((ControlCode >= 410) && (ControlCode <= 413))
        OutOnOffSetting("Fading (Fade in/out) is %s", Result);
    else if ((ControlCode >= 420) && (ControlCode <= 423))
        OutOnOffSetting("Spectrum analyzer is %s", Result);
    else if ((ControlCode >= 430) && (ControlCode <= 433))
        OutOnOffSetting("Remove missing songs is %s", Result);
    else if ((ControlCode >= 440) && (ControlCode <= 443))
        OutOnOffSetting("Remove manually skipped/ended songs is %s", Result);
    else if ((ControlCode >= 450) && (ControlCode <= 453))
        OutOnOffSetting("Remove played songs is %s", Result);
    else if ((ControlCode >= 460) && (ControlCode <= 468))
    {
        switch (Result)
        {
            case 0: addlistboxf(OutInfoList, "End-song-processing: Off (continous playback)"); break;
            case 1: addlistboxf(OutInfoList, "End-song-processing: Crossfading"); break;
            case 2: addlistboxf(OutInfoList, "End-song-processing: MiniDisk record mode");  break;
            case 3: addlistboxf(OutInfoList, "End-song-processing: Stop after each song");  break;
        }
    }
    else if ((ControlCode >= 480) && (ControlCode <= 483))
        OutOnOffSetting("Intro mode is %s", Result);
}


BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
        {
            OutInfoList = GetDlgItem(hwndDlg, 60000);
            return TRUE;
        }
        case WM_CLOSE:
        {
            EndDialog(hwndDlg, 0);
            return TRUE;
        }

        case WM_COMMAND:
        {
            ControlClick(LOWORD(wParam));
            return TRUE;
        }
    }
    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst=hInstance;
    InitCommonControls();
    return DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgMain);
}
