// UI.cpp : 定义应用程序的入口点。
//

#include "../framework.h"
#include "../UI.h"
#include "wintoastlib.h"
#include <codecvt>

class MyToastHandler :public WinToastLib::IWinToastHandler {
public:

    MyToastHandler() {}

    void toastActivated()const {
        //LOG(DEBUG) << "The user clicked in this toast";

    }

    void toastActivated(int actionIndex)const {
        //LOG(DEBUG) << "The user clicked on action #" << actionIndex;
        m_resultCode = actionIndex;
    }


    void toastDismissed(WinToastDismissalReason state) const {
        switch (state) {
        case UserCanceled:
            //LOG(DEBUG) << "The user dismissed this toast";
            //exit(1);
            break;
        case TimedOut:
            //LOG(DEBUG) << "The toast has timed out";
            //exit(2);
            break;
        case ApplicationHidden:
            //LOG(DEBUG) << "The application hid the toast using ToastNotifier.hide()";
            //exit(3);
            break;
        default:
            //LOG(DEBUG) << "Toast not activated";
            //exit(4);
            break;
        }
        //m_packet->SetShowToast(false);
    }
    void toastFailed() const {
        //LOG(ERROR) << "Error showing current toast";
    }


    static int m_resultCode;
};

int MyToastHandler::m_resultCode = -1;



int APIENTRY wWinMain0(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);



    std::wstring appUserModelID = L"警告";
    WinToastLib::WinToast::instance()->setAppName(L"警告");
    WinToastLib::WinToast::instance()->setAppUserModelId(appUserModelID);
    WinToastLib::WinToastTemplate toast(WinToastLib::WinToastTemplate::ImageAndText02);
    toast.setTextField(L"10s后将关闭plm客户端...", WinToastLib::WinToastTemplate::FirstLine);

    char path[MAX_PATH] = { 0 };
    GetModuleFileNameA(NULL, path, MAX_PATH);

    std::string  filePath = path;
    size_t nPos = filePath.find_last_of('\\');
    filePath = filePath.substr(0, nPos);
    std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    std::wstring wstr = converter.from_bytes(filePath + "\\notify.webp");
    toast.setImagePath(wstr);

    //toast.addAction(L"确定");
    //toast.addAction(L"取消");

    toast.setScenario(WinToastLib::WinToastTemplate::Scenario::Alarm);  //报警
    toast.setExpiration(10000);   //10s

    INT64 id = -1;
    if (WinToastLib::WinToast::instance()->initialize()) {
        id = WinToastLib::WinToast::instance()->showToast(toast, new MyToastHandler());
    }

    int count = 100,ret = -1;
    while (count--) {
        ret = (MyToastHandler::m_resultCode == 0 || MyToastHandler::m_resultCode == 1) ? MyToastHandler::m_resultCode : ret;
        if (ret != -1) {
            ++ret;    //1:确定  2:取消
            break;
        }
        Sleep(100);
    }

    WinToastLib::WinToast::instance()->hideToast(id);

    return ret;
}

