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

#include "pch.h"
#include "TCL Copy Tool.h"
#include "ChassisConfigDlg.h"
#include "afxdialogex.h"

// CChassisConfigDlg 对话框

IMPLEMENT_DYNAMIC(CChassisConfigDlg, CDialogEx)

CChassisConfigDlg::CChassisConfigDlg(CWnd *pParent /*=nullptr*/)
	: CDialogEx(IDD_CONFIG_DIALOG, pParent)
{
}

CChassisConfigDlg::~CChassisConfigDlg()
{
}

void CChassisConfigDlg::DoDataExchange(CDataExchange *pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_CHASSIS, m_cb_chassis);
	DDX_Control(pDX, IDC_CHECK_MTK_INIT, m_check_mtk_init);
	DDX_Control(pDX, IDC_CHECK_WB_INIT, m_check_wb_init);
	DDX_Control(pDX, IDC_CHECK_WRITE_PID, m_check_pid);
	DDX_Control(pDX, IDC_CHECK_COPY_DID, m_check_did);
	DDX_Control(pDX, IDC_CHECK_COPY_MAC, m_check_mac);
	DDX_Control(pDX, IDC_CHECK_COPY_HDCP, m_check_hdcp);
	DDX_Control(pDX, IDC_CHECK_COPY_ESN, m_check_esn);
	DDX_Control(pDX, IDC_CHECK_COPY_WIDI, m_check_widi);
	DDX_Control(pDX, IDC_CHECK_COPY_WIDEVINE, m_check_widevine);
	DDX_Control(pDX, IDC_CHECK_COPY_HDCP2, m_check_hdcp2);
	DDX_Control(pDX, IDC_CHECK_COPY_CIKEY, m_check_cikey);
	DDX_Control(pDX, IDC_CHECK_COPY_CHANNEL, m_check_channel);
	DDX_Control(pDX, IDC_CHECK_COPY_OSDLANG, m_check_osd_lang);
	DDX_Control(pDX, IDC_CHECK_COPY_SHOPLANG, m_check_shop_lang);
	DDX_Control(pDX, IDC_CHECK_COPY_WB, m_check_wb);
}

BEGIN_MESSAGE_MAP(CChassisConfigDlg, CDialogEx)
ON_CBN_SELCHANGE(IDC_COMBO_CHASSIS, &CChassisConfigDlg::OnCbnSelchangeComboChassis)
ON_BN_CLICKED(BTN_ADD_CHASSIS, &CChassisConfigDlg::OnBnClickedAddChassis)
END_MESSAGE_MAP()

// CChassisConfigDlg 消息处理程序

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

	SetDlgItemInt(EDIT_WARN_COUNT, 200);
	// TODO:  在此添加额外的初始化
	CWnd *pWnd = GetDlgItem(EDIT_CHASSIS_NAME);
	if (Global::g_tConfig.mode < 2)
	{ // 显示下拉框;
		CRect rcWnd;
		pWnd->ShowWindow(SW_HIDE);
		pWnd->GetWindowRect(rcWnd);
		ScreenToClient(rcWnd);
		m_cb_chassis.MoveWindow(rcWnd);
		m_cb_chassis.ShowWindow(SW_SHOW);
		GetDlgItem(BTN_ADD_CHASSIS)->ShowWindow(SW_SHOW);
		for (auto it : Global::g_tConfig.chassislist)
		{
			m_cb_chassis.AddString(it.first.c_str());
		}
#if SAVE_AUTO_ONLINE
		if (!IsChassisExsit(Global::g_AutoOnlineChassis.name))
			m_cb_chassis.AddString(Global::g_AutoOnlineChassis.name.c_str());
		m_cb_chassis.SelectString(0, Global::g_AutoOnlineChassis.name.c_str());
#else
		m_cb_chassis.SelectString(0, Global::g_tConfig.chassis.c_str());
#endif
	}
	
#if SAVE_AUTO_ONLINE
	if ( Global::g_tConfig.mode < 2 )
		FillData2Wnd(Global::g_AutoOnlineChassis);
	else if (Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis.c_str()) != Global::g_tConfig.chassislist.end())
		FillData2Wnd(Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis.c_str())->second);
#else
	if (Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis.c_str()) != Global::g_tConfig.chassislist.end())
	{
		FillData2Wnd(Global::g_tConfig.chassislist.find(Global::g_tConfig.chassis.c_str())->second);
	}
#endif

	return TRUE; // return TRUE unless you set the focus to a control
				 // 异常: OCX 属性页应返回 FALSE
}

void CChassisConfigDlg::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类
	CString strChassis;
	if (GetDlgItem(BTN_ADD_CHASSIS)->IsWindowVisible())
	{
		int nCurSel = m_cb_chassis.GetCurSel();
		if (nCurSel != CB_ERR)
		{
			m_cb_chassis.GetLBText(nCurSel, strChassis);
		}
	}
	else
	{
		GetDlgItemText(EDIT_CHASSIS_NAME, strChassis);
	}

	if ( strChassis.IsEmpty() )
	{
		MessageBox(_T("Chassis name can't empty!"), _T("Warnnig"), MB_OK | MB_ICONERROR);
		return;
	}

	SaveChange2Config();
	CDialogEx::OnOK();
}

