#include "StdAfx.h"
#include "lyfzLoadLibrary.h"

namespace lyfzLibrary
{
	CString g_strCloundHost = _T("http://121.42.170.207"); // �Ʒ�����������ַ;
	HMODULE g_hCloudSelectionLib = NULL;

	API_uploadorder g_uploadorder = NULL;
	API_uploadgoodinfo g_uploadgoodinfo = NULL;
	API_uploadimg g_uploadimg = NULL;
	API_getcloundresult g_getcloundresult = NULL;

	BOOL LoadCloudSelectionLib()
	{
		if ( g_hCloudSelectionLib == NULL )
			g_hCloudSelectionLib = LoadLibrary(_T("lyfzCloudSelection.dll"));

		if ( !g_hCloudSelectionLib )
		{
			//AfxMessageBox(_T("�������ӿ�ʧ��"));
			return FALSE;
		}

		g_uploadorder = (API_uploadorder)GetProcAddress(g_hCloudSelectionLib, "uploadorder");
		if ( g_uploadorder == NULL )
		{
			//AfxMessageBox(_T("��ȡ�����ƽӿ�ʧ��"));
			return FALSE;
		}

		g_uploadgoodinfo = (API_uploadgoodinfo)GetProcAddress(g_hCloudSelectionLib, "uploadgoodinfo");
		if ( g_uploadgoodinfo == NULL )
		{
			//AfxMessageBox(_T("��ȡ�����ƽӿ�ʧ��"));
			return FALSE;
		}

		g_uploadimg = (API_uploadimg)GetProcAddress(g_hCloudSelectionLib, "uploadImg");
		if ( g_uploadimg == NULL )
		{
			//AfxMessageBox(_T("��ȡ�����ƽӿ�ʧ��"));
			return FALSE;
		}

		g_getcloundresult = (API_getcloundresult)GetProcAddress(g_hCloudSelectionLib, "getresult");
		if ( g_getcloundresult == NULL )
		{
			return FALSE;
		}
		
		return TRUE;
	}

	void FreeCloudSelectionLib()
	{
		if ( g_hCloudSelectionLib )
		{
			if ( FreeLibrary(g_hCloudSelectionLib) )
			{
				g_hCloudSelectionLib = NULL;	
				g_uploadorder = NULL;
				g_uploadgoodinfo = NULL;
				g_uploadimg = NULL;
				g_getcloundresult = NULL;
			}
		}
	}

	void getXPJsonresult(IN CString strJson, IN CArray<CStringArray,CStringArray>& AryResult)
	{
		INT i = 0;
		INT nIndex = 0;
		INT nCount = 0;
		CString strTemp = _T("");
		CString strTemp2 = _T("");
		CString strMark = _T("{\"photo_list\": ");
		nCount = strJson.Replace(strMark, strMark);
		strJson += _T(", {\"photo_list\": ");
		AryResult.SetSize(nCount,1);

		strJson = strJson.Mid(strMark.GetLength());
		while ( strJson.Find(strMark) != -1 )
		{
			nIndex = strJson.Find(strMark);
			strTemp = strJson.Left(nIndex - 3);
			strJson = strJson.Mid(nIndex + strMark.GetLength());

			// ������Json����;
			AryResult.ElementAt(i).RemoveAll();
			// ��Ƭ��;
			nIndex = strTemp.Find(_T(", "));
			if ( nIndex != -1)
			{
				strTemp2 = strTemp.Left(nIndex);
				strTemp2.TrimLeft(_T("\""));
				strTemp2.TrimRight(_T("\""));
				AryResult.ElementAt(i).Add(strTemp2);
				strTemp = strTemp.Mid(nIndex + 2);
			}

			// ��Ʒ����;
			nIndex = strTemp.Find(_T("\"num\": "));
			if ( nIndex != -1)
			{
				strTemp = strTemp.Mid(nIndex + 7);
				nIndex = strTemp.Find(_T(", \"product_id\": "));
				if ( nIndex != -1)
				{
					strTemp2 = strTemp.Left(nIndex);
					AryResult.ElementAt(i).Add(strTemp2);
					strTemp = strTemp.Mid(nIndex + strlen(_T(", \"product_id\": ")));
				}
			}

			// ��ƷID;
			nIndex = strTemp.Find(_T(", \"name\": "));
			if ( nIndex != -1)
			{
				strTemp2 = strTemp.Left(nIndex);
				strTemp2.TrimLeft(_T("\""));
				strTemp2.TrimRight(_T("\""));
				AryResult.ElementAt(i).Add(strTemp2);
				strTemp = strTemp.Mid(nIndex + _tcslen(_T(", \"name\": ")));
			}

			// ��Ʒ����;
			if ( strTemp.GetLength() )
			{
				strTemp.TrimLeft(_T("\""));
				strTemp.TrimRight(_T("\""));
				AryResult.ElementAt(i).Add(strTemp);
			}
			i++;
		}
	}

