using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using DebriefingSoftware.Common.Business.Filtering;
using DebriefingSoftware.Web.PresentationObjects;
using System.Web;

[ServiceKnownType(typeof(Distributor))]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single)]
public class GridViewService : IGridViewService
{
	public Distributor.GridData GetDistributorDataAndCount(int startIndex, int maximumRows, string sortExpressions, string filterExpressions)
	{
		var gridData = new Distributor.GridData();

		var jsonString = Uri.UnescapeDataString(filterExpressions);
		if (!string.IsNullOrEmpty(jsonString))
		{
			var parameters = Filter.GetFilterParameters(jsonString).ToList();
			Filter.Current.AddParameters(parameters);
		}
		/*
		Filter.Current.AddParameters(new List<FilterParameter>
											{
												new FilterParameter
													{
														FieldNames = new[] { "DSWIZARD.COMPANY.PARENT_ID" },
														FieldValues = new[] { CompanyController.CurrentCompany.ID.ToString() },
														DataType = SqlDataType.Integer,
														OperationType = SqlOperationType.EqualTo,
														Group = "Distributor"
													}
											});
		*/

		/*
		gridData.Data = (from companyInfo in controller.LoadForView(startIndex, maximumRows, sortExpressions)
						 select new Distributor.GridInfo
									{
										ID = companyInfo.ID,
										Name = companyInfo.Name,
										ContactEmail = companyInfo.Contact.Email
									}).ToList();*/

		var gridInfoList = new List<Distributor.GridInfo>
		                   	{
		                   		new DebriefingSoftware.Web.PresentationObjects.Distributor.GridInfo {ID = 0, ContactName = "ivan@ivan.com", Name = string.Format("<span class='bold'>{0:N1}</span>", 3.4564d), ZZZName = "Ivanov" },
		                   		new DebriefingSoftware.Web.PresentationObjects.Distributor.GridInfo {ID = 1, ContactName = "sasha@sasha.com", Name = "Sasha", ZZZName = "Sashev"},
		                   		new DebriefingSoftware.Web.PresentationObjects.Distributor.GridInfo {ID = 2, ContactName = "dmitry@dmitry.com", Name = "Dmitry", ZZZName = "Dmitriev"},
		                   		new DebriefingSoftware.Web.PresentationObjects.Distributor.GridInfo {ID = 3, ContactName = "yana@yana.com", Name = "Yana", ZZZName = "Yanova"}
		                   	};

		var resultInfoList = (from gridInfo in gridInfoList select gridInfo).Skip(startIndex).Take(maximumRows).ToList();

		gridData.Data = resultInfoList;
		gridData.Count = gridInfoList.Count;
		//gridData.Count = controller.GetRecordCount();

		Filter.Current.Clear();

		HttpContext.Current.Session.Add(GridEntitySessionData.Distributor.ToString(), gridData.Data);

		return gridData;
	}
}