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

#include "stdafx.h"
#include "demo.h"
#include "demoInitDlg.h"
#include "afxdialogex.h"
#include "HwmSdkAgent.h"
#include "demoTools.h"
#include "demoNotifyProc.h"
#include "demoCallBackProc.h"

// demoInitDlg 对话框

IMPLEMENT_DYNAMIC(demoInitDlg, CDialogEx)

demoInitDlg::demoInitDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_INIT_DIALOG, pParent)
    , m_radioAuthType(0)
    , m_isMultiAppLoginSupported(FALSE)
{

}

demoInitDlg::~demoInitDlg()
{
}

BOOL demoInitDlg::OnInitDialog()
{
    BOOL ret = CDialogEx::OnInitDialog();

    m_comboSiteType.InsertString(0, _T("中国站点"));
    m_comboSiteType.InsertString(1, _T("国际站点"));
    m_comboSiteType.SetCurSel(0);

    m_comboLogLevel.InsertString(0, _T("INFO"));
    m_comboLogLevel.InsertString(1, _T("WARNING"));
    m_comboLogLevel.InsertString(2, _T("ERROR"));
    m_comboLogLevel.SetCurSel(0);

    CString defaultAppId = _T("");
    CTools::getIniConfigParam(_T("LoginConfig"), _T("defaultAppId"), defaultAppId);
    m_editAppId.SetWindowText(defaultAppId);
    m_editExeName.SetWindowText(_T("HwmSdk"));
    m_logKeepDays.SetWindowText(_T("0"));
    m_appVersion.SetWindowText(_T(""));

    return ret;
}

void demoInitDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT_APP_ID, m_editAppId);
    DDX_Radio(pDX, IDC_RADIO_AUTH_TYPE_ACCOUNT, m_radioAuthType);
    DDX_Control(pDX, IDC_COMBO_SITE_TYPE, m_comboSiteType);
    DDX_Control(pDX, IDC_COMBO_LOG_LEVEL, m_comboLogLevel);
    DDX_Control(pDX, IDC_EDIT_EXE_NAME, m_editExeName);
    DDX_Control(pDX, IDC_EDIT_LOG_KEEP_DAYS_ID, m_logKeepDays);
    DDX_Control(pDX, IDC_EDIT_PREFERRED_CAMERA, m_preferredCamera);
    DDX_Control(pDX, IDC_EDIT_APP_VERSION, m_appVersion);
    DDX_Check(pDX, IDC_CHECK1, m_isMultiAppLoginSupported);
}

BEGIN_MESSAGE_MAP(demoInitDlg, CDialogEx)
    ON_BN_CLICKED(IDC_BUTTON_INIT_OK, &demoInitDlg::OnBnClickedButtonOk)
    ON_BN_CLICKED(IDC_RADIO_AUTH_TYPE_ACCOUNT, &demoInitDlg::OnBnClickedRadioAuthTypeAccount)
    ON_BN_CLICKED(IDC_RADIO_AUTH_TYPE_APPID, &demoInitDlg::OnBnClickedRadioAuthTypeAppid)
END_MESSAGE_MAP()

static demoNotifyProc *notifyObj = new(std::nothrow) demoNotifyProc();
//设置回调接口
static demoCallbackProc *callbackObj = new(std::nothrow) demoCallbackProc();

