#include <windows.h>
#include <conio.h>
#include <iostream>
using namespace std;
#include <mmsystem.h>
#include <stdio.h>
#include <assert.h>
#pragma comment(lib,"winmm.lib")

#pragma warning(disable:4305)
#pragma warning(disable:4309)

void   clrscr(HANDLE hndl)   
{      
    CONSOLE_SCREEN_BUFFER_INFO   csbi={0};   
    GetConsoleScreenBufferInfo(hndl,   &csbi);   
    DWORD   written;   
    DWORD   N   =   csbi.dwSize.X   *   csbi.dwCursorPosition.Y   +   
        csbi.dwCursorPosition.X   +   1;   
    COORD   curhome   =   {0,0};   
    
    FillConsoleOutputCharacter(hndl,   '   ',   N,   curhome,   &written);   
    csbi.srWindow.Bottom   -=   csbi.srWindow.Top;   
    csbi.srWindow.Top   =   0;   
    SetConsoleWindowInfo(hndl,   TRUE,   &csbi.srWindow);   
    SetConsoleCursorPosition(hndl,   curhome);     
}
class Player
{
   char  m_lpszTime[MAX_PATH];
   DWORD m_dwDeviceID;
   bool  m_bIsPlay;
public:
    Player(LPSTR lpszFileName):m_dwDeviceID(0),m_bIsPlay(false)
    {
      MCI_OPEN_PARMS OpenParms={0};
      OpenParms.lpstrDeviceType=(LPCSTR) MCI_DEVTYPE_SEQUENCER;//"mpegvideo";
      OpenParms.lpstrElementName=lpszFileName;
      mciSendCommandA(NULL,MCI_OPEN,
          MCI_WAIT|
          //MCI_OPEN_TYPE|
          //MCI_OPEN_TYPE_ID|
          MCI_OPEN_ELEMENT,
          (DWORD)(LPVOID)&OpenParms);
      m_dwDeviceID=OpenParms.wDeviceID;
    }
    virtual ~Player(){}
    void SetTime(int h,int m,int s)
    {
        memset(m_lpszTime,0,MAX_PATH);
        wsprintf(m_lpszTime,"%d:%d:%d",h,m,s);
    }
    bool DiffTime(int h,int m,int s)
    {
        char szTemp[MAX_PATH]="";
        wsprintf(szTemp,"%d:%d:%d",h,m,s);
        return memcmp(m_lpszTime,szTemp,MAX_PATH)==0;
    }
    void Play()
    {
        m_bIsPlay=true;
        MCI_PLAY_PARMS PlayParms;
        PlayParms.dwFrom=0;
        mciSendCommandA(m_dwDeviceID,MCI_PLAY,MCI_FROM,
            (DWORD)(LPVOID)(&PlayParms));
    }
    bool IsPlay()
    {
        return m_bIsPlay;
    }
};
int main()
{
    Player player("mp3.mp3");
    int h,m,s;
    HANDLE   hndl   =   GetStdHandle(STD_OUTPUT_HANDLE);
    
    clrscr(hndl);
    int count=0;
    do
    {
        clrscr(hndl);
        cout << "我是小闹钟-_-\n\n";
        cout << "输入定时播放MP3的时间,格式:HH:MM:SS 回车 如:12:30:00 回车\n";
        char getstr[MAX_PATH]="";
        bool ok=true;
        cin>>getstr;
        sscanf(getstr,"%d",&h);
        sscanf(getstr+3,"%d",&m);
        sscanf(getstr+6,"%d",&s);
        do
        {
            if (h<0||h>23) {ok=false;break;}
            if (m<0||m>59) {ok=false;break;}
            if (s<0||s>59) {ok=false;break;}
        }while(0);
        if (ok)
        break;
        else
        {
            ++count;
            if (count==3)
            {
                clrscr(hndl);
                cout << "小闹钟被你玩了太多次,很不开心,自杀了...";
                cin.get();
                cin.get();
                CloseHandle(hndl);
                exit(0);
            }
            clrscr(hndl);
            cout << "不要玩我啊!~我只是个小闹钟-_-||\n(按任意键继续...)\n";
            cin.get();
            cin.get();
        }
    }while(1);
    
    player.SetTime(h,m,s);
    int ret=SetTimer(NULL,0,1000,NULL);
    assert(ret!=0);
    MSG msg={0};
    SYSTEMTIME systime={0};
    clrscr(hndl);
    while (GetMessage(&msg,NULL,0,0))
    {
        if (msg.message==WM_TIMER)
        {
            GetLocalTime(&systime);
            char szTime[MAX_PATH]="";
            
            wsprintf(szTime,"\r    现在时间:%d:%d:%d",systime.wHour,systime.wMinute,systime.wSecond);
            
            if (player.DiffTime(systime.wHour,systime.wMinute,systime.wSecond))
            {
                if(!player.IsPlay())
                {
                 player.Play();
                 cout <<"\r    时间到了,正在播放MP3...\n";
                }
            }
            else
            {
                cout << "\r    现在时间:  :  :  ";
                cout << szTime;
            }
        }
    }    

    KillTimer(NULL,0);
    CloseHandle(hndl);
    return 0;
}