
// demoBeforeLoginDlg.cpp : 实现文件
//

#include "stdafx.h"
#include <string>
#include "afxdialogex.h"
#include "demo.h"
#include "demoBeforeLoginDlg.h"
#include "HwmSdkAgent.h"
#include "demoCustomMessage.h"
#include "demoTools.h"
#include "demoLoginDlg.h"
#include "demoJoinConfByIdAnonymousDlg.h"
#include "demoSetProxyInfoDlg.h"
#include "demoSetServerAddressDlg.h"
#include "demoJoinConfByLinkDlg.h"
#include "demoSettingDlg.h"
#include "demoConfCtrlDlg.h"
#include "demoLoginByAppIdDlg.h"
#include "demoInitDlg.h"
#include "demoLoginBySSO.h"
#include "demoSetNetworkModeDlg.h"
#include "demoSetGuaranteedIpAddressesDlg.h"
#include "demoSetCAVerifyInfoDlg.h"
#include "demoSetGlobalGuaranteedIpAddressesDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace std;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
    CAboutDlg();
    ~CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_ABOUTBOX };
#endif

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

CAboutDlg::~CAboutDlg()
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CdemoBeforeLoginDlg 对话框



CdemoBeforeLoginDlg::CdemoBeforeLoginDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(IDD_BEFORE_LOGIN_DIALOG, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDI_MAINFRAME);
}

CdemoBeforeLoginDlg::~CdemoBeforeLoginDlg()
{
}

void CdemoBeforeLoginDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);

    DDX_Control(pDX, IDC_EDIT_OUTPUT, m_editOutput);
}

BEGIN_MESSAGE_MAP(CdemoBeforeLoginDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()

    ON_MESSAGE(WM_LOGIN_RESULT, &CdemoBeforeLoginDlg::loginCallBack)

    ON_BN_CLICKED(IDC_BUTTON_INIT, &CdemoBeforeLoginDlg::OnBnClickedButtonInit)
    ON_BN_CLICKED(IDC_BUTTON_JUMP_LOGIN_DLG, &CdemoBeforeLoginDlg::OnBnClickedButtonJumpLoginDlg)
    ON_BN_CLICKED(IDC_BUTTON_JOIN_MEETING_ANONYMOUS, &CdemoBeforeLoginDlg::OnBnClickedJoinConfAnonymous)

    ON_BN_CLICKED(IDC_BUTTON_GO_PROXY, &CdemoBeforeLoginDlg::OnBnClickedButtonGoProxy)
    ON_BN_CLICKED(IDC_BUTTON_GO_NETWORK_MODE, &CdemoBeforeLoginDlg::OnBnClickedButtonGoNetworkMode)
    ON_BN_CLICKED(IDC_BUTTON_EXIT, &CdemoBeforeLoginDlg::OnBnClickedButtonExit)
    ON_BN_CLICKED(IDC_BUTTON_SET_SERVER, &CdemoBeforeLoginDlg::OnBnClickedButtonSetServer)
    ON_BN_CLICKED(IDC_BUTTON_JOIN_MEETING_BY_LINK, &CdemoBeforeLoginDlg::OnBnClickedJoinConfByLink)
    ON_BN_CLICKED(IDC_BUTTON_SEETING, &CdemoBeforeLoginDlg::OnBnClickedButtonSeeting)
    ON_BN_CLICKED(IDC_BUTTON_CONF_CTRL, &CdemoBeforeLoginDlg::OnBnClickedButtonConfCtrl)
    ON_BN_CLICKED(IDC_BUTTON_GO_LOGIN_BY_APPID, &CdemoBeforeLoginDlg::OnBnClickedButtonGoLoginByAppid)
    ON_BN_CLICKED(IDC_GET_USER_STATE, &CdemoBeforeLoginDlg::OnBnClickedGetUserState)
    ON_BN_CLICKED(IDC_BUTTON_LOGIN_BY_SSO, &CdemoBeforeLoginDlg::OnBnClickedButtonLoginBySso)
    ON_BN_CLICKED(IDC_BUTTON_SET_GUARANTEEDIP, &CdemoBeforeLoginDlg::OnBnClickedButtonSetGuaranteedIp)
    ON_BN_CLICKED(IDC_SET_CA_VERIFY_INFO, &CdemoBeforeLoginDlg::OnBnClickedSetCaVerifyInfo)
    ON_BN_CLICKED(IDC_BUTTON_SET_GLOBAL_GUARANTEEDIP, &CdemoBeforeLoginDlg::OnBnClickedButtonSetGlobalGuaranteedIp)
    ON_BN_CLICKED(IDC_GET_LOGIN_STATE, &CdemoBeforeLoginDlg::OnBnClickedGetLoginState)
END_MESSAGE_MAP()


// CdemoBeforeLoginDlg 消息处理程序

BOOL CdemoBeforeLoginDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // 将“关于...”菜单项添加到系统菜单中。

    // IDM_ABOUTBOX 必须在系统命令范围内。
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
    //  执行此操作
    SetIcon(m_hIcon, TRUE);            // 设置大图标
    SetIcon(m_hIcon, FALSE);        // 设置小图标
    // 显示历史输出
    UpdateOutput();
    return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CdemoBeforeLoginDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialogEx::OnSysCommand(nID, lParam);
    }
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CdemoBeforeLoginDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 用于绘制的设备上下文

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // 使图标在工作区矩形中居中
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // 绘制图标
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CdemoBeforeLoginDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}

