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

namespace WebSite.Controllers
{
    public class HostModalController : BaseController
    {
        public IHostManager HostManager { get; set; }
        public IHostModalManager HostModalManager { get; set; }
        public IRoomTypeModalManager RoomTypeModalManager { get; set; }
        public IRoomTypeChannelsManager RoomTypeChannelsManager { get; set; }

        #region Action

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

        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(HostModalController));
        /// <summary>
        /// 加载回路
        /// </summary>
        /// <param name="hostID"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult LoadHostModals(int hostID)
        {
            try
            {
                var host = HostManager.Get(hostID);
                var list1 = HostModalManager.LoadByHostID(hostID);
                if (list1.ToList().Count == 0)//如果没有回路，则根据所属房型，新建回路
                {
                    if (UpdateHostModals(host))
                    {
                        list1 = HostModalManager.LoadByHostID(hostID);
                    }
                }
                var list = list1.Where(r => r.Modal.ActiveIndicator).OrderBy(r => r.Modal.ModalAddress);//只装置标志启动的
                var channelList = RoomTypeChannelsManager.LoadAll(host.RoomType.ID);//获取该主机电视频道
                string strChannels = Newtonsoft.Json.JsonConvert.SerializeObject(channelList.Select(r => new { Code = r.Code, Name = r.Name }));
                //var result = list.Select(r => new
                //{
                //    HostID = r.HostID,
                //    ModalID = r.Modal.ID,
                //    r.Modal.Outlet,
                //    Name = ReturnNameByLanguage(r.Modal.Name, r.Modal.EnglishName, r.Modal.TWName),//(Language == Domain.Language.CN ? r.Modal.Name : r.Modal.EnglishName),
                //    r.Modal.ModalAddress,
                //    Type = r.Modal.Type.ToString(),
                //    TypeName = HttpContext.InnerLanguage(r.Modal.Type.ToString()),
                //    Status = r.Status,
                //    r.Brightness,
                //    r.CurrentTemp,
                //    r.SettingTemp,
                //    r.FanSpeed,
                //    r.Mode,
                //    r.Valve,
                //    Channels = strChannels
                //    //+ (r.Status == 1 ?
                //    //Convert.ToInt32((DateTime.Now - HostModalRecordManager.GetLastHostModalRecord(r.HostID, r.Modal.ID).StartTime).TotalMinutes) : 0)//如果当前灯光处于开的状态，计算截止到当前开的时长
                //});
                var result = list.Select(r => new ResultData()
                {
                    HostID = r.HostID,
                    ModalID = r.Modal.ID,
                    Outlet= r.Modal.Outlet,
                    Name = ReturnNameByLanguage(r.Modal.Name, r.Modal.EnglishName, r.Modal.TWName),//(Language == Domain.Language.CN ? r.Modal.Name : r.Modal.EnglishName),
                    ModalAddress= r.Modal.ModalAddress,
                    Type = r.Modal.Type.ToString(),
                    TypeName = HttpContext.InnerLanguage(r.Modal.Type.ToString()),
                    Status = r.Status,
                    Brightness= r.Brightness,
                    CurrentTemp= r.CurrentTemp,
                    SettingTemp= r.SettingTemp,
                    FanSpeed= r.FanSpeed,
                    Mode= r.Mode,
                    Valve= r.Valve,
                    Channels = strChannels
                }).ToList();



                foreach (var item in result)
                {
                    string KKey = CacheKey.HostModalStatus_Prefix + "_" + item.HostID + "_" + item.ModalAddress;
                    var hostModal = CSRedisCacheHelper.Get_Partition<HostModal_Cache>(KKey);
                    if (hostModal != null)
                    {
                        item.Status = hostModal.Status;
                        item.Brightness = hostModal.Brightness;
                        var aaa = hostModal.AirConditionData;
                        item.CurrentTemp = aaa.CurrentTemp;
                        item.SettingTemp = aaa.SettingTemp;
                        item.FanSpeed = aaa.FanSpeed;
                        item.Mode = aaa.Mode;
                        item.Valve = aaa.Valve;
                    }
                }
                return Json(new { total = result.Count(), rows = result });
            }
            catch (Exception ex)
            {
                return Json(new { IsSuccess = false, Message = HttpContext.InnerLanguage("LoadDataFailedBecause") + ex.Message });
            }
        }

        private bool UpdateHostModals(Host host)
        {
            if (host == null)
            {
                return false;
            }
            HostModalManager.DeteleByHostID(host.ID);
            var list = RoomTypeModalManager.LoadAll().Where(r => r.RoomType == host.RoomType);
            DateTime now = DateTime.Now;
            foreach (var modal in list)
            {
                HostModalManager.Save(new HostModal { HostID = host.ID, Modal = modal, Status = 2, Time = 0, UpdateTime = now });
            }

            return true;
        }

        #endregion
        public class ResultData
        {
            public int HostID { get; set; }
            public int ModalID;
            public string Outlet;
            public string Name; //(Language == Domain.Language.CN ? r.Modal.Name : r.Modal.EnglishName),
            public string ModalAddress;
            public string Type;
            public string TypeName;
            public int Status;
            public int Brightness;
            public int CurrentTemp;
            public int SettingTemp;
            public int FanSpeed;
            public int Mode;
            public int Valve;
            public string Channels;
        }
    }

}
