using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Aram_CRM_Project.BLL.Entity;

namespace Aram_CRM_Project.BLL
{
    class UserCRUD : Aram_CRM_Project.DAL.DA
    {
        public int InsertUser(User UserVar)
        {
            try
            {
                base.SQL = "SP_InsertUser";
                base.InitializeCommand();
                base.AddParameter("@Name", UserVar.Name);
                base.AddParameter("@Username", UserVar.Username);
                base.AddParameter("@Password", UserVar.Password);
                base.AddParameter("@UserStatusId", UserVar.UserStatusId);
                base.AddParameter("@Comment", UserVar.Comment);
                base.AddParameter("@MenuItem", UserVar.MenuItem);
                base.AddParameter("@MenuItemCount", UserVar.MenuItemCount);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

        public int UpdateUser(User UserVar)
        {
            try
            {
                base.SQL = "SP_UpdateUser";
                base.InitializeCommand();
                base.AddParameter("@Id", UserVar.Id);
                base.AddParameter("@Name", UserVar.Name);
                base.AddParameter("@Username", UserVar.Username);
                base.AddParameter("@Password", UserVar.Password);
                base.AddParameter("@UserStatusId", UserVar.UserStatusId);
                base.AddParameter("@Comment", UserVar.Comment);
                base.AddParameter("@MenuItem", UserVar.MenuItem);
                base.AddParameter("@MenuItemCount", UserVar.MenuItemCount);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

        public int DeleteUser(string Id)
        {
            try
            {
                base.SQL = "SP_DeleteUserf";
                base.InitializeCommand();
                base.AddParameter("@Id", Id);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("COLUMN REFERENCE"))
                    return -1;
                else
                    return 0;
            }
        }

        public User ReadUser(string Id)
        {
            try
            {
                User UserVar = new User();
                DataTable dtTemp = new DataTable();

                base.SQL = "SP_ReadUser";
                base.InitializeCommand();
                base.AddParameter("@Id", Id);
                base.FillDataTable(dtTemp);
                UserVar.Id = dtTemp.Rows[0]["Id"].ToString();
                UserVar.Name = dtTemp.Rows[0]["Name"].ToString();
                UserVar.Username = dtTemp.Rows[0]["Username"].ToString();
                UserVar.Password = dtTemp.Rows[0]["Password"].ToString();
                UserVar.UserStatusId = dtTemp.Rows[0]["UserStatusId"].ToString();
                UserVar.Comment = dtTemp.Rows[0]["Comment"].ToString();
                UserVar.MenuItem = dtTemp.Rows[0]["MenuItem"].ToString();
                UserVar.MenuItemCount = dtTemp.Rows[0]["MenuItemCount"].ToString();
                dtTemp.Dispose();
                return UserVar;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable SearchUser(string Name)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_SearchUser";
                base.InitializeCommand();
                base.AddParameter("@Name", Name);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable ReportUser(string Name)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_ReportUser";
                base.InitializeCommand();
                base.AddParameter("@Name", Name);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public int UpdatePassword(string Id, string Password)
        {
            try
            {
                base.SQL = "SP_UpdatePassword";
                base.InitializeCommand();
                base.AddParameter("@Id", Id);
                base.AddParameter("@Password", Password);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

    }
}