/**
* 屏蔽快捷键
*/
BOOL CdemoBeforeLoginDlg::PreTranslateMessage(MSG* pMsg)
{
    //屏蔽ESC关闭窗体/
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
    {
        return TRUE;
    }
    //屏蔽回车关闭窗体
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN && pMsg->wParam)
    {
        return TRUE;
    }
    else
    {
        return CDialog::PreTranslateMessage(pMsg);
    }
}

/**
* 登录成功回调接口
*/
afx_msg LRESULT CdemoBeforeLoginDlg::loginCallBack(WPARAM wParam, LPARAM lParam)
{
    if (wParam == hwmsdk::HWM_COMMON_SUCCESS)
    {
        OnOK();
    }

    // 服务器降级，接入方收到该错误码可以继续调用入会接口离线入会
    if (wParam == hwmsdk::HWM_LOGIN_E_ERR_MNG_FAILED)
    {
        OnOK();
    }

    return 0L;
}

/**
* 打开初始化窗口
*/
void CdemoBeforeLoginDlg::OnBnClickedButtonInit()
{
    demoInitDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}

/**
*打开登录窗口
*/
void CdemoBeforeLoginDlg::OnBnClickedButtonJumpLoginDlg()
{
    //登录窗口
    demoLoginDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}

/**
*打开匿名入会窗口
*/
void CdemoBeforeLoginDlg::OnBnClickedJoinConfAnonymous()
{
    //匿名入会窗口
    demoJoinConfByIdAnonymousDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}

/**
*打开设置代理信息窗口
*/
void CdemoBeforeLoginDlg::OnBnClickedButtonGoProxy()
{
    //匿名入会窗口
    demoSetProxyInfoDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}

void CdemoBeforeLoginDlg::OnBnClickedButtonGoNetworkMode()
{
    // 设置网络模式 
    demoSetNetworkModeDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}

/**
* 退出sdk
*/
void CdemoBeforeLoginDlg::OnBnClickedButtonExit()
{
    int ret = hwmsdkagent::Exit();
    if (hwmsdk::HWM_COMMON_SUCCESS != ret)
    {
        CTools::OutputRetStr("Exit error");
    }
}

/**
* 打开设置服务器信息窗口
*/
void CdemoBeforeLoginDlg::OnBnClickedButtonSetServer()
{
    demoSetServerAddressDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}

/**
* 打开链接入会窗口
*/
void CdemoBeforeLoginDlg::OnBnClickedJoinConfByLink()
{
    demoJoinConfByLinkDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}

/**
* 打开设置窗口
*/
void CdemoBeforeLoginDlg::OnBnClickedButtonSeeting()
{
    demoSettingDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}

/**
* 打开会议控制窗口
*/
void CdemoBeforeLoginDlg::OnBnClickedButtonConfCtrl()
{
    demoConfCtrlDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}


void CdemoBeforeLoginDlg::OnBnClickedButtonGoLoginByAppid()
{
    demoLoginByAppIdDlg dlg;
    INT_PTR nResponse = dlg.DoModal();
}

void CdemoBeforeLoginDlg::UpdateOutput()
{
    // 显示
    if (IsWindow(m_hWnd) && !CStaticData::GetOutput().empty()) {
        m_editOutput.SetWindowTextW(CTools::UTF2UNICODE(CStaticData::GetOutput()).c_str());
        m_editOutput.SendMessage(WM_VSCROLL, SB_BOTTOM, 0);  // 滚动条到底部
    }
}


void CdemoBeforeLoginDlg::OnBnClickedGetUserState()
{
    int ret = hwmsdkagent::GetUserState();
    if (hwmsdk::HWM_COMMON_SUCCESS != ret)
    {
        CTools::OutputRetStr("GetUserState error");
    }
}

// 打开SSO登录窗口
void CdemoBeforeLoginDlg::OnBnClickedButtonLoginBySso()
{
    demoLoginBySSO loginBySSODlg;
    INT_PTR nResponse = loginBySSODlg.DoModal();
}


void CdemoBeforeLoginDlg::OnBnClickedButtonSetGuaranteedIp()
{
    demoSetGuaranteedIpAddressesDlg setGuaranteedIpAddressesDlg;
    INT_PTR nResponse = setGuaranteedIpAddressesDlg.DoModal();
}


void CdemoBeforeLoginDlg::OnBnClickedSetCaVerifyInfo()
{
    demoSetCAVerifyInfoDlg setCAVerifyInfoDlg;
    INT_PTR nResponse = setCAVerifyInfoDlg.DoModal();
}

void CdemoBeforeLoginDlg::OnBnClickedButtonSetGlobalGuaranteedIp()
{
    demoSetGlobalGuaranteedIpAddressesDlg setGlobalGuaranteedIpAddressesDlg;
    INT_PTR nResponse = setGlobalGuaranteedIpAddressesDlg.DoModal();
}

void CdemoBeforeLoginDlg::OnBnClickedGetLoginState()
{
    int ret = hwmsdkagent::GetLoginState();
    if (hwmsdk::HWM_COMMON_SUCCESS != ret)
    {
        CTools::OutputRetStr("GetLoginState error");
    }
}
