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 ReminderCRUD : Aram_CRM_Project.DAL.DA
    {
        public int InsertReminder(Reminder ReminderVar)
        {
            try
            {
                base.SQL = "SP_InsertReminder";
                base.InitializeCommand();
                base.AddParameter("@Name", ReminderVar.Name);
                base.AddParameter("@Date", ReminderVar.Date);
                base.AddParameter("@Comment", ReminderVar.Comment);
                base.AddParameter("@UserId", ReminderVar.UserId);
                base.AddParameter("@Alarm", ReminderVar.Alarm);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

        public int UpdateReminder(Reminder ReminderVar)
        {
            try
            {
                base.SQL = "SP_UpdateReminder";
                base.InitializeCommand();
                base.AddParameter("@Id", ReminderVar.Id);
                base.AddParameter("@Name", ReminderVar.Name);
                base.AddParameter("@Date", ReminderVar.Date);
                base.AddParameter("@Comment", ReminderVar.Comment);
                base.AddParameter("@Alarm", ReminderVar.Alarm);
                return base.ExecuteStoredProcedure();
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("UNIQUE KEY") && exp.Message.Contains("duplicate"))
                    return -1;
                else
                    return 0;
            }
        }

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

        public Reminder ReadReminder(string Id)
        {
            try
            {
                Reminder ReminderVar = new Reminder();
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_ReadReminder";
                base.InitializeCommand();
                base.AddParameter("@Id", Id);
                base.FillDataTable(dtTemp);
                ReminderVar.Id = dtTemp.Rows[0]["Id"].ToString();
                ReminderVar.Name = dtTemp.Rows[0]["Name"].ToString();
                ReminderVar.Date = dtTemp.Rows[0]["Date"].ToString();
                ReminderVar.Comment = dtTemp.Rows[0]["Comment"].ToString();
                ReminderVar.UserId = dtTemp.Rows[0]["UserId"].ToString();
                ReminderVar.Alarm = dtTemp.Rows[0]["Alarm"].ToString();
                dtTemp.Dispose();
                return ReminderVar;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable SearchReminderForAdmin(Reminder ReminderVar)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_SearchReminderForAdmin";
                base.InitializeCommand();
                base.AddParameter("@Name", ReminderVar.Name);
                base.AddParameter("@StartDate", ReminderVar.StartDate);
                base.AddParameter("@EndDate", ReminderVar.EndDate);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }


        public DataTable SearchReminderForAlarm(string UserId, string Date)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_SearchReminderForAlarm";
                base.InitializeCommand();
                base.AddParameter("@UserId", UserId);
                base.AddParameter("@Date", Date);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable SearchReminder(Reminder ReminderVar)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_SearchReminder";
                base.InitializeCommand();
                base.AddParameter("@Name", ReminderVar.Name);
                base.AddParameter("@StartDate", ReminderVar.StartDate);
                base.AddParameter("@EndDate", ReminderVar.EndDate);
                base.AddParameter("@UserId", ReminderVar.UserId);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public DataTable ReportReminder(Reminder ReminderVar)
        {
            try
            {
                DataTable dtTemp = new DataTable();
                base.SQL = "SP_ReportReminder";
                base.InitializeCommand();
                base.AddParameter("@Name", ReminderVar.Name);
                base.AddParameter("@StartDate", ReminderVar.StartDate);
                base.AddParameter("@EndDate", ReminderVar.EndDate);
                base.FillDataTable(dtTemp);
                return dtTemp;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
    }
}
