void CBacnetUserConfig::OnBnClickedButtonUserDelete()
{
	// TODO: Add your control notification handler code here
	if((m_slected_item<0) || (m_slected_item >= BAC_USER_LOGIN_COUNT ))
		return;

	memset(&m_user_login_data.at(m_slected_item),0,sizeof(Str_userlogin_point));



	int temp_invoke_id = -1;
	int send_status = true;
	int	resend_count = 0;
	for (int z=0;z<3;z++)
	{
		do 
		{
			resend_count ++;
			if(resend_count>5)
			{
				send_status = false;
				break;
			}
			temp_invoke_id = WritePrivateData(g_bac_instance,WRITEUSER_T3000,m_slected_item,m_slected_item);

			Sleep(SEND_COMMAND_DELAY_TIME);
		} while (g_invoke_id<0);
		if(send_status)
		{
			Sleep(1000);
			if(tsm_invoke_id_free(temp_invoke_id))
			{
				ok_button_stage = 0;
				PostMessage(WM_REFRESH_BAC_USER_NAME_LIST,NULL,NULL);
				Enable_Window_Stage(HIDE_ALL);
				MessageBox(_T("Delete success!"));
				return;
			}
			else
				continue;
		}
	}

	MessageBox(_T("Timeout!Please try again!"));

}
void CBacnetProgramEdit::OnSend()
{
	// TODO: Add your command handler code here
	renumvar = 1;
	error = -1; //Default no error;
	CString tempcs;
	((CRichEditCtrl *)GetDlgItem(IDC_RICHEDIT2_PROGRAM))->GetWindowTextW(tempcs);

	char*     pElementText;
	int    iTextLen;
	// wide char to multi char
	iTextLen = WideCharToMultiByte( CP_ACP,0,tempcs,-1,NULL,0,NULL,NULL );
	memset( ( void* )editbuf, 0, sizeof( char ) * ( iTextLen + 1 ) );
	::WideCharToMultiByte( CP_ACP,0,tempcs,-1,editbuf,iTextLen,NULL,NULL );
	Encode_Program();
	if(error == -1)
	{
		TRACE(_T("Encode_Program length is %d ,copy length is %d\r\n"),program_code_length[program_list_line],my_lengthcode + 10);
		if(my_lengthcode+10 <400)
		memcpy_s(program_code[program_list_line],my_lengthcode+10,mycode,my_lengthcode + 10);
		else
		{
			CString temp_mycs;//如果npdu的长度过长,大于 400了,目前是通过换一个page来存;
			temp_mycs.Format(_T("Encode Program Code Length is %d,Please Try to code in another page."),my_lengthcode+10);
			MessageBox(temp_mycs);
			memset(program_code[program_list_line],0,400);
			return;
		}
			

		g_invoke_id =WritePrivateData(g_bac_instance,WRITEPROGRAMCODE_T3000,program_list_line,program_list_line/*,my_lengthcode*/);
		if(g_invoke_id>=0)
		{
			CString temp_cs_show;
			temp_cs_show.Format(_T("Task ID = %d. Write program code to item %d "),g_invoke_id,program_list_line);
			Post_Invoke_ID_Monitor_Thread(MY_INVOKE_ID,g_invoke_id,this->m_hWnd/*BacNet_hwd*/,temp_cs_show);
		}

	}
	else
	{
		CString cstring_error;

		int len = strlen(mesbuf);
		int  unicodeLen = ::MultiByteToWideChar( CP_ACP,0, mesbuf,-1,NULL,0 ); 
		::MultiByteToWideChar( CP_ACP,  0,mesbuf,-1,cstring_error.GetBuffer(MAX_PATH),unicodeLen );  
		cstring_error.ReleaseBuffer();


//		MessageBox(cstring_error);
		CStringArray  Error_info;  
		SplitCStringA(Error_info,cstring_error,_T("\r\n"));//Splite the CString with "\r\n" and then add to the list.(Fance)
		Sleep(1);
		m_information_window.ResetContent();
		for (int i=0;i<(int)Error_info.GetSize();i++)
		{
			m_information_window.InsertString(i,Error_info.GetAt(i));
		}

		MessageBox(_T("Errors,program NOT Sent!"));
	}

}
void CBacnetUserConfig::OnBnClickedButtonUserOk()
{
	// TODO: Add your control notification handler code here
	char temp_buffer[255];
	int compare_ret = 0;
	CString temp_enter_original;
	CString first_pw;
	CString second_pw;
	CString temp_user_name;
	CString access_string;
	switch(ok_button_stage)
	{
	case stage_enter_original_password://检查原始密码是否正确;
		{
			//m_user_login_data.at(m_slected_item).password
			GetDlgItemTextW(IDC_EDIT_USER_NAME,temp_user_name);
			GetDlgItemTextW(IDC_EDIT_USER_PASSWORD,temp_enter_original);
			if(temp_user_name.IsEmpty())
			{
				MessageBox(_T("user name is empty"),_T("Warning"),MB_OK);
				return;
			}
			if(temp_user_name.GetLength()>=STR_USER_NAME_LENGTH)
			{
				MessageBox(_T("User name is too long!"),_T("Warning"),MB_OK);
				return;
			}
			if(temp_enter_original.IsEmpty())
			{
				MessageBox(_T("Password is empty"),_T("Warning"),MB_OK);
				return;
			}
			memset(temp_buffer,0,255);
			WideCharToMultiByte(CP_ACP,NULL,temp_enter_original.GetBuffer(),-1,temp_buffer,255,NULL,NULL);
			compare_ret = strcmp(temp_buffer,m_user_login_data.at(m_slected_item).password);
			if(compare_ret == 0)
			{
				ok_button_stage = stage_enter_new_password;
				Enable_Window_Stage(ENTER_NEW_PASSWORD);
			}
			else
			{
				SetDlgItemTextW(IDC_EDIT_USER_PASSWORD,_T(""));
				MessageBox(_T("Password error"),_T("Warning"),MB_OK);	
				GetDlgItem(IDC_EDIT_USER_PASSWORD)->SetFocus();
				return;
			}
		}
		break;
	case stage_enter_new_password:
		{
			GetDlgItemTextW(IDC_EDIT_USER_NEW_PASSWORD,first_pw);
			if(first_pw.IsEmpty())
			{
				MessageBox(_T("Password is empty"),_T("Warning"),MB_OK);
				return;
			}
			if(first_pw.GetLength()>=STR_USER_PASSWORD_LENGTH)
			{
				MessageBox(_T("Password too long!"),_T("Warning"),MB_OK);
				return;
			}
			ok_button_stage = stage_retype_password;
			Enable_Window_Stage(RETYPE_NEW_PASSWORD);
		}
		break;
	case  stage_retype_password:
		{
			GetDlgItemTextW(IDC_EDIT_USER_NEW_PASSWORD,first_pw);
			GetDlgItemTextW(IDC_EDIT_USER_RETYPE_PASSWORD,second_pw);
			GetDlgItemTextW(IDC_EDIT_USER_NAME,temp_user_name);
			GetDlgItemTextW(IDC_COMBO_ACCESS_LEVEL,access_string);
			
			if(second_pw.IsEmpty())
			{
				MessageBox(_T("Password is empty"),_T("Warning"),MB_OK);
				return;
			}
			if(second_pw.GetLength()>=STR_USER_PASSWORD_LENGTH)
			{
				MessageBox(_T("Password too long!"),_T("Warning"),MB_OK);
				return;
			}

			if(first_pw.CompareNoCase(second_pw) == 0)
			{
				if(temp_user_name.IsEmpty())
				{
					MessageBox(_T("user name is empty"),_T("Warning"),MB_OK);
					return;
				}
				memset(temp_buffer,0,255);
				WideCharToMultiByte(CP_ACP,NULL,temp_user_name.GetBuffer(),-1,temp_buffer,255,NULL,NULL);
				memcpy(m_user_login_data.at(m_slected_item).name,temp_buffer,STR_USER_NAME_LENGTH);

				if(access_string.CompareNoCase(_T("View only")) == 0 )
				{
					m_user_login_data.at(m_slected_item).access_level = 1;
				}
				else if(access_string.CompareNoCase(_T("Graphic Mode")) == 0)
				{
					m_user_login_data.at(m_slected_item).access_level = 3;
				}
				else if(access_string.CompareNoCase(_T("Routine Mode")) == 0)
				{
					m_user_login_data.at(m_slected_item).access_level = 4;
				}
				else
					m_user_login_data.at(m_slected_item).access_level = 2;
				//_T("View only"));
				//_T("Full access"));

				memset(temp_buffer,0,255);
				WideCharToMultiByte(CP_ACP,NULL,second_pw.GetBuffer(),-1,temp_buffer,255,NULL,NULL);
				memcpy(m_user_login_data.at(m_slected_item).password,temp_buffer,STR_USER_PASSWORD_LENGTH);

				//Post_Write_Message(g_bac_instance,WRITEUSER_T3000,m_slected_item,m_slected_item,sizeof(Str_userlogin_point),m_user_config_hwnd ,_T(""),m_slected_item,m_slected_item);
				int temp_invoke_id = -1;
				int send_status = true;
				int	resend_count = 0;
				for (int z=0;z<3;z++)
				{
					do 
					{
						resend_count ++;
						if(resend_count>5)
						{
							send_status = false;
							break;
						}
						temp_invoke_id = WritePrivateData(g_bac_instance,WRITEUSER_T3000,m_slected_item,m_slected_item);

						Sleep(SEND_COMMAND_DELAY_TIME);
					} while (g_invoke_id<0);
					if(send_status)
					{
						Sleep(1000);
						if(tsm_invoke_id_free(temp_invoke_id))
						{
							ok_button_stage = 0;
							PostMessage(WM_REFRESH_BAC_USER_NAME_LIST,NULL,NULL);
							Enable_Window_Stage(HIDE_ALL);
							MessageBox(_T("Operation success!"),_T("Information"),MB_OK);
							return;
						}
						else
							continue;
					}
				}



			}
			else
			{
				MessageBox(_T("Password is not the same!"),_T("Warning"),MB_OK);
				return;
			}
		}
		break;
	default:
		break;
	}
}
void CBacnetUserConfig::OnBnClickedCheckUserlistInfo()
{
	// TODO: Add your control notification handler code here
	if(!((CButton *)GetDlgItem(IDC_CHECK_USERLIST_INFO))->GetCheck())
	{
		((CButton *)GetDlgItem(IDC_CHECK_USERLIST_INFO))->SetCheck(false);

		Device_Basic_Setting.reg.user_name = 1;
		//CString temp_task_info;
		//temp_task_info.Format(_T("Disable user list feature "));
		//Post_Write_Message(g_bac_instance,(int8_t)WRITE_SETTING_COMMAND,0,0,sizeof(Str_Setting_Info),this->m_hWnd,temp_task_info);

	}
	else
	{
		bool any_user_valid = false;
		bool any_user_administrator = false;
		//检测是否里面存在有效的账号密码;
		for (int i=0;i<(int)m_user_login_data.size();i++)
		{
			CString temp_user_name;
			MultiByteToWideChar( CP_ACP, 0, (char *)m_user_login_data.at(i).name, (int)strlen((char *)m_user_login_data.at(i).name)+1, 
				temp_user_name.GetBuffer(MAX_PATH), MAX_PATH );
			temp_user_name.ReleaseBuffer();

			CString temp_password;
			MultiByteToWideChar( CP_ACP, 0, (char *)m_user_login_data.at(i).password, (int)strlen((char *)m_user_login_data.at(i).password)+1, 
				temp_password.GetBuffer(MAX_PATH), MAX_PATH );
			temp_password.ReleaseBuffer();
			if(!temp_user_name.IsEmpty())
			{
				if(!temp_password.IsEmpty())
				{
					if(m_user_login_data.at(i).access_level == LOGIN_SUCCESS_FULL_ACCESS)
					{
						any_user_administrator = true;
						break;
					}
					any_user_valid = true;	//User表里面 存在可用的 账号密码;
				}
			}		
		}

		if(any_user_administrator)
		{
			((CButton *)GetDlgItem(IDC_CHECK_USERLIST_INFO))->SetCheck(true);
			Device_Basic_Setting.reg.user_name = 2;
			/*CString temp_task_info;
			temp_task_info.Format(_T("Enable user list feature "));
			Post_Write_Message(g_bac_instance,(int8_t)WRITE_SETTING_COMMAND,0,0,sizeof(Str_Setting_Info),this->m_hWnd,temp_task_info);*/
		}
		else if(any_user_valid)
		{
			((CButton *)GetDlgItem(IDC_CHECK_USERLIST_INFO))->SetCheck(false);
			Device_Basic_Setting.reg.user_name = 1;
			MessageBox(_T("User List don't contain any user have full access level!\r\nPlease add it and then enable this feature!"),_T("Warning"),MB_OK | MB_ICONINFORMATION);
			return;
		}
		else
		{
			((CButton *)GetDlgItem(IDC_CHECK_USERLIST_INFO))->SetCheck(false);
			Device_Basic_Setting.reg.user_name = 1;
			MessageBox(_T("User List don't contain any valid user name and password!\r\nPlease add it and then enable this feature!"),_T("Warning"),MB_OK | MB_ICONINFORMATION);
			return;
		}

		

	}

	int temp_invoke_id = -1;
	int send_status = true;
	int	resend_count = 0;
	for (int z=0;z<3;z++)
	{
		do 
		{
			resend_count ++;
			if(resend_count>5)
			{
				send_status = false;
				break;
			}
			temp_invoke_id = WritePrivateData(g_bac_instance,WRITE_SETTING_COMMAND,0,0);

			Sleep(SEND_COMMAND_DELAY_TIME);
		} while (g_invoke_id<0);
		if(send_status)
		{
			Sleep(1000);
			if(tsm_invoke_id_free(temp_invoke_id))
			{
				ok_button_stage = 0;
				PostMessage(WM_REFRESH_BAC_USER_NAME_LIST,NULL,NULL);
				Enable_Window_Stage(HIDE_ALL);
				MessageBox(_T("Operation success!"),_T("Information"),MB_OK);
				return;
			}
			else
				continue;
		}
	}

}