using APT.BaseData.Domain.ApiModel;
using APT.BaseData.Domain.Entities;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices;
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

using APT.Utility;namespace APT.PF.WebApiControllers.Api.PF
{
	[Route("api/PF/PageChart")]
	[APT.Infrastructure.Api.RootOrg]
	public class PageChartController : AuthorizeApiController<T_PF_PAGE_CHART>
	{
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("Entities")]
		public JsonActionResult<IEnumerable<T_PF_PAGE_CHART>> Entities([FromBody]KeywordFilter filter)
		{
			return WitEntities(null, filter);
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("Paged")]
		public PagedActionResult<T_PF_PAGE_CHART> Paged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitPaged(null, pageFilter);
		}
		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="pageFilter"></param>
		/// <returns></returns>
		[HttpPost, Route("OrderPaged")]
		public PagedActionResult<T_PF_PAGE_CHART> OrderPaged([FromBody]KeywordPageFilter pageFilter)
		{
			return WitOrderPaged(null, pageFilter);
		}

		[HttpPost, Route("Update")]
		public JsonActionResult<bool> Update([FromBody]T_PF_PAGE_CHART entity)
		{
			return SafeExecute<bool>(() =>
			{
				
				this.UpdateEntity(entity);
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.PageChart, entity.ID.ToString());
				return true;

			});

		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpGet, Route("Delete")]
		public JsonActionResult<bool> Delete(string id)
		{
			return SafeExecute<bool>(() =>
			{
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.PageChart, id);
				List<Guid> ids = new List<Guid>();
				ids.Add(new Guid(id));
				this.DoDelete(ids);
				return true;
			});
		}


		private void DoDelete(List<Guid> ids)
		{
			this.DeleteEntity<T_PF_PAGE_CHART_PARAM>(t => ids.Contains(t.PAGE_CHART_ID));
			this.DeleteEntity<T_PF_PAGE_CHART>(t => ids.Contains(t.ID));
		}

		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpGet, Route("BatchDelete")]
		public JsonActionResult<bool> BatchDelete(string ids)
		{
			return SafeExecute<bool>(() =>
			{
				var formService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IPFFormService>();
				formService.CreateFormConfigVersion(PFFormConfigVersionEnum.PageChart, ids);
				var arrays = string.IsNullOrEmpty(ids) ? null : ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
						.Select(t => new Guid(t)).ToList();
				if (arrays != null && arrays.Count > 0)
					this.DoDelete(arrays);
				return true;
			});
		}
		/// <summary>
		/// 根据ID获得实体数据
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost, Route("Get")]
		public JsonActionResult<T_PF_PAGE_CHART> Get([FromBody] KeywordFilter filter)
		{
			return WitEntity(null, filter);
		}

		/// <summary>
		/// 所有表
		/// </summary>
		/// <param name="filter"></param>
		/// <returns></returns>
		[HttpPost, Route("GetChartPageConfigInfo")]
		public JsonActionResult<ChartPageModel> GetChartPageConfigInfo([FromBody] KeywordFilter filter)
		{
			return SafeExecute<ChartPageModel>(() =>
			{
				string formCode = filter.Keyword;
				string formId = filter.Parameter1;
				filter.OrgType = FilterOrgTypeEnum.仅本组织;
				Expression<Func<T_PF_FORM, bool>> expression = t => true;
				T_PF_FORM form = null;
				if (!string.IsNullOrEmpty(formCode))
					form = this.GetEntityByRedis<T_PF_FORM>(formCode, filter.GetOrgId());
				else if (!string.IsNullOrEmpty(formId))
					form = this.GetEntityByRedis<T_PF_FORM>(formId, filter.GetOrgId());
				else
					return null;
				if (form == null)
					return null;
				ChartPageModel model = new ChartPageModel();
				model.Nav_PageForm = form;
				//
				var pageChars = this.GetEntitiesByRedis<T_PF_PAGE_CHART>(filter);
				pageChars = pageChars.Where(t => t.PAGE_FORM_ID == form.ID).ToList();

				var pageCharParams = this.GetEntitiesByRedis<T_PF_PAGE_CHART_PARAM>(filter);
				foreach(var ch in pageChars)
                {
					ch.Nav_Params= pageCharParams.Where(t => t.PAGE_CHART_ID == ch.ID).ToList();
				}
				model.Nav_PageCharts = pageChars;

				//model.Nav_PageCharts = this.GetEntities<T_PF_PAGE_CHART>(t => t.PAGE_FORM_ID == form.ID,
				//		new BaseFilter(), new string[] { "Nav_Params" }).OrderBy(t => t.NUM).ToList();
				return model;
			});
		}

		[HttpPost, Route("GetChartDataTest")]
		public JsonActionResult<ChartDataModel> GetChartDataTest([FromBody] KeywordFilter filter)
		{
			return SafeExecute<ChartDataModel>(() =>
			{
				//ChartDataModel model = new ChartDataModel();

				////model.XAxis
				//ChartDataXAxisModel xAxisModel = new ChartDataXAxisModel();
				//xAxisModel.Data.Add("北京");
				//xAxisModel.Data.Add("上海");
				//xAxisModel.Data.Add("广东");
				//xAxisModel.Data.Add("南京");
				//xAxisModel.Data.Add("天津");
				//model.XAxis.Add(xAxisModel);

				//for (int i = 0; i < 5; i++)
				//{
				//	ChartDataDetailModel detail = new ChartDataDetailModel();
				//	detail.Name = "测试" + (i + 1);

				//	for (int j = 0; j < xAxisModel.Data.Count; j++)
				//	{
				//		ChartDataValueModel value = new ChartDataValueModel();
				//		var r = new Random(Guid.NewGuid().GetHashCode());
				//		value.Value = r.Next(0, 900);
				//		if (j == 0)
				//		{
				//			value.Heads.Add("天气");
				//			value.Heads.Add("天数");
				//			value.Heads.Add("比率");
				//			List<string> xx1 = new List<string>();
				//			xx1.Add("晴天");
				//			xx1.Add("201");
				//			xx1.Add("10%");
				//			value.Data.Add(xx1);
				//			List<string> xx2 = new List<string>();
				//			xx2.Add("雨天");
				//			xx2.Add("202");
				//			xx2.Add("12%");
				//			value.Data.Add(xx2);
				//			List<string> xx3 = new List<string>();
				//			xx3.Add("雾天");
				//			xx3.Add("203");
				//			xx3.Add("13%");
				//			value.Data.Add(xx3);

				//		}

				//		detail.Data.Add(value);
				//	}
				//	model.Data.Add(detail);
				//}


				//return model;


                //多个Y轴

                ChartDataModel model = new ChartDataModel();

                //model.XAxis
                ChartDataXAxisModel xAxisModel = new ChartDataXAxisModel();
                xAxisModel.Data.Add("北京");
                xAxisModel.Data.Add("上海");
                xAxisModel.Data.Add("广东");
                xAxisModel.Data.Add("南京");
                xAxisModel.Data.Add("天津");
                model.XAxis.Add(xAxisModel);

                ChartDataYAxisModel y = new ChartDataYAxisModel();
                y.Name = "测试1";
                y.Max = 1800;
                model.YAxis.Add(y);

                ChartDataYAxisModel y1 = new ChartDataYAxisModel();
                y1.Name = "测试2";
                y1.Max = 100;
                model.YAxis.Add(y1);

                ChartDataDetailModel detail = new ChartDataDetailModel();
                detail.Name = "测试1组1";

                for (int j = 0; j < xAxisModel.Data.Count; j++)
                {
                    ChartDataValueModel value = new ChartDataValueModel();
                    var r = new Random(Guid.NewGuid().GetHashCode());
                    value.Value = r.Next(0, 900);
                    detail.Data.Add(value);
                }
                model.Data.Add(detail);


                ChartDataDetailModel detail1 = new ChartDataDetailModel();
                detail1.Name = "测试1组2";
                //detail1.YAxisIndex = 1;

                for (int j = 0; j < xAxisModel.Data.Count; j++)
                {
                    ChartDataValueModel value = new ChartDataValueModel();
                    var r = new Random(Guid.NewGuid().GetHashCode());
                    value.Value = r.Next(0, 900);

                    detail1.Data.Add(value);
                }
                model.Data.Add(detail1);

				ChartDataDetailModel detail11 = new ChartDataDetailModel();
				detail11.Name = "测试1组11";
				//detail1.YAxisIndex = 1;
				detail11.Stack = "广告类";
				for (int j = 0; j < xAxisModel.Data.Count; j++)
				{
					ChartDataValueModel value = new ChartDataValueModel();
					var r = new Random(Guid.NewGuid().GetHashCode());
					value.Value = r.Next(0, 900);

					detail11.Data.Add(value);
				}
				model.Data.Add(detail11);


				ChartDataDetailModel detail12 = new ChartDataDetailModel();
				detail12.Name = "测试1组12";
				//detail1.YAxisIndex = 1;
				detail12.Stack = "广告类";
				for (int j = 0; j < xAxisModel.Data.Count; j++)
				{
					ChartDataValueModel value = new ChartDataValueModel();
					var r = new Random(Guid.NewGuid().GetHashCode());
					value.Value = r.Next(0, 900);

					detail12.Data.Add(value);
				}
				model.Data.Add(detail12);



				ChartDataDetailModel detail2 = new ChartDataDetailModel();
                detail2.Name = "测试2组1";
                detail2.YAxisIndex = 1;
				detail2.ChartType =(int)PFChartTypeEnum.line;

				for (int j = 0; j < xAxisModel.Data.Count; j++)
                {
                    ChartDataValueModel value = new ChartDataValueModel();
                    var r = new Random(Guid.NewGuid().GetHashCode());
                    value.Value = r.Next(0, 100);

                    detail2.Data.Add(value);
                }
                model.Data.Add(detail2);



                return model;
            });
		}


	}
}
