using Microsoft.AspNet.Identity.EntityFramework;
using Sevomin.Models;
using Sevomin.Models.Helpers;
using System.IO;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Sevomin.WebFrontend.Controllers
{
    [Authorize]
    public class AuthorizedController : BaseController
    {
        public AuthorizedController()
            : this(new SevominUserManager(new UserStore<User>(SevominDbContext.Current)))
        {
        }

        public AuthorizedController(SevominUserManager userManager)
        {
            UserManager = userManager;
        }

        public SevominUserManager UserManager { get; private set; }

        public async Task<ActionResult> GetResume(string userid = "", bool english = false)
        {
            if (string.IsNullOrWhiteSpace(userid))
                userid = (await UserManager.FindByNameAsync(User.Identity.Name)).Id;
            Dovomin user = await UserManager.FindByIdAsync(userid) as Dovomin;

            if (user == null)
                return HttpNotFound();
            //ToDo: فکر کنم هر کسی که لاگ این کرده باشه میتونه رزومه هر کسی را دانلود کنه
            string path = Path.Combine(Server.MapPath("~/App_Data/resumes/"), english ? user.EnglishResume : user.PersianResume);
            string fileDownloadName = string.Format("{0} {1} Resume{2}", user.DisplayName, english ? "English" : "Persian", Path.GetExtension(path));
            return File(path, "application/octet-stream", fileDownloadName);
        }
			
    }
}