	void getXPJsonresult(IN CString strJson, IN vector<STSpJson>& vtSpJson)
	{
		INT i = 0;
		INT nIndex = 0;
		INT nCount = 0;
		CString strTemp = _T("");
		CString strTemp2 = _T("");
		CString strMark = _T("{\"photo_list\": ");
		nCount = strJson.Replace(strMark, strMark);
		strJson += _T(", {\"photo_list\": ");

		strJson = strJson.Mid(strMark.GetLength());
		while ( strJson.Find(strMark) != -1 )
		{
			STSpJson tagJson;
			nIndex = strJson.Find(strMark);
			strTemp = strJson.Left(nIndex - 3);
			strJson = strJson.Mid(nIndex + strMark.GetLength());

			// ������Json����;
			// ��Ƭ��;
			nIndex = strTemp.Find(_T(", "));
			if ( nIndex != -1)
			{
				strTemp2 = strTemp.Left(nIndex);
				strTemp2.TrimLeft(_T("\""));
				strTemp2.TrimRight(_T("\""));
				//AryResult.ElementAt(i).Add(strTemp2);
				tagJson.strImgs = strTemp2;
				strTemp = strTemp.Mid(nIndex + 2);
			}

			// ��Ʒ����;
			nIndex = strTemp.Find(_T("\"num\": "));
			if ( nIndex != -1)
			{
				strTemp = strTemp.Mid(nIndex + 7);
				nIndex = strTemp.Find(_T(", \"product_id\": "));
				if ( nIndex != -1)
				{
					strTemp2 = strTemp.Left(nIndex);
					//AryResult.ElementAt(i).Add(strTemp2);
					tagJson.strSpCount = strTemp2;
					strTemp = strTemp.Mid(nIndex + strlen(_T(", \"product_id\": ")));
				}
			}

			// ��ƷID;
			nIndex = strTemp.Find(_T(", \"name\": "));
			if ( nIndex != -1)
			{
				strTemp2 = strTemp.Left(nIndex);
				strTemp2.TrimLeft(_T("\""));
				strTemp2.TrimRight(_T("\""));
				//AryResult.ElementAt(i).Add(strTemp2);
				tagJson.strSpId = strTemp2;
				strTemp = strTemp.Mid(nIndex + _tcslen(_T(", \"name\": ")));
			}

			// ��Ʒ����;
			if ( strTemp.GetLength() )
			{
				strTemp.TrimLeft(_T("\""));
				strTemp.TrimRight(_T("\""));
				//AryResult.ElementAt(i).Add(strTemp);
				tagJson.strSpName = strTemp;
			}

			vtSpJson.push_back(tagJson);

			i++;
		}
	}

	void getXYJsonresult(IN CString strJson, OUT CString& strResult)
	{
		//CString strResult = _T("");
		CString strTemp = _T("");
		CString strSubJson = _T("");
		CString strMark = _T("\"general_require\": ");
		CString strMark2 = _T("");
		CString strImgName = _T("");		// ��Ƭ��;
		CString strImgctx = _T("");			// ��Ƭ�޸�Ҫ��;
		INT nIndex = strJson.Find(strMark);
		if ( nIndex != -1 )
			strJson = strJson.Mid(nIndex + strMark.GetLength());

		strMark = _T(", \"sample_list\": [{");
		nIndex = strJson.Find(strMark);
		if ( nIndex != -1 )
		{
			// �ܵ�Ҫ��;
			strTemp = strJson.Left(nIndex);
			strTemp.TrimLeft(_T("\""));
			strTemp.TrimRight(_T("\""));
			if ( !strTemp.IsEmpty() )
				strResult += strTemp + _T(";");
			strJson = strJson.Mid(nIndex + strMark.GetLength());
		}

		strMark = _T("\"modify\": ");
		strJson += strMark;
		nIndex = strJson.Find(strMark) ;
		if ( nIndex != -1 )
		{
			strJson = strJson.Mid(nIndex + strMark.GetLength());

			while( strJson.Find(strMark) != -1 )
			{
				nIndex = strJson.Find(strMark);
				strSubJson = strJson.Left(nIndex);
				strJson = strJson.Mid(nIndex+ strMark.GetLength());

				if ( nIndex != -1 )
				{
					strMark2 = _T(", \"modify_note\": ");
					nIndex = strSubJson.Find(strMark2);
					if ( nIndex != -1)
					{
						// ��Ƭѡ������ 2 = �޸ģ� 1 = ȷ��;
						strTemp = strSubJson.Left(nIndex);
						if ( strTemp == _T("2") )
						{
							strSubJson = strSubJson.Mid(nIndex + strMark2.GetLength() );
							strMark2 = _T("\", \"photo_name\": ");
							nIndex = strSubJson.Find(strMark2);
							if ( nIndex != -1 )
							{
								strImgctx = strSubJson.Left(nIndex);
								strImgctx.TrimLeft(_T("\""));
								strImgName = strSubJson.Mid(nIndex + strMark2.GetLength()+1);
								strImgName = strImgName.Left(strImgName.Find(_T("\"")));

								strResult += strImgName + _T(":") + strImgctx + _T(";");
							}
						}
					}
				}
			}
		}

		OutputDebugString(strResult + _T("\n"));
	}