void CChassisConfigDlg::FillData2Wnd(Global::TChassis &chassis)
{
#if SAVE_AUTO_ONLINE
	if (Global::g_tConfig.mode < 2)
	{
		SetDlgItemText(EDIT_CHASSIS_NAME, Global::g_AutoOnlineChassis.name.c_str());
		SetDlgItemText(EDIT_URL, Global::g_AutoOnlineChassis.url.c_str());
	}
	else
	{
		SetDlgItemText(EDIT_CHASSIS_NAME, Global::g_tConfig.chassis.c_str());
		SetDlgItemText(EDIT_URL, Global::g_tConfig.serverurl.c_str());
	}
#else
	SetDlgItemText(EDIT_CHASSIS_NAME, Global::g_tConfig.chassis.c_str());
	SetDlgItemText(EDIT_URL, Global::g_tConfig.serverurl.c_str());
#endif
	SetDlgItemInt(EDIT_WARN_COUNT, Global::g_tConfig.warncount);
	// check box
	m_check_mtk_init.SetCheck(chassis.IsMTKInit);
	m_check_wb_init.SetCheck(chassis.IsWBInit);
	m_check_pid.SetCheck(chassis.IsWritePID);
	m_check_did.SetCheck(chassis.IsCopyDID);
	m_check_mac.SetCheck(chassis.IsCopyMAC);
	m_check_hdcp.SetCheck(chassis.IsCopyKEY);
	m_check_esn.SetCheck(chassis.IsCopyESN);
	m_check_widi.SetCheck(chassis.IsCopyWiDi);
	m_check_widevine.SetCheck(chassis.IsCopyWidevine);
	m_check_hdcp2.SetCheck(chassis.IsCopyKEY2_2);
	m_check_cikey.SetCheck(chassis.IsCopyCikey);
	m_check_channel.SetCheck(chassis.IsCopyChannel);
	m_check_osd_lang.SetCheck(chassis.IsOsdLanguage);
	m_check_shop_lang.SetCheck(chassis.IsShopLanguage);
	m_check_wb.SetCheck(chassis.IsCopyWB);

	SetDlgItemText(IDC_EDIT_WBFILE, chassis.WBFile.c_str());
	SetDlgItemText(IDC_EDIT_CHANNEL, chassis.Channel.c_str());
	SetDlgItemText(IDC_EDIT_OSD_LANG, chassis.OsdLanguage.c_str());
	SetDlgItemText(IDC_EDIT_SHOP_LANG, chassis.ShopLanguage.c_str());
	SetDlgItemInt(IDC_EDIT_PID, chassis.ProjectID);
	SetDlgItemText(IDC_EDIT_DID_TYPE, chassis.ClientType.c_str());
	SetDlgItemText(IDC_EDIT_MAC_TYPE, chassis.MACType.c_str());
	SetDlgItemText(IDC_EDIT_HDCP_TYPE, chassis.HDCPKeyType.c_str());
	SetDlgItemText(IDC_EDIT_HDCP2_TYPE, chassis.HDCPKey22Type.c_str());
	SetDlgItemText(IDC_EDIT_WIDI_TYPE, chassis.WiDiType.c_str());
	SetDlgItemText(IDC_EDIT_WIDEVINE_TYPE, chassis.WidevineType.c_str());
	SetDlgItemText(IDC_EDIT_ESN_TYPE, chassis.ESNType.c_str());
	SetDlgItemText(IDC_EDIT_CIKEY_TYPE, chassis.CIKeyType.c_str());

	SetDlgItemInt(EDIT_DELAY, chassis.Delay);
	SetDlgItemText(EDIT_CHECKSTRING, chassis.CheckString.c_str());
}

