using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Domain;
using NHibernate;
using System.Linq.Dynamic;

namespace Dao.Implement
{
	public class GroupRepository : RepositoryBase<Group>, IGroupRepository
	{
		public IList<Group> GetGroupList(Group group)
		{
			IList<Group> groupList = new List<Group>();

			if (group == null)
			{
				return groupList;
			}

			groupList.Add(group);

			IList<Group> subGroups = LoadAll().Where(r => r.Parent == group).OrderBy(o => o.Sort).ToList();

			foreach (Group subGroup in subGroups)
			{
				foreach (Group subGroup1 in GetGroupList(subGroup))
				{
					groupList.Add(subGroup1);
				}
			}

			return groupList;
		}

        public IList<Group> GetGroupList(int hotelID)
        {
            IList<Group> groupList = new List<Group>();

            if (hotelID == 0)
            {
                return groupList;
            }
            IList<Group> groups = LoadAll().Where(r => r.HotelID == hotelID).ToList();
            groupList.Add(groups[0]);

            IList<Group> subGroups = LoadAll().Where(r => r.Parent == groups[0]).OrderBy(o => o.Sort).ToList();

            foreach (Group subGroup in subGroups)
            {
                foreach (Group subGroup1 in GetGroupList(subGroup))
                {
                    groupList.Add(subGroup1);
                }
            }

            return groupList;
        }

        public GroupMenu GetGroupMenu(int id, int hotelID)
        {
            GroupMenu groupMenu = null;

            using (IStatelessSession session = SessionFactory.OpenStatelessSession())
            using (IDbCommand command = session.Connection.CreateCommand())
            {
                command.CommandType = CommandType.Text;
                command.CommandText = "SELECT ID, ParentID, Name, Sort FROM tb_Groups WHERE ID=@ID AND HotelID=@HotelID";

                IDbDataParameter parameter = command.CreateParameter();
                parameter.DbType = DbType.Int32;
                parameter.ParameterName = "@ID";
                parameter.Value = id;
                command.Parameters.Add(parameter);

                parameter = command.CreateParameter();
                parameter.DbType = DbType.Int32;
                parameter.ParameterName = "@HotelID";
                parameter.Value = hotelID;
                command.Parameters.Add(parameter);

                using (IDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        groupMenu = new GroupMenu();
                        groupMenu.ID = reader.GetInt32(0);
                        groupMenu.Name = reader.GetString(2);
                        groupMenu.Sort = reader.GetInt32(3);

                        IList<GroupMenu> subGroupMenus = GetGroupMenuListByParentID(groupMenu.ID);
                        foreach (var subGroupMenu in subGroupMenus)
                        {
                            groupMenu.SubGroupMenus.Add(subGroupMenu);
                        }
                    }
                }
            }

            return groupMenu;
        }

		public IList<GroupMenu> GetGroupMenuListByParentID(int? parentId)
		{
			IList<GroupMenu> groupMenus = new List<GroupMenu>();

			using (IStatelessSession session = SessionFactory.OpenStatelessSession())
			using (IDbCommand command = session.Connection.CreateCommand())
			{
				command.CommandType = CommandType.Text;

				if (parentId.HasValue)
				{
					command.CommandText = "SELECT ID, ParentID, Name, Sort FROM tb_Groups WHERE ParentID=@ParentID ORDER BY Sort";

					IDbDataParameter parameter = command.CreateParameter();
					parameter.DbType = DbType.Int32;
					parameter.ParameterName = "@ParentID";
					parameter.Value = parentId.Value;
					command.Parameters.Add(parameter);
				}
				else 
				{
					command.CommandText = "SELECT ID, ParentID, Name, Sort FROM tb_Groups WHERE ParentID IS NULL ORDER BY Sort";
				}

				using (IDataReader reader = command.ExecuteReader())
				{
					while (reader.Read())
					{
						var groupMenu = new GroupMenu();
						groupMenu.ID = reader.GetInt32(0);
						groupMenu.Name = reader.GetString(2);
						groupMenu.Sort = reader.GetInt32(3);

						IList<GroupMenu> subGroupMenus = GetGroupMenuListByParentID(groupMenu.ID);
						foreach (var subGroupMenu in subGroupMenus)
						{
							groupMenu.SubGroupMenus.Add(subGroupMenu);
						}

						groupMenus.Add(groupMenu);
					}
				}
			}

			return groupMenus;
		}
	}
}
