using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace INT69DB_2A.Part11_UserManager
{
    public partial class UserManager
    {
        public delegate void ObjectEventHandler(object data);
        public static event ObjectEventHandler UserListNewDataEvent;
        public static event ObjectEventHandler UserListAddDataEvent;
        public static event ObjectEventHandler UserListModifyUserDataEvent;
        public static event ObjectEventHandler UserListModifyInfoDataEvent;
        public static event ObjectEventHandler UserListDeleteDataEvent;
        public static event ObjectEventHandler UserListLockDataEvent;
        public static event ObjectEventHandler UserListDefautSettingDataEvent;
        public static event ObjectEventHandler UserListLoginTimeoutDataEvent;
        public static event ObjectEventHandler UserListGetListDataEvent;
        public static event ObjectEventHandler UserListGetListNameDataEvent;
        public static event ObjectEventHandler UserListGetVersionDataEvent;

        public delegate void IntObjectEventHandler(int status, object data);
        public static event IntObjectEventHandler UserListLoginDataEvent;

        public delegate void IntEventHandler(int status);
        public static event IntEventHandler UserListLogoutDataEvent;

        public static void IntializeUserManager(string folder)
        {
            // regisetr callback
            fnUserMgrVersionCallBack = UserManager_VersionCallback;
            DLL_UserMgr_Register_VersionCB(fnUserMgrVersionCallBack);

            fnUserMgrDebugCallBack = UserManager_DebugCallback;
            DLL_UserMgr_Register_DebugCB(fnUserMgrDebugCallBack);

            fnUserMgrNewUserCallBack = UserManager_NewUserCallBack;
            DLL_UserMgr_Register_NewStatusCB(fnUserMgrNewUserCallBack);

            fnUserMgrAddUserCallBack = UserManager_AddUserCallBack;
            DLL_UserMgr_Register_AddStatusCB(fnUserMgrAddUserCallBack);

            fnUserMgrModifyUserCallBack = UserManager_ModifyUserCallBack;
            DLL_UserMgr_Register_ModifyUserCB(fnUserMgrModifyUserCallBack);

            fnUserMgrModifyInfoCallBack = UserManager_ModifyInfoCallBack;
            DLL_UserMgr_Register_ModifyStatusCB(fnUserMgrModifyInfoCallBack);

            fnUserMgrDelUserCallBack = UserManager_DelUserCallBack;
            DLL_UserMgr_Register_DelStatusCB(fnUserMgrDelUserCallBack);

            fnUserMgrUserLockCallBack = UserManager_LockCallBack;
            DLL_UserMgr_Register_UserLockCB(fnUserMgrUserLockCallBack);

            fnUserMgrDefaultSettingCallBack = UserManager_DefaultSettingCallBack;
            DLL_UserMgr_Register_DefaultSettingCB(fnUserMgrDefaultSettingCallBack);

            fnUserMgrLoginCallBack = UserManager_LoginCallBack;
            DLL_UserMgr_Register_LoginStausCB(fnUserMgrLoginCallBack);

            fnUserMgrLogoutCallBack = UserManager_LogoutCallBack;
            DLL_UserMgr_Register_LogoutStausCB(fnUserMgrLogoutCallBack);

            fnUserMgrLoginTimeoutCallBack = UserManager_LoginTimeoutCallBack;
            DLL_UserMgr_Register_LoginTimeoutCB(fnUserMgrLoginTimeoutCallBack);

            fnUserMgrGetListCallBack = UserManager_GetListCallBack;
            DLL_UserMgr_Register_GetListCB(fnUserMgrGetListCallBack);

            fnUsermgrGetListNameCallBack = UserManager_GetListNameCallBack;
            DLL_UserMgr_Register_GetListNameCB(fnUsermgrGetListNameCallBack);

            DLL_UserMgr_Activation();

            IntPtr pFolder = Marshal.StringToBSTR(folder);
            DLL_UserMgr_SetFolder(pFolder);
        }

        #region UM_FUNCTION
        public static void UserManager_GetUserList(String data)
        {
            if (String.IsNullOrEmpty(data) == true)
                DLL_UserMgr_GetUserListID(IntPtr.Zero);
            else
            {
                IntPtr pID = Marshal.StringToBSTR(data);
                DLL_UserMgr_GetUserListID(pID);
            }
        }
        public static void UserManager_UserAdd(String sID, String sPW, int fAdmin, int expire_period_pw, int expire_period_account, int active_level, DateTime_t password_date, DateTime_t account_date)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pPW = Marshal.StringToBSTR(sPW.ToString());

            DLL_UserMgr_UserAdd(pID, pPW, fAdmin, expire_period_pw, expire_period_account, active_level, password_date, account_date);
        }
        public static void UserManager_UserNew(String sID, String sPW, int fAdmin, int expire_period_pw, int expire_period_account, int active_level, MenuID_t menuId)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pPW = Marshal.StringToBSTR(sPW.ToString());

            IntPtr pMenuId = Marshal.AllocHGlobal(Marshal.SizeOf(menuId));
            Marshal.StructureToPtr(menuId, pMenuId, false);

            DLL_UserMgr_UserNew(pID, pPW, fAdmin, expire_period_pw, expire_period_account, active_level, pMenuId);
        }
        public static void UserManager_UserAddHistoryPW(String sID, String sHistoryPW)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pPW = Marshal.StringToBSTR(sHistoryPW.ToString());

            DLL_UserMgr_UserAddHistoryPW(pID, pPW);
        }
        public static void UserManager_UserResetHistoryPW(String sID)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());

            DLL_UserMgr_UserResetHistoryPW(pID);
        }
        public static void UserManager_UserModify(String sID, String sPW, int fAdmin, int expire_period_pw, int expire_period_account, int active_level, MenuID_t menuId)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pPW = Marshal.StringToBSTR(sPW.ToString());

            IntPtr pMenuId = Marshal.AllocHGlobal(Marshal.SizeOf(menuId));
            Marshal.StructureToPtr(menuId, pMenuId, false);

            DLL_UserMgr_UserModify(pID, pPW, fAdmin, expire_period_pw, expire_period_account, active_level, pMenuId);
        }
        public static void UserManager_UserModifyPW(String sID, String sNewPW)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pPW = Marshal.StringToBSTR(sNewPW.ToString());

            DLL_UserMgr_UserModifyPW(pID, pPW);
        }
        public static int UserManager_UserModifyPWDirect(String sID, String sNewPW)
        {
            int ret = 0;

            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pPW = Marshal.StringToBSTR(sNewPW.ToString());

            ret = DLL_UserMgr_UserModifyPWDirect(pID, pPW);

            return ret;
        }
        public static int UserManager_UserResetPWDirect(String sID, String sNewPW)
        {
            int ret = 0;

            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pPW = Marshal.StringToBSTR(sNewPW.ToString());

            ret = DLL_UserMgr_UserResetPWDirect(pID, pPW);

            return ret;
        }

        public static void UserManager_UserModifyID(String sID, String sNewID)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pNewID = Marshal.StringToBSTR(sNewID.ToString());

            DLL_UserMgr_UserModifyID(pID, pNewID);
        }
        public static void UserManager_UserModifyOthers(String sID, int fadmin, int period_expire_pw, int period_expire_account, int active_level)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());

            DLL_UserMgr_UserModifyOthers(pID, fadmin, period_expire_pw, period_expire_account, active_level);
        }
        public static void UserManager_UserDel(String sID)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());

            DLL_UserMgr_UserDel(pID);
        }
        public static void UserManager_UserAllDel()
        {
            DLL_UserMgr_UserAllDel_Default();
        }
        public static void UserManager_UserLockRelease(String sID, bool lock_account, bool lock_password)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());

            DLL_UserMgr_UserLockRelease(pID, lock_account, lock_password);
        }
        public static void UserManager_UserLockGet(String sID)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());

            DLL_UserMgr_UserLockGet(pID);
        }
        public static void UserManager_UserLoginCB(String sID, String sPW)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pPW = Marshal.StringToBSTR(sPW.ToString());

            DLL_UserMgr_UserLoginCB(pID, pPW);
        }
        public static void UserManager_UserLoginDirect(string sID, string sPW, ref UserMgr_user_info_t user_info)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());
            IntPtr pPW = Marshal.StringToBSTR(sPW.ToString());

            DLL_UserMgr_UserLoginDirect(pID, pPW, ref user_info);
        }
        public static void UserManager_UserLogout()
        {
            DLL_UserMgr_UserLogout();
        }
        public static void UserManager_AutoLogoutTimeoutReset()
        {
            DLL_UserMgr_AutoLogoutTimeoutReset();
        }
        public static void UserManager_AutoLogoutTimeoutPause()
        {
            DLL_UserMgr_AutoLogoutTimeoutPause();
        }
        public static void UserManager_AutoLogoutSetTimeout(int timeout_min, int warning_timeout_sec)
        {
            DLL_UserMgr_AutoLogoutSetTimeout(timeout_min, warning_timeout_sec);
        }
        public static void UserManager_GetUserList(int index)
        {
            DLL_UserMgr_GetUserList(index);
        }
        public static void UserManager_GetUserListID(String sID)
        {
            IntPtr pID = Marshal.StringToBSTR(sID.ToString());

            DLL_UserMgr_GetUserListID(pID);
        }
        public static void UserManager_GetUserListName()
        {
            DLL_UserMgr_GetUserListName();
        }
        public static void UserManager_GetVersion()
        {
            DLL_UserMgr_Getversion();
        }
        #endregion

        #region UM_CALLBACK
        private static void UserManager_GetListNameCallBack(ref UserMgr_user_list_name_t user_name)
        {
            try
            {
                if (UserListGetListNameDataEvent != null)
                    UserListGetListNameDataEvent(user_name);
            }
            catch
            {
            }
        }
        private static void UserManager_GetListCallBack(ref UserMgr_user_list_t user_list)
        {
            try
            {
                if (UserListGetListDataEvent != null)
                    UserListGetListDataEvent(user_list);
            }
            catch
            {
            }
        }
        private static void UserManager_LoginTimeoutCallBack(ref UserMgr_login_timeout_t timeout)
        {
            try
            {
                if (UserListLoginTimeoutDataEvent != null)
                    UserListLoginTimeoutDataEvent(timeout);
            }
            catch
            {
            }
        }
        private static void UserManager_LogoutCallBack(int status)
        {
            try
            {
                if (UserListLogoutDataEvent != null)
                    UserListLogoutDataEvent(status);

            }
            catch
            {
            }
        }
        private static void UserManager_LoginCallBack(int status, IntPtr pBuffer)
        {
            try
            {
                if (UserListLoginDataEvent != null)
                    UserListLoginDataEvent(status, pBuffer);
            }
            catch
            {
            }
        }
        private static void UserManager_LockCallBack(ref UserMgr_user_lock_t user_lock)
        {
            try
            {
                if (UserListLockDataEvent != null)
                    UserListLockDataEvent(user_lock);
            }
            catch
            {
            }
        }
        private static void UserManager_DefaultSettingCallBack(int status)
        {
            try
            {
                if (UserListDefautSettingDataEvent != null)
                    UserListDefautSettingDataEvent(status);
            }
            catch
            {
            }
        }
        private static void UserManager_DelUserCallBack(ref UserMgr_user_del_t user_del)
        {
            try
            {
                if (UserListDeleteDataEvent != null)
                    UserListDeleteDataEvent(user_del);
            }
            catch
            {
            }
        }
        private static void UserManager_ModifyUserCallBack(ref UserMgr_user_modify_t user_mod)
        {
            try
            {
                if (UserListModifyUserDataEvent != null)
                    UserListModifyUserDataEvent(user_mod);
            }
            catch
            {
            }
        }
        private static void UserManager_ModifyInfoCallBack(ref UserMgr_user_info_t user_info)
        {
            try
            {
                if (UserListModifyInfoDataEvent != null)
                    UserListModifyInfoDataEvent(user_info);
            }
            catch
            {
            }
        }
        private static void UserManager_AddUserCallBack(ref UserMgr_user_add_t user_add)
        {
            try
            {
                if (UserListAddDataEvent != null)
                    UserListAddDataEvent(user_add);
            }
            catch
            {
            }
        }
        private static void UserManager_NewUserCallBack(IntPtr pBuffer)
        {
            try
            {
                if (pBuffer != IntPtr.Zero)
                {
                    UserMgr_user_info_t user_info = (UserMgr_user_info_t)Marshal.PtrToStructure(pBuffer, typeof(UserMgr_user_info_t));

                    if (UserListNewDataEvent != null)
                        UserListNewDataEvent(user_info);
                }
            }
            catch
            {
            }
        }
        private static void UserManager_DebugCallback(int type, StringBuilder pMessage)
        {
            try
            {
            }
            catch
            {
            }
        }
        private static void UserManager_VersionCallback(StringBuilder version)
        {
            try
            {
                if (UserListGetVersionDataEvent != null)
                    UserListGetVersionDataEvent(version);
            }
            catch
            {

            }
        }
        #endregion
    }
}