void CChassisConfigDlg::SaveChange2Config()
{
	UpdateData();
	CString strChassis = _T(""), strURL = _T("");
	GetDlgItemText(EDIT_URL, strURL);
	int WarnCount = GetDlgItemInt(EDIT_WARN_COUNT);
	if (GetDlgItem(BTN_ADD_CHASSIS)->IsWindowVisible())
	{
		int nCurSel = m_cb_chassis.GetCurSel();
		if (nCurSel != CB_ERR)
		{
			m_cb_chassis.GetLBText(nCurSel, strChassis);
		}
	}
	else
	{
		GetDlgItemText(EDIT_CHASSIS_NAME, strChassis);
	}
	Global::g_tConfig.warncount = WarnCount;
	Global::g_tConfig.serverurl = strURL.GetString();

	Global::TChassis tchassis;
	// check box
	tchassis.IsWBInit = m_check_wb_init.GetCheck();
	tchassis.IsWritePID = m_check_pid.GetCheck();
	tchassis.IsCopyDID = m_check_did.GetCheck();
	tchassis.IsCopyMAC = m_check_mac.GetCheck();
	tchassis.IsCopyKEY = m_check_hdcp.GetCheck();
	tchassis.IsCopyESN = m_check_esn.GetCheck();
	tchassis.IsCopyWiDi = m_check_widi.GetCheck();
	tchassis.IsCopyWidevine = m_check_widevine.GetCheck();
	tchassis.IsCopyKEY2_2 = m_check_hdcp2.GetCheck();
	tchassis.IsCopyCikey = m_check_cikey.GetCheck();
	tchassis.IsCopyChannel = m_check_channel.GetCheck();
	tchassis.IsOsdLanguage = m_check_osd_lang.GetCheck();
	tchassis.IsShopLanguage = m_check_shop_lang.GetCheck();
	tchassis.IsCopyWB = m_check_wb.GetCheck();
	tchassis.IsMTKInit = m_check_mtk_init.GetCheck();

	CString strData = _T("");
	GetDlgItemText(IDC_EDIT_WBFILE, strData);
	tchassis.WBFile = strData.GetString();
	GetDlgItemText(IDC_EDIT_CHANNEL, strData);
	tchassis.Channel = strData.GetString();
	GetDlgItemText(IDC_EDIT_OSD_LANG, strData);
	tchassis.OsdLanguage = strData.GetString();
	GetDlgItemText(IDC_EDIT_SHOP_LANG, strData);
	tchassis.ShopLanguage = strData.GetString();
	tchassis.ProjectID = GetDlgItemInt(IDC_EDIT_PID);
	GetDlgItemText(IDC_EDIT_DID_TYPE, strData);
	tchassis.ClientType = strData.GetString();
	GetDlgItemText(IDC_EDIT_MAC_TYPE, strData);
	tchassis.MACType = strData.GetString();
	GetDlgItemText(IDC_EDIT_HDCP_TYPE, strData);
	tchassis.HDCPKeyType = strData.GetString();
	GetDlgItemText(IDC_EDIT_HDCP2_TYPE, strData);
	tchassis.HDCPKey22Type = strData.GetString();
	GetDlgItemText(IDC_EDIT_WIDI_TYPE, strData);
	tchassis.WiDiType = strData.GetString();
	GetDlgItemText(IDC_EDIT_WIDEVINE_TYPE, strData);
	tchassis.WidevineType = strData.GetString();
	GetDlgItemText(IDC_EDIT_ESN_TYPE, strData);
	tchassis.ESNType = strData.GetString();
	GetDlgItemText(IDC_EDIT_CIKEY_TYPE, strData);
	tchassis.CIKeyType = strData.GetString();

	tchassis.Delay = GetDlgItemInt(EDIT_DELAY);
	GetDlgItemText(EDIT_CHECKSTRING, strData);
	tchassis.CheckString = strData.GetString();

	// 此Chassis是否存在于列表中;
	std::map<std::string, Global::TChassis>::iterator it = Global::g_tConfig.chassislist.find(strChassis.GetString());
	if (it != Global::g_tConfig.chassislist.end())
	{
		it->second = tchassis;
	}
	else
	{
		Global::g_tConfig.chassislist.insert(pair<std::string, Global::TChassis>(strChassis.GetString(), tchassis));
	}

	Global::SetConfig();
}

BOOL CChassisConfigDlg::IsChassisExsit(std::string chassis)
{
	BOOL bExsit = FALSE;
	CString strChassis;
	int nCount = m_cb_chassis.GetCount();
	for ( int i= 0; i < nCount; i++ )
	{
		m_cb_chassis.GetLBText(i, strChassis);
		if ( _tcsicmp(chassis.c_str(), strChassis.GetString() ) == 0 )
		{
			bExsit = TRUE;
			break;
		}
	}

	return bExsit;
}

void CChassisConfigDlg::OnCbnSelchangeComboChassis()
{
	// TODO: 在此添加控件通知处理程序代码
	int nCurSel = m_cb_chassis.GetCurSel();
	if (nCurSel != CB_ERR)
	{
		CString strChassis;
		m_cb_chassis.GetLBText(nCurSel, strChassis);
		FillData2Wnd(Global::g_tConfig.chassislist.find(strChassis.GetString())->second);
	}
}

void CChassisConfigDlg::OnBnClickedAddChassis()
{
	// TODO: 在此添加控件通知处理程序代码
	if (Global::g_tConfig.mode < 2)
	{
		// 隐藏combobox;
		int nCurSel = m_cb_chassis.GetCurSel();
		if (nCurSel != CB_ERR)
		{
			CString strChassis;
			m_cb_chassis.GetLBText(nCurSel, strChassis);
			SetDlgItemText(EDIT_CHASSIS_NAME, strChassis);
		}
		m_cb_chassis.ShowWindow(SW_HIDE);
		GetDlgItem(EDIT_CHASSIS_NAME)->ShowWindow(SW_SHOW);
		GetDlgItem(BTN_ADD_CHASSIS)->ShowWindow(SW_HIDE);
	}
}