	//////////////////////////////////////////////////////////////////////////
	HMODULE g_hQRCodeLibrary = NULL;
	API_GetQRCodeImg g_GetQRCodeImg = NULL;
	API_ShowQRCodeBitmap g_ShowQRCodeBitmap = NULL;
	API_ShowQRCodeOnDC g_ShowQRCodeOnDC = NULL;

	BOOL LoadQRCodeLibrary()
	{
		if ( g_hQRCodeLibrary == NULL )
		{
			g_hQRCodeLibrary = ( HMODULE )LoadLibrary(_T("QRCode.dll"));
			if ( g_hQRCodeLibrary == NULL )
				return FALSE;
		}

		g_GetQRCodeImg = (API_GetQRCodeImg)GetProcAddress(g_hQRCodeLibrary, "GetQRCodeBitmap");
		if ( g_GetQRCodeImg == NULL )
		{
			FreeLibrary(g_hQRCodeLibrary);
			return FALSE;
		}

		g_ShowQRCodeBitmap = (API_ShowQRCodeBitmap)GetProcAddress(g_hQRCodeLibrary, "ShowQRCodeBitmap");
		if ( g_ShowQRCodeBitmap == NULL )
		{
			FreeLibrary(g_hQRCodeLibrary);
			return FALSE;
		}

		g_ShowQRCodeOnDC = (API_ShowQRCodeOnDC)GetProcAddress(g_hQRCodeLibrary, "ShowQRCodeOnDC");
		if ( g_ShowQRCodeOnDC == NULL )
		{
			FreeLibrary(g_hQRCodeLibrary);
			return FALSE;
		}

		return TRUE;
	}

	void FreeQRCodeLibrary()
	{
		if ( g_hQRCodeLibrary )
		{
			if ( FreeLibrary(g_hQRCodeLibrary) )
			{
				g_hQRCodeLibrary = NULL;
				g_GetQRCodeImg = NULL;
				g_ShowQRCodeBitmap = NULL;
			}
		}
	}

	//////////////////////////////////////////////////////////////////////////
	// ΢��ģ��;
	HMODULE g_hWeiXinLibaray = NULL;
	API_ShowWeiXingDialog g_PtrShowWeiXinDialog = NULL;
	API_ShowWeiXingDialog g_PtrShowWXMsgconfigDialog = NULL;
	API_ShowWeiXingDialog g_PtrShowWXMsgRcordDialog = NULL;
	API_ShowSoftRunTimeDialog g_PtrShowSoftRunTimeDialog = NULL;

	BOOL LoadWeiXinLibaray()
	{
		if ( g_hWeiXinLibaray == NULL )
		{
			g_hWeiXinLibaray = (HMODULE)LoadLibrary(_T("lyfzWeiXin"));
			if (!g_hWeiXinLibaray)
				return FALSE;
		}

		g_PtrShowWeiXinDialog = (API_ShowWeiXingDialog) GetProcAddress(g_hWeiXinLibaray, "ShowWeiXinDialog");
		if ( !g_PtrShowWeiXinDialog )
		{
			FreeLibrary(g_hWeiXinLibaray);
			return FALSE;
		}

		g_PtrShowWXMsgconfigDialog = (API_ShowWeiXingDialog) GetProcAddress(g_hWeiXinLibaray, "ShowWXMsgConfigDialog");
		if ( !g_PtrShowWXMsgconfigDialog )
		{
			FreeLibrary(g_hWeiXinLibaray);
			return FALSE;
		}

		g_PtrShowWXMsgRcordDialog = (API_ShowWeiXingDialog) GetProcAddress(g_hWeiXinLibaray, "ShowWXMsgRecordDialog");
		if ( !g_PtrShowWXMsgRcordDialog )
		{
			FreeLibrary(g_hWeiXinLibaray);
			return FALSE;
		}

		g_PtrShowSoftRunTimeDialog = (API_ShowSoftRunTimeDialog) GetProcAddress(g_hWeiXinLibaray, "ShowSoftRunTimeDialog");
		if ( !g_PtrShowSoftRunTimeDialog )
		{
			FreeLibrary(g_hWeiXinLibaray);
			return FALSE;
		}

		return TRUE;
	}

	void FreeWeiXinLibaray()
	{
		if ( g_hWeiXinLibaray )
		{
			if ( FreeLibrary(g_hWeiXinLibaray) )
			{
				g_hWeiXinLibaray = NULL;
				g_PtrShowWeiXinDialog = NULL;
				g_PtrShowWXMsgconfigDialog = NULL;
				g_PtrShowWXMsgRcordDialog = NULL;
				g_PtrShowSoftRunTimeDialog = NULL;
			}
		}
	}

};