// DlgUserSettings.cpp : implementation file
//

#include "stdafx.h"
#include "MsSDKTest.h"
#include "MsSDKTestDlg.h"
#include "DlgUserSettings.h"
#include "DlgUserSettingsWarning.h"
#include "MsIpcSDK.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgUserSettings dialog
enum LIST_COLUMN
{
	COLUMN_USERNAME = 0,
	//COLUMN_PASSWORD,
	COLUMN_AUTHORITY,
};


CDlgUserSettings::CDlgUserSettings(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgUserSettings::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgUserSettings)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgUserSettings::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgUserSettings)
	DDX_Control(pDX, IDC_LIST_USER, m_listUser);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgUserSettings, CDialog)
	//{{AFX_MSG_MAP(CDlgUserSettings)
	ON_NOTIFY(NM_CLICK, IDC_LIST_USER, OnClickListUser)
	ON_NOTIFY(NM_RCLICK, IDC_LIST_USER, OnRclickListUser)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgUserSettings message handlers

BOOL CDlgUserSettings::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	((CEdit*)GetDlgItem(IDC_EDIT_USERNAME))->SetLimitText(32);
	((CEdit*)GetDlgItem(IDC_EDIT_PASSWORD))->SetLimitText(16);
	InitListCtrl();
	LoadAllUser();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CDlgUserSettings::InitListCtrl()
{
	DWORD dwStyle = m_listUser.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	dwStyle |= LVS_EX_GRIDLINES;
	m_listUser.SetExtendedStyle(dwStyle);


	m_listUser.InsertColumn(COLUMN_USERNAME, "Name", LVCFMT_LEFT, 160);
	//m_listUser.InsertColumn(COLUMN_PASSWORD, "Password", LVCFMT_LEFT, 140);
	m_listUser.InsertColumn(COLUMN_AUTHORITY, "Authority", LVCFMT_LEFT, 100);

	return TRUE;
}

void GetUserAuthority(int nType, char *sAuthority, int nLen)
{
	if(nType == 0){
		_snprintf(sAuthority, nLen-1, "Admin");
	}else if(nType == 1){
		_snprintf(sAuthority, nLen-1, "Operator");	
	}else if(nType == 2){
		_snprintf(sAuthority, nLen-1, "Viewer");	
	}else{
		_snprintf(sAuthority, nLen-1, "Unknown");
	}
}
BOOL CDlgUserSettings::LoadAllUser()
{
	char sAuthority[32] = {0};
	Ms_Ipc_UserInfo userInfo[MAX_USER_NUM] = {0};
	if(Ms_Ipc_GetAllUser(m_nUserId, userInfo))
	{
		CMsSDKTestDlg::OutputTips("Init user list failed. Please try again...");
		return FALSE;
	}
	for(int i = 0; i < MAX_USER_NUM; i++){
		if(userInfo[i].nUserType > 2 || userInfo[i].sUserName[0] == '\0')
			break;
		int nItem = m_listUser.InsertItem(i, userInfo[i].sUserName, 0);
		//m_listUser.SetItemText(nItem, COLUMN_PASSWORD, userInfo[i].sPassword);
		GetUserAuthority(userInfo[i].nUserType, sAuthority, sizeof(sAuthority));
		m_listUser.SetItemText(nItem, COLUMN_AUTHORITY, sAuthority);
	}
	return TRUE;
}

void CDlgUserSettings::DeleteAllItem()
{
	m_listUser.DeleteAllItems();
}

