using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Common;
using Domain;
using Service;

namespace WebSite.Controllers
{
	public class HostAirController : BaseController
	{
		private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HostAirController));

		public IHostAirManager HostAirManager { get; set; }

		public IHostManager HostManager { get; set; }

		#region Action

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

		[Authorize]
		public ActionResult LoadHostAirs(int hostID)
		{
			var list = HostAirManager.LoadByHostID(hostID);

			var result = list.Select(r => new {
				r.HostID,
				ModalTypeID = r.Modal.ModalType.ID,
				CustomerName = (Language == Language.CN ? r.Modal.CustomerName : r.Modal.EnglishName),
				r.No,
				r.OnOff,
				r.CurrentTemp,
				r.SettingTemp,
				r.Speed,
				r.KeepTemp,
				r.InitTemp,
				r.HightTemp,
				r.LowerTemp,
				r.DeadTemp,
				r.HotDevition,
				r.ColdDevition,
				r.WelcomeTime,
				r.IsLockTemp,
				r.LockTemp,
				r.Mode,
				Valve = HttpContext.InnerLanguage(r.Valve.ToString()),
				r.ControlType,
				ControlTypeName = HttpContext.InnerLanguage(r.ControlType.ToString() + "Control"),
				r.CompensatoryTemp,
				r.RelateRoomStatus,
				r.RelateDoorContact,
				r.ColdHotMode,
				r.ColdHotSwitchDelayTime,
				r.FanStop,
				r.DisableFanHighSpeed,
				r.SleepFlag,
				r.SleepStartTime,
				r.SleepEndTime,
				r.SleepDevition,
				r.TimeFlag,
				r.TimeStartTime1,
				r.TimeEndTime1,
				r.TimeStartTime2,
				r.TimeEndTime2,
				r.TimeStartTime3,
				r.TimeEndTime3
			});

			return Json(result, JsonRequestBehavior.AllowGet);
		}

		[Authorize]
		public ActionResult Save(string jsonData)
		{
			try
			{
				IDictionary<string, string> data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonData);
				if (data == null)
				{
                    throw new ApplicationException(HttpContext.InnerLanguage("PleaseChooseAirConditioning"));
				}

				if (!data.ContainsKey("HostID"))
				{
					throw new ApplicationException(HttpContext.InnerLanguage("InvalidRoom"));
				}

				var host = HostManager.Get(Convert.ToInt32(data["HostID"]));
				if (host == null)
				{
					throw new ApplicationException(HttpContext.InnerLanguage("InvalidRoom"));
				}

				if (!host.Status)
				{
					string message = String.Format(HttpContext.InnerLanguage("SendFailedHostOffline"), host.RoomNumber);
					return Json(new { IsSuccess = false, Message = message });
				}

				if (host.RoomCard != null)
				{
                    throw new ApplicationException(HttpContext.InnerLanguage("ThisRoomIsInTheCardAndCanNotSetAirConditioningInformation"));
				}

				int modalTypeId = 0;
				if (!data.ContainsKey("ModalTypeID") || !int.TryParse(data["ModalTypeID"], out modalTypeId))
				{
                    throw new ApplicationException(HttpContext.InnerLanguage("InvalidAirConditioning"));
				}

				var hostAir = HostAirManager.GetByModalTypeID(host.ID, modalTypeId);
				if (hostAir == null)
				{
                    throw new ApplicationException(HttpContext.InnerLanguage("InvalidAirConditioning"));
				}

				hostAir.SettingTemp = Convert.ToInt32(data["SettingTemp"]);
				hostAir.CompensatoryTemp = Convert.ToSingle(data["CompensatoryTemp"]);
				hostAir.Speed = Convert.ToInt32(data["Speed"]);
				hostAir.Mode = Convert.ToInt32(data["Mode"]);
				hostAir.IsLockTemp = Convert.ToBoolean(data["IsLockTemp"]);
				hostAir.LockTemp = Convert.ToInt32(data["LockTemp"]);

				hostAir.KeepTemp = Convert.ToInt32(data["KeepTemp"]);
				hostAir.InitTemp = Convert.ToInt32(data["InitTemp"]);
				hostAir.HightTemp = Convert.ToInt32(data["HightTemp"]);
				hostAir.LowerTemp = Convert.ToInt32(data["LowerTemp"]);
				hostAir.ColdHotSwitchDelayTime = Convert.ToInt32(data["ColdHotSwitchDelayTime"]);
				hostAir.ColdHotMode = Convert.ToInt32(data["ColdHotMode"]);
				hostAir.DeadTemp = Convert.ToInt32(data["DeadTemp"]);
				hostAir.HotDevition = Convert.ToInt32(data["HotDevition"]);
				hostAir.ColdDevition = Convert.ToInt32(data["ColdDevition"]);
				hostAir.WelcomeTime = Convert.ToInt32(data["WelcomeTime"]);
				hostAir.RelateRoomStatus = Convert.ToBoolean(data["RelateRoomStatus"]);
				hostAir.RelateDoorContact = Convert.ToBoolean(data["RelateDoorContact"]);
				hostAir.FanStop = Convert.ToBoolean(data["FanStop"]);
				hostAir.DisableFanHighSpeed = Convert.ToBoolean(data["DisableFanHighSpeed"]);

				hostAir.SleepFlag = false;
				hostAir.SleepStartTime = "00:00";
				hostAir.SleepEndTime = "00:00";
				hostAir.SleepDevition = 0;

				hostAir.TimeFlag = false;
				hostAir.TimeStartTime1 = "00:00";
				hostAir.TimeEndTime1 = "00:00";
				hostAir.TimeStartTime2 = "00:00";
				hostAir.TimeEndTime2 = "00:00";
				hostAir.TimeStartTime3 = "00:00";
				hostAir.TimeEndTime3 = "00:00";

				HostAirManager.Update(hostAir);

                return Json(new { IsSuccess = true, Message = HttpContext.InnerLanguage("SentSuccessfully") });
			}
			catch (Exception ex)
			{
				if (logger.IsErrorEnabled)
				{
                    logger.Error(HttpContext.InnerLanguage("SendAirconditioningSettingsFailed"), ex);
				}

                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("SendFailedBecause") + ex.Message });
			}
		}

		#endregion
	}
}
