using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain;
using Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace WebSite.Controllers
{
    public class OverviewController : BaseController
	{
		public IHostManager HostManager { get; set; }
        public IOverviewManager OverviewManager { get; set; }
        public IHostFaultRecordsManager HostFaultRecordsManager { get; set; }

		#region Action

		/// <summary>
		/// 回路状态记录
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public ActionResult HostModalRecords()
		{
			return View();
		}
		/// <summary>
		/// 获取回路状态记录
        /// 
        /// 
        /// 严重怀疑这里的调用有问题
		/// </summary>
		/// <param name="page"></param>
		/// <param name="rows"></param>
		/// <param name="order"></param>
		/// <param name="sort"></param>
		/// <param name="roomNumber"></param>
		/// <param name="startTime"></param>
		/// <param name="endTime"></param>
		/// <param name="modalIds"></param>
		/// <returns></returns>
		public ActionResult LoadHostModalRecords(int? page, int? rows, string order, string sort, string roomNumber, string startTime, string endTime, DeviceType? deviceType, string modalIds)
		{
			long total = 0;
			var list = OverviewManager.LoadHostModalRecords(out total, 1, 100, order, sort, roomNumber, startTime, endTime, deviceType, modalIds, CurrentHotelID);
			var result = new { total = total, rows = list };
			return Content(JsonConvert.SerializeObject(result, new DataTableConverter()));
		}
        /// <summary>
        /// 回路异常记录
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult HostAbnormalRecords()
        {
            return View();
        }
        /// <summary>
        /// 获取异常类型
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadAbnormalType()
        {
            IList<object> result = new List<object>();
            result.Add(new { Value = 1, Name = "门磁超过2天无开关记录（表示门磁已坏）" });
            result.Add(new { Value = 2, Name = "已取电，10小时内红外无打开记录（表示红外已坏）" });
            result.Add(new { Value = 3, Name = "已取电，10秒内门磁无开关记录（表示取电异常）" });

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取回路异常记录
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="order"></param>
        /// <param name="sort"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public ActionResult LoadHostAbnormalRecords(int? page, int? rows, string order, string sort, int type)
        {
            //严重怀疑这里的代码有问题
            long total = 0;
            var list = OverviewManager.LoadHostAbnormalRecords(type, CurrentHotelID);
            var result = new { total = total, rows = list };
            return Content(JsonConvert.SerializeObject(result, new DataTableConverter()));
        }
        /// <summary>
        /// 外设异常记录
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult HostFaultRecords()
        {
            return View();
        }
        /// <summary>
        /// 获取存在外设异常的房号
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadRoomNumbers()
        {
            IList<object> result = new List<object>();
            IList<string> roomNumbers = HostFaultRecordsManager.LoadRoomNumbers(CurrentHotelID);
            foreach (string roomNumber in roomNumbers)
            {
                result.Add(new { Value = roomNumber, Name = roomNumber });
            }
            result.Insert(0, new { Value = "", Name = HttpContext.InnerLanguage("CheckAll") });

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取回路异常记录
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="order"></param>
        /// <param name="sort"></param>
        /// <param name="roomNumber"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="deviceType"></param>
        /// <param name="modalIds"></param>
        /// <returns></returns>
        public ActionResult LoadHostFaultRecords(int? page, int? rows, string order, string sort, string roomNumber, string startTime, string endTime, DeviceType? deviceType, string modalIds)
        {
            long total = 0;
            var list = HostFaultRecordsManager.LoadHostFaultRecords(out total, 1, 100, order, sort, roomNumber, startTime, endTime, deviceType, modalIds, CurrentHotelID);
            var result = new { total = total, rows = list };
            return Content(JsonConvert.SerializeObject(result, new DataTableConverter()));
        }

		[Authorize]
		public ActionResult Index()
        {
			return View("SimonIndex");
		}

		/// <summary>
		/// 加载RCU联网通讯状况
		/// </summary>
		/// <returns></returns>
        [Authorize]
        public ActionResult LoadRCUState()
        {
            var list = HostManager.LoadAll(CurrentHotelID);
            var state = new
            {
                Online = list.Count(r => r.Status == true),
                Offline = list.Count(r => r.Status == false),
                Total = list.Count()
            };
            return Json(state, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 加载客房房门开关状况
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult LoadDoorState()
        {
            return Json(OverviewManager.LoadDoorState(CurrentHotelID), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 加载空调运行状况
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult LoadAirConditionState()
        {
            return Json(OverviewManager.LoadAirConditionState(CurrentHotelID), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 加载客房出租状况
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult LoadRoomRentState()
        {
            return Json(OverviewManager.LoadRoomRentState(CurrentHotelID), JsonRequestBehavior.AllowGet);
        }

		/// <summary>
		/// 加载正在请求的服务状况
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public ActionResult LoadRoomServiceState()
		{
			return Json(OverviewManager.LoadRoomServiceState(), JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// 加载保险箱状况
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public ActionResult LoadSafeBoxState()
		{
            return Json(OverviewManager.LoadSafeBoxState(), JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// 加载客房状况
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public ActionResult LoadRoomState()
		{
			return Json(OverviewManager.LoadRoomState(), JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// 加载客房窗门状况
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public ActionResult LoadWindowState()
		{
			var state = new
			{

			};

			return Json(state, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// 加载客房阳台门状况
		/// </summary>
		/// <returns></returns>
		[Authorize]
		public ActionResult LoadBalconyDoorState()
		{
			var state = new
			{
			};

			return Json(state, JsonRequestBehavior.AllowGet);
		}

		#endregion

	}
}
