using System.Web.Mvc;
using wijmoSample1.Models.FileExplorer;
using ActionResult = System.Web.Mvc.ActionResult;
using System.Collections.Specialized;

namespace wijmoSample1.Controllers
{
	public class FileExplorerController : Controller
	{
		public ActionResult Overview()
		{
			return View();
		}

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

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

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

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

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

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

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

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

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

		public ActionResult Action()
		{
			var actionParam = ParseActionParam(HttpContext.Request.QueryString);
			var result = FileExplorerAction.Process(actionParam);
			return new DataContractJsonResult<Models.FileExplorer.ActionResult>(result, new [] { typeof(FileExplorerItem), typeof(ItemOperationResult) });
		}

		private static ActionParam ParseActionParam(NameValueCollection param)
		{
			return new ActionParam
			{
				CommandName = param["commandName"].SafeParseEnum<FileCommand>(),
				Path = param["path"],
				SourcePaths = param["sourcePaths[]"].SafeParseStrings(),
				SearchPatterns = param["searchPatterns"].SafeParseStrings(),
				FilterExpression = param["filterExpression"],
				SortExpression = param["SortExpression"].SafeParseEnum<SortField>(),
				SortDirection = param["SortDirection"].SafeParseEnum<System.Web.UI.WebControls.SortDirection>(),
				PageSize = param["PageSize"].SafeParseInt(),
				PageIndex = param["PageIndex"].SafeParseInt(),
			};
		}

		private FileExplorerAction _action;
		private FileExplorerAction FileExplorerAction
		{
			get
			{
				return _action ?? (_action = new FileExplorerAction());
			}
		}
	}
}