using BLL;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Http;
namespace WebAPI.Controllers
{
    /// 
    /// 
    /// 
    public class VideoController : ApiController
    {
        /// 
        /// 
        /// 
        /// 
        /// 
        public Model.ResponeData getVideoInfor(string projectId)
        {
            Model.ResponeData responeData = new Model.ResponeData();
            try
            {               
                string url =ConfigurationManager.AppSettings["Video_URL"];
                var project = BLL.ProjectService.GetProjectByProjectId(projectId);
                string URLToken = "";
                String restoken = BLL.CommonService.CreateGetHttpResponse(url + "api/v1/login?username=" + project.ProjectCode + "&password=" + Funs.EncryptionPassword(project.MonitorPW));
                Dictionary dicres = JsonConvert.DeserializeObject>(restoken);
                URLToken = dicres["URLToken"];
                List> data = new List>();
                responeData.data = data;
                List urls = new List();
                string res = APIGetHttpService.Http(url + "api/v1/device/channeltree?token=" + URLToken);
                List> jsonNvr = JsonConvert.DeserializeObject>>(res);
                foreach (Dictionary nvr in jsonNvr)
                {
                   
                        string jsonCameras = APIGetHttpService.Http(url + "api/v1/device/channeltree?serial=" + nvr["serial"]+ "&token="+URLToken);
                        data.AddRange(JsonConvert.DeserializeObject>>(jsonCameras));
                    
                }
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
                ErrLogInfo.WriteLog(ex, "WX接口-监控视频", "VideoController.getVideoInfor");
            }
            return responeData;
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public Model.ResponeData getVideoUrl(string projectId, string serial, string code)
        {
            var responeData = new Model.ResponeData();
            try
            {
                string url = ConfigurationManager.AppSettings["Video_URL"];
                var project = BLL.ProjectService.GetProjectByProjectId(projectId);
                string URLToken = "";
                String restoken = BLL.CommonService.CreateGetHttpResponse(url + "api/v1/login?username=" + project.ProjectCode + "&password=" + Funs.EncryptionPassword(project.MonitorPW));
                Dictionary dicres = JsonConvert.DeserializeObject>(restoken);
                URLToken = dicres["URLToken"];
                
                var stmp = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds() + "";
                string jsonCamera = APIGetHttpService.Http(url + "api/v1/stream/start?token="+URLToken+"&serial=" + serial + "&code=" + code + "&_=" + stmp);
                responeData.data = JsonConvert.DeserializeObject>(jsonCamera);
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
                ErrLogInfo.WriteLog(ex, "WX接口-监控视频", "VideoController.getVideoUrl");
            }
            return responeData;
        }
    }
}