using AspNetFrameworkMVC.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;

namespace AspNetFrameworkMVC.Controllers
{
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			return View();
		}

		public ActionResult About()
		{
			ViewBag.Message = "Your application description page.";

			return View();
		}

		public ActionResult Contact()
		{
			ViewBag.Message = "Your contact page.";

			return View();
		}

		public ActionResult NewGuid()
		{
			string guid = Guid.NewGuid().ToString();

			return Content(guid);
		}

		[HttpPost]
		public HttpResponseMessage UploadSample()
		{
			HttpRequestBase req = HttpContext.Request;

			if (req.Files.Count < 1)
				return new HttpResponseMessage(HttpStatusCode.BadRequest);

			foreach (string file in req.Files)
			{
				HttpPostedFileBase uploadFile = req.Files[file];
				string outputFilePath = HttpContext.Server.MapPath($"~/Uploads/{uploadFile.FileName}");
				uploadFile.SaveAs(outputFilePath);
			}

			return new HttpResponseMessage(HttpStatusCode.OK);
		}

		[HttpGet]
		public FileResult DownloadSample()
		{
			string filename = Request.QueryString["filename"];
			string filePath = HttpContext.Server.MapPath($"~/Uploads/{filename}");

			byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
			return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Zip, Path.GetFileName(filePath));
		}

		public ActionResult CheckLogin()
		{
			string filename = Request.QueryString["username"];
			string password = Request.QueryString["password"];

			bool success = LoginManager.CheckLogin(filename, password);
			ViewBag.Success = success;
			return View();
		}
	}
}