// aboutdlg.h : interface of the CAboutDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#include "adetect.h"
#include "ConfigMgr.h"



class CAboutDlg 
    : public CDialogImpl<CAboutDlg>
    , public CWinDataExchangeEx<CAboutDlg> 
{
public:
    CAboutDlg(bool lang)
        : m_IsLang(lang)
        , m_IsSplash(0)
        , m_LangNum(0)
    {
    }

    enum { IDD = IDD_ABOUTBOX };

BEGIN_DDX_MAP(CAboutDlg)
    DDX_COMBO_INDEX(IDC_COMBO_LANG, m_LangNum)
    DDX_CHECK(IDC_CHECK_SPLASH, m_IsSplash)
    DDX_BITMAP(IDC_LOGO, m_strLogo) \
END_DDX_MAP() 

BEGIN_MSG_MAP(CAboutDlg)
    MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
    COMMAND_ID_HANDLER(IDOK, OnClose)
END_MSG_MAP()

    LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
        CenterWindow(GetParent());

        // @TODO:  to add rus/eng page visiting (LangNum dependant)
        //m_Url.SetLabel(_T(""));
        m_Url.SetHyperLink(_T(CPURM_HOMEPAGE));
        m_Url.SubclassWindow(GetDlgItem(IDC_STATIC_URL));

        // create custom font
        m_font = (HFONT)SendMessage(WM_GETFONT);
        if (!m_font.IsNull())
        {
            LOGFONT lf;
            if (m_font.GetLogFont(lf))
            {
                lf.lfWeight = FW_BOLD;
                m_font.Detach();
                m_font.CreateFontIndirect(&lf);
                SendDlgItemMessage(IDC_STATIC_ABOUT, WM_SETFONT, (WPARAM)(HFONT)m_font);
            }
        }

        if (!m_IsLang)
        {
            ::ShowWindow(GetDlgItem(IDC_CHECK_SPLASH), SW_HIDE);
            ::ShowWindow(GetDlgItem(IDC_STATIC_LANG), SW_HIDE);
            ::ShowWindow(GetDlgItem(IDC_COMBO_LANG), SW_HIDE);

            return TRUE;
        }
    
        TCHAR data[256];
        DWORD size = sizeof(data);
        
        CString str;
        CString strCurrentLang;
        CProfile cfg(_T(VR_PROFILE));

        cfg.Open(_T("common"));
        cfg.QueryValue(m_IsSplash, _T("ShowSplash"));
        cfg.QueryValue(data, _T("Language"), &size);

        strCurrentLang = data;
        size = sizeof(data);

        cfg.Open(_T("languages"));
        cfg.QueryValue(data, NULL, &size);  // get all Languages

        HWND hCtl = GetDlgItem(IDC_COMBO_LANG);
        ::SendMessage(hCtl, CB_RESETCONTENT, 0, 0); // Purge combo droplist
        LPTSTR ptr = data;
        for (int n,i=0;; i++)
        {
            TCHAR buf[32];
            str = ptr;

            if (!str.GetLength()) break;

            size = sizeof(buf);
            cfg.QueryValue(buf, ptr, &size);
            m_ll.Add(str);
            
            if (!strCurrentLang.CompareNoCase(ptr))
                m_LangNum = i;

            ptr += (str.GetLength() + 1);
            n = ::SendMessage(hCtl, CB_ADDSTRING, 0, (LPARAM)buf);
                ::SendMessage(hCtl, CB_SETITEMDATA, n, (LPARAM)i);
        }

        UINT logo;

        _Module.GetCpuCaps(CPU_VENDOR_LOGO, &logo);
        str.Format(_T("%d"), logo);

        size = sizeof(data);
        cfg.Open(_T("logo"));
        cfg.QueryValue(data, str, &size);
        m_strLogo = _T("logo\\");
        m_strLogo += data;

        DoDataExchange(DDX_LOAD);

        return TRUE;
    }

    LRESULT OnClose(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        DoDataExchange(DDX_SAVE);
        // really we'd select an index in lang array, not in combobox. 
        // the maybe differ due to c-box sorting option.
        m_LangNum = ::SendMessage(GetDlgItem(IDC_COMBO_LANG)
                                 , CB_GETITEMDATA, m_LangNum, 0);
        EndDialog(wID);
        return 0;
    }

public:
    bool m_IsLang;
    int  m_LangNum;
    int  m_IsSplash;
    CSimpleArray<CString> m_ll;

private:
    CAboutDlg() {}
    
    CFont m_font;
    CHyperLink m_Url;
    CString m_strLogo;
};