/**
*初始化接口，拉起应用
*/
int demoInitDlg::Init()
{ 
    //组装入参结构体
    hwmsdkagent::HwmInitInfo initParam;
    memset(&initParam, 0, sizeof(hwmsdkagent::HwmInitInfo));

    //获取sdk路径
    TCHAR szFilePath[HWM_MAX_FILE_PATH_LEN] = { 0 };
    GetModuleFileName(NULL, szFilePath, HWM_MAX_FILE_PATH_LEN);
    if (_tcsrchr(szFilePath, _T('\\')) != NULL)
    {
        (_tcsrchr(szFilePath, _T('\\')))[1] = 0; // 删除文件名，只获得路径字串
    }
    string temp = CTools::UNICODE2UTF(szFilePath) + "\\HwmSdk\\";
    // 对exe文件重命名后，根据自定义的exe文件名拼接exe路径(UTF8格式)
    CString editExeNameCstr;
    m_editExeName.GetWindowText(editExeNameCstr);
    string exeName = CTools::UNICODE2UTF(editExeNameCstr.GetString());
    temp = temp + exeName + ".exe";
    errno_t err = strcpy_s(initParam.exePath, HWM_MAX_FILE_PATH_LEN, temp.c_str());
    // 指定日志路径与用户数据路径，若不指定将在appdata下创建默认路径
    string tempLogPath = CTools::UNICODE2UTF(szFilePath) + "\\log\\";
    err += strcpy_s(initParam.logPath, HWM_MAX_FILE_PATH_LEN, tempLogPath.c_str());
    string tempUserDataPath = tempLogPath + "\\UserData\\";
    err += strcpy_s(initParam.userDataPath, HWM_MAX_FILE_PATH_LEN, tempUserDataPath.c_str());
    //解析AppId数据(UTF8格式)
    CString editAppIdCstr;
    m_editAppId.GetWindowText(editAppIdCstr);
    string appId = CTools::UNICODE2UTF(editAppIdCstr.GetString());
    //保存AppId信息用于AppId登录
    CTools::writeIniConfigParam(_T("LoginConfig"), _T("appId"), editAppIdCstr);
    err += strcpy_s(initParam.appId, HWM_MAX_APPID_LEN, appId.c_str());
    //保存优选摄像头列表参数用于初次选择摄像头
    CString editpreferredCameraCstr;
    m_preferredCamera.GetWindowText(editpreferredCameraCstr);
    string preferredCamera = CTools::UNICODE2UTF(editpreferredCameraCstr.GetString());
    err += strcpy_s(initParam.preferredCamera, HWM_MAX_PREFERRED_CAMERA_LEN, preferredCamera.c_str());
    // 应用版本号
    CString cAppVersion;
    m_appVersion.GetWindowText(cAppVersion);
    string appVersion = CTools::UNICODE2UTF(cAppVersion.GetString());
    err += strcpy_s(initParam.appVersion, HWM_MAX_APPVERSION_LEN, appVersion.c_str());
    if (err != S_OK)
    {
        AfxMessageBox(_T("String Copy Error!"));
    }
    //设置鉴权类型
    initParam.authType = (hwmsdkagent::HwmAuthType)m_radioAuthType;
    //设置回调与通知对象
    initParam.notify = notifyObj;
    initParam.callback = callbackObj;
    // 登录站点设置
    initParam.siteType = SITE_TYPE_CHINA;
    if (m_comboSiteType.GetCurSel() == 1)
    {
        initParam.siteType = SITE_TYPE_AP;
    }

    // 日志级别
    initParam.logLevel = hwmsdkagent::HwmLogLevel::HWM_LOG_LEVEL_INFO;
    if (m_comboLogLevel.GetCurSel() == 1)
    {
        initParam.logLevel = hwmsdkagent::HwmLogLevel::HWM_LOG_LEVEL_WARNING;
    }
    else if (m_comboLogLevel.GetCurSel() == 2)
    {
        initParam.logLevel = hwmsdkagent::HwmLogLevel::HWM_LOG_LEVEL_ERROR;
    }

    // 日志最大保存天数
    CString keepDays;
    m_logKeepDays.GetWindowText(keepDays);
    initParam.logKeepDays = static_cast<unsigned int>(_ttoi(keepDays));
    initParam.isSupportMultiAppLogin = m_isMultiAppLoginSupported;

    int ret = hwmsdkagent::Init(&initParam);
    return ret;
}


void demoInitDlg::OnBnClickedButtonOk()
{
    UpdateData(1);
    int ret = Init();
    if (hwmsdk::HWM_COMMON_SUCCESS != ret) {
        CTools::OutputRetStr("Init error");
    }
    else {
        OnOK();
    }
}

void demoInitDlg::OnBnClickedRadioAuthTypeAccount()
{
    m_radioAuthType = 0;
}

void demoInitDlg::OnBnClickedRadioAuthTypeAppid()
{
    m_radioAuthType = 1;
}
