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

namespace Dao.Implement
{
    public class HostModalRecordRepository : RepositoryBase<HostModalRecord>, IHostModalRecordRepository
    {
        public IQueryable<HostModalRecord> LoadByHostID(int hostID, string modalAddress = "")
        {
            if (string.IsNullOrEmpty(modalAddress))
            {
                return LoadAll().Where(r => r.HostID == hostID);
            }
            else
            {
                return LoadAll().Where(r => r.HostID == hostID && r.ModalAddress == modalAddress);
            }
        }

        public DataTable LoadHostModalRecords(out long total, int page, int rows, string order, string sort, string roomNumber, string deviceTypes, string deviceSubtypes, string startTime, string endTime, int hotelID)
        {
            total = 0;

            IDbCommand cmd = Session.Connection.CreateCommand();

            cmd.CommandText = "QueryHostModalRecords2";
            cmd.CommandType = CommandType.StoredProcedure;

            IDbDataParameter parameter = cmd.CreateParameter();
            parameter.ParameterName = "@total";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@page";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = page;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@rows";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = rows;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@order";
            parameter.DbType = DbType.String;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = order;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@sort";
            parameter.DbType = DbType.String;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = sort;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@roomNumber";
            parameter.DbType = DbType.String;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = roomNumber;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@deviceTypes";
            parameter.DbType = DbType.String;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = deviceTypes;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@deviceSubtypes";
            parameter.DbType = DbType.String;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = deviceSubtypes;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@startTime";
            parameter.DbType = DbType.String;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = startTime;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@endTime";
            parameter.DbType = DbType.String;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = endTime;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@hotelID";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = hotelID;
            cmd.Parameters.Add(parameter);

            IDbDataAdapter da = new SqlDataAdapter(cmd as SqlCommand);

            DataSet ds = new DataSet();

            da.Fill(ds);

            parameter = cmd.Parameters["@total"] as IDbDataParameter;

            total = (parameter != null && parameter.Value != null && parameter.Value != DBNull.Value) ? Convert.ToInt64(parameter.Value) : 0;

            return ds.Tables[0];
        }

        public HostModalRecord GetLastHostModalRecord(int hostId, int roomTypeModalId)
        {
            return LoadAll().FirstOrDefault(r => r.HostID == hostId && r.RoomTypeModalID == roomTypeModalId && r.EndTime == null);
            //return LoadAll().Where(r => r.HostID == hostId && r.RoomTypeModalID == roomTypeModalId && r.EndTime == null).ToList().FirstOrDefault();
        }

        public IList<string> LoadRoomNumbers(int hotelID)
        {
            IList<string> roomNumbers = new List<string>();

            IDbCommand cmd = Session.Connection.CreateCommand();

            cmd.CommandText = "SELECT DISTINCT hmr.RoomNumber FROM tb_HostModalRecords_bak hmr LEFT JOIN tb_Hosts h ON hmr.HostID=h.ID WHERE h.HotelID=" + hotelID + " ORDER BY hmr.RoomNumber";
            cmd.CommandType = CommandType.Text;
            using (IDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    roomNumbers.Add(reader["RoomNumber"].ToString());
                }
            } 
            cmd = Session.Connection.CreateCommand();
            cmd.CommandText = "SELECT DISTINCT hmr.RoomNumber FROM tb_HostModalRecords hmr LEFT JOIN tb_Hosts h ON hmr.HostID=h.ID WHERE h.HotelID=" + hotelID + " ORDER BY hmr.RoomNumber";
            cmd.CommandType = CommandType.Text;
            using (IDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    string roomNumber = reader["RoomNumber"].ToString();
                    if (!roomNumbers.Contains(roomNumber))
                    {
                        roomNumbers.Add(roomNumber);
                    }
                }
            }
            return roomNumbers;
        }

        public DataTable LoadModals(string roomNumber, int hotelID)
        {
            IList<string> roomNumbers = new List<string>();

            IDbCommand cmd = Session.Connection.CreateCommand();

            cmd.CommandText = @"WITH HostModals AS 
								(
									SELECT DISTINCT RoomTypeID,ModalAddress 
									FROM tb_HostModalRecords
									WHERE RoomNumber=@roomNumber
								)
								SELECT c.Name 'RoomType',b.ModalAddress,b.Outlet,b.Name,b.EnglishName,b.TWName 
								FROM HostModals a
								INNER JOIN tb_RoomTypeModal b ON a.RoomTypeID=b.RoomTypeID AND a.ModalAddress=b.ModalAddress
								INNER JOIN tb_RoomType c ON c.ID=b.RoomTypeID WHERE c.HotelID=@hotelID";

            cmd.CommandType = CommandType.Text;

            var parameter = cmd.CreateParameter();
            parameter.ParameterName = "@roomNumber";
            parameter.DbType = DbType.String;
            parameter.Value = roomNumber;
            cmd.Parameters.Add(parameter);

            parameter = cmd.CreateParameter();
            parameter.ParameterName = "@hotelID";
            parameter.DbType = DbType.Int32;
            parameter.Direction = ParameterDirection.Input;
            parameter.Value = hotelID;
            cmd.Parameters.Add(parameter);

            IDbDataAdapter da = new SqlDataAdapter((SqlCommand)cmd);

            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
    }
}