void CDlgUserSettings::OnOK() 
{
	// TODO: Add extra validation here
	char sUsername[32] = {0}, sPassword[16] = {0};
	Ms_Ipc_UserInfo userInfo = {0};
	if(GetCheckedRadioButton(IDC_RADIO1, IDC_RADIO3) == IDC_RADIO1)
		userInfo.nUserType = 0;
	else if(GetCheckedRadioButton(IDC_RADIO1, IDC_RADIO3) == IDC_RADIO2)
		userInfo.nUserType = 1;
	else
		userInfo.nUserType = 2;
	CString strTemp;
	GetDlgItem(IDC_EDIT_USERNAME)->GetWindowText(strTemp); strTemp.TrimLeft();strTemp.TrimRight();
	GetDlgItem(IDC_EDIT_USERNAME)->SetWindowText(strTemp);
	GetDlgItem(IDC_EDIT_PASSWORD)->GetWindowText(strTemp); strTemp.TrimLeft();strTemp.TrimRight();
	GetDlgItem(IDC_EDIT_PASSWORD)->SetWindowText(strTemp);

	GetDlgItem(IDC_EDIT_USERNAME)->GetWindowText(sUsername, sizeof(sUsername));
	GetDlgItem(IDC_EDIT_PASSWORD)->GetWindowText(sPassword, sizeof(sPassword));
	if(strlen(sUsername) < 4 || strlen(sPassword) < 4){
		AfxMessageBox("UserName or Password can't be less than 4 characters.");
		return ;
	}
	_snprintf(userInfo.sUserName, sizeof(userInfo.sUserName), "%s", sUsername);
	_snprintf(userInfo.sPassword, sizeof(userInfo.sPassword), "%s", sPassword);
	
	Ms_Ipc_DeleteUser(m_nUserId, sUsername);
	if(!Ms_Ipc_AddUser(m_nUserId, &userInfo))
	{
		char sNameTemp[32] = {0}, sAuthority[32] = {0};
		BOOL bNewUser = TRUE;
		for(int i = 0; i < m_listUser.GetItemCount(); i++)
		{
			m_listUser.GetItemText(i, COLUMN_USERNAME, sNameTemp, sizeof(sNameTemp));
			if(!strcmp(sUsername, sNameTemp))
			{
				bNewUser = FALSE;
				break;
			}
		}
		if(bNewUser){
			int nItem = m_listUser.InsertItem(m_listUser.GetItemCount(), userInfo.sUserName, 0);
			//m_listUser.SetItemText(nItem, COLUMN_PASSWORD, userInfo[i].sPassword);
			GetUserAuthority(userInfo.nUserType, sAuthority, sizeof(sAuthority));
			m_listUser.SetItemText(nItem, COLUMN_AUTHORITY, sAuthority);
			if(IsTiIPC(CMsSDKTestDlg::m_pThis->m_sModel))
			{
				CDlgUserSettingsWarning dlgNewUserWarning(this);
				if(dlgNewUserWarning.DoModal() == IDOK)
				{
					Ms_Ipc_RebootSystem(m_nUserId);
				}
			}else{ // amba don't need to reboot
				AfxMessageBox("Add user successfully");
			}
		}else{
			AfxMessageBox("Successfully");
		}
	}
	else
		AfxMessageBox("Failed.");
	//Ms_Ipc_ModifyPassword(m_nUserId, sPassword, sizeof(sPassword));
	//CDialog::OnOK();
}

void CDlgUserSettings::OnClickListUser(NMHDR* pNMHDR, LRESULT* pResult) 
{
	char sUsername[32] = {0};
	char sAuthority[32] = {0};
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	if (pNMListView->iSubItem != -1 && pNMListView->iItem != -1){
		m_listUser.GetItemText(pNMListView->iItem, COLUMN_USERNAME, sUsername, sizeof(sUsername));
		m_listUser.GetItemText(pNMListView->iItem, COLUMN_AUTHORITY, sAuthority, sizeof(sAuthority));
		GetDlgItem(IDC_EDIT_USERNAME)->SetWindowText(sUsername);
		if(!stricmp(sAuthority, "admin")){
			CheckRadioButton(IDC_RADIO1, IDC_RADIO3, IDC_RADIO1);
		}else if(!stricmp(sAuthority, "operator")){
			CheckRadioButton(IDC_RADIO1, IDC_RADIO3, IDC_RADIO2);
		}else{// if(!stricmp(sAuthority, "viewer"))
			CheckRadioButton(IDC_RADIO1, IDC_RADIO3, IDC_RADIO3);
		}
		if(!stricmp(sUsername, "admin")){ // admin 不允许用户类型
			GetDlgItem(IDC_RADIO2)->EnableWindow(FALSE);
			GetDlgItem(IDC_RADIO3)->EnableWindow(FALSE);
		}else{
			GetDlgItem(IDC_RADIO2)->EnableWindow(TRUE);
			GetDlgItem(IDC_RADIO3)->EnableWindow(TRUE);
		}
	}
	*pResult = 0;
	UpdateData(FALSE);
}

void CDlgUserSettings::OnRclickListUser(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	char sUsername[32] = {0};
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	if(pNMListView->iItem == -1) return;
	CPoint pt; 
	GetCursorPos(&pt);
	CMenu menu;
	menu.LoadMenu(IDR_MENU_USER);
	CMenu *pPopMenu = menu.GetSubMenu(0);
	ASSERT(pPopMenu != NULL);

	m_listUser.GetItemText(pNMListView->iItem, COLUMN_USERNAME, sUsername, sizeof(sUsername));
	if(!strcmp(sUsername, "admin")) // 不能删除admin用户
		pPopMenu->EnableMenuItem(IDR_MENU_DELETE_USER, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);

	int nCmd = pPopMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON|TPM_RETURNCMD, pt.x, pt.y, this);
	switch(nCmd)
	{
	case IDR_MENU_DELETE_USER:
		if(sUsername[0] == '\0') return;
		if(!Ms_Ipc_DeleteUser(m_nUserId, sUsername)){
			AfxMessageBox("Successfully");
			((CListCtrl*)GetDlgItem(IDC_LIST_USER))->DeleteItem(pNMListView->iItem);
		}
		else
			AfxMessageBox("Failed.");
		break;
	default:
		break;
	}
	*pResult = 0;
}
