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

namespace WebSite.Controllers
{
    public class AirConditionStatusController : BaseController
    {
        public IHostModalRecordManager HostModalRecordManager { get; set; }

        public IHostAirRecordManager HostAirRecordManager { get; set; }

        #region Actions

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LoadAirNos()
        {
            var result = new List<object>();

            //result.Add(new { Value = DeviceType.AirConditioner1, Name = HttpContext.InnerLanguage(DeviceType.AirConditioner1.ToString()) });
            //result.Add(new { Value = DeviceType.AirConditioner2, Name = HttpContext.InnerLanguage(DeviceType.AirConditioner2.ToString()) });
            //result.Add(new { Value = DeviceType.AirConditioner3, Name = HttpContext.InnerLanguage(DeviceType.AirConditioner3.ToString()) });
            //result.Add(new { Value = DeviceType.AirConditioner4, Name = HttpContext.InnerLanguage(DeviceType.AirConditioner4.ToString()) });

            return Json(result);
        }

        public ActionResult LoadDeviceSubtypes()
        {
            var result = new List<object>();

            var enumType = typeof(DeviceSubtype);

            var values = Enum.GetValues(enumType);

            foreach (var value in values)
            {
                string name = Enum.GetName(enumType, value);
                result.Add(new { Value = value, Name = HttpContext.InnerLanguage(name) });
            }

            return Json(new { total = result.Count, rows = result });
        }


        public ActionResult LoadACDeviceSubtypes()
        {
            var result = new List<object>();

            result.Add(new { Value = DeviceSubtype.SpeedHigh, Name = HttpContext.InnerLanguage(DeviceSubtype.SpeedHigh.ToString()) });
            result.Add(new { Value = DeviceSubtype.SpeedMiddle, Name = HttpContext.InnerLanguage(DeviceSubtype.SpeedMiddle.ToString()) });
            result.Add(new { Value = DeviceSubtype.SpeedLow, Name = HttpContext.InnerLanguage(DeviceSubtype.SpeedLow.ToString()) });
            result.Add(new { Value = DeviceSubtype.HotValve, Name = HttpContext.InnerLanguage(DeviceSubtype.HotValve.ToString()) });
            result.Add(new { Value = DeviceSubtype.ColdValve, Name = HttpContext.InnerLanguage(DeviceSubtype.ColdValve.ToString()) });

            return Json(new { total = result.Count, rows = result });
        }


        public ActionResult LoadTempTypes()
        {
            var result = new List<object>();

            var enumType = typeof(TempType);

            var values = Enum.GetValues(enumType);

            foreach (var value in values)
            {
                string name = Enum.GetName(enumType, value);
                result.Add(new { Value = value, Name = HttpContext.InnerLanguage(name) });
            }

            return Json(new { total = result.Count, rows = result });
        }

        /// <summary>
        /// 查询空调记录
        /// </summary>
        /// <param name="roomNumber">房号</param>
        /// <param name="airNo">空调序号</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">截止时间</param>
        /// <param name="airRecordtype">空调记录查询类型：AirCircuit/空调回路，RoomTemp/客房温度</param>
        /// <param name="deviceSubtypes">空调回路类型</param>
        /// <param name="tempTypes">温度类型</param>
        /// <returns></returns>
        public ActionResult LoadHostAirRecords(int page, int rows, string order, string sort, string roomNumber, int airNo, string startTime, string endTime, string airRecordtype, string deviceSubtypes, string tempTypes)
        {
            try
            {
                long total = 0;

                if (airRecordtype == "AirCircuit")
                {
                    var table = HostModalRecordManager.LoadHostModalRecords(out total, page, rows, order, sort, roomNumber, airNo.ToString(), deviceSubtypes, startTime, endTime, CurrentHotelID);

                    table.Columns.Add("SubtypeName", typeof(string));
                    foreach (DataRow dr in table.Rows)
                    {
                        var subtypeName = Enum.GetName(typeof(DeviceSubtype), dr["Subtype"]);
                        dr["SubtypeName"] = HttpContext.InnerLanguage(subtypeName);
                    }

                    return Content(JsonConvert.SerializeObject(new { total = total, rows = table }, new DataTableConverter()));
                }
                else if (airRecordtype == "RoomTemp")
                {
                    var table = HostAirRecordManager.LoadHostAirRecords(out total, page, rows, order, sort, roomNumber, airNo, startTime, endTime, tempTypes, CurrentHotelID);

                    return Content(JsonConvert.SerializeObject(new { total = total, rows = table }, new DataTableConverter()));
                }

                return Json(new { total = 0, rows = new object[0] });
            }
            catch (Exception ex)
            {
                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("LoadDataFailedReason") + ex.Message });
            }
        }

        #endregion
    }
}
