using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using GanjooRazor.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using RMuseum.Models.Auth.Memory;
using RMuseum.Models.Ganjoor;
using RMuseum.Models.Ganjoor.ViewModels;
namespace GanjooRazor.Pages
{
public class TransModel : PageModel
{
///
/// HttpClient instance
///
private readonly HttpClient _httpClient;
///
/// configration file reader (appsettings.json)
///
private readonly IConfiguration Configuration;
///
/// constructor
///
///
///
public TransModel(HttpClient httpClient, IConfiguration configuration)
{
_httpClient = httpClient;
Configuration = configuration;
}
///
/// poem
///
public GanjoorPoemCompleteViewModel Poem { get; set; }
public GanjoorLanguage[] Languages { get; set; }
public GanjoorPoemTranslationViewModel[] Translations { get; set; }
public GanjoorPoemTranslationViewModel Translation { get; set; }
public string ErrorMessage { get; set; }
public int PoemId { get; set; }
///
/// is logged on
///
public bool CanTranslate { get; set; }
public int LanguageId { get; set; }
public async Task OnGetAsync()
{
if (bool.Parse(Configuration["MaintenanceMode"]))
{
return StatusCode(503);
}
ErrorMessage = "";
CanTranslate = false;
if (!string.IsNullOrEmpty(Request.Cookies["Token"]))
{
await GanjoorSessionChecker.ApplyPermissionsToViewData(Request, Response, ViewData);
if (ViewData.ContainsKey($"{RMuseumSecurableItem.GanjoorEntityShortName}-{RMuseumSecurableItem.Translations}"))
{
CanTranslate = true;
}
}
PoemId = int.Parse(Request.Query["p"]);
ViewData["GoogleAnalyticsCode"] = Configuration["GoogleAnalyticsCode"];
HttpResponseMessage responseLanguages = await _httpClient.GetAsync($"{APIRoot.Url}/api/translations/languages");
if (!responseLanguages.IsSuccessStatusCode)
{
ErrorMessage = JsonConvert.DeserializeObject(await responseLanguages.Content.ReadAsStringAsync());
return Page();
}
var allLanguages = JsonConvert.DeserializeObject(await responseLanguages.Content.ReadAsStringAsync());
HttpResponseMessage response = await _httpClient.GetAsync($"{APIRoot.Url}/api/translations/poem/{PoemId}/published");
if (!response.IsSuccessStatusCode)
{
ErrorMessage = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
return Page();
}
Translations = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
LanguageId = string.IsNullOrEmpty(Request.Query["lang"]) ? -1 : int.Parse(Request.Query["lang"]);
List poemLanguages = new List();
foreach (var lang in allLanguages)
{
if (Translations.Where(t => t.Language.Id == lang.Id).FirstOrDefault() != null)
{
poemLanguages.Add(lang);
}
}
if (LanguageId != -1)
if (!poemLanguages.Where(l => l.Id == LanguageId).Any())
{
poemLanguages.Add(allLanguages.Where(l => l.Id == LanguageId).Single());
}
Languages = poemLanguages.ToArray();
if (LanguageId != -1)
Translations = Translations.Where(t => t.Language.Id == LanguageId).ToArray();
Translation = Translations.Length > 0 ? Translations[0] : null;
if (Translation != null)
LanguageId = Translation.Language.Id;
var responsePoem = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/poem/{PoemId}?verseDetails=true&catInfo=true&rhymes=false&recitations=false&images=false&songs=false&comments=false&navigation=true");
if (!responsePoem.IsSuccessStatusCode)
{
ErrorMessage = JsonConvert.DeserializeObject(await responsePoem.Content.ReadAsStringAsync());
return Page();
}
Poem = JsonConvert.DeserializeObject(await responsePoem.Content.ReadAsStringAsync());
return Page();
}
}
}