using System.Linq;
using System.Threading;
using System.Web.Mvc;
using System.Data.Entity;
using atakanozbancom.Models.classes;

namespace atakanozbancom.Controllers
{
    public class myprojectsController : Controller
    {
        private readonly Context c = new Context();

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

        public PartialViewResult projectposts()
        {
            var cultureFull = Thread.CurrentThread.CurrentUICulture.Name;
            var cultureShort = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
            var isEnglish = cultureShort == "en";

            var projects = c.MyProjects
                .Include(p => p.Translations)
                .ToList();

            var ids = projects.Select(p => p.id).ToList();

            var medias = c.projectmedia
                .Where(m => ids.Contains(m.project_id))
                .OrderBy(m => m.sort_order)
                .ThenBy(m => m.id)
                .ToList();

            var mediaLookup = medias
                .GroupBy(m => m.project_id)
                .ToDictionary(g => g.Key, g => g.ToList());

            var model = projects.Select(p =>
            {
                var title = !isEnglish
                    ? p.Translations
                        .Where(t => t.culture == cultureFull || t.culture == cultureShort)
                        .Select(t => t.title)
                        .FirstOrDefault() ?? p.title
                    : p.title;

                var description = !isEnglish
                    ? p.Translations
                        .Where(t => t.culture == cultureFull || t.culture == cultureShort)
                        .Select(t => t.description)
                        .FirstOrDefault() ?? p.description
                    : p.description;

                var vm = new MyProjectsVM
                {
                    id = p.id,
                    title = title,
                    description = description,
                    image = p.image,
                    iframe = p.iframe
                };

                if (mediaLookup.TryGetValue(p.id, out var list))
                {
                    vm.medias = list.Select(m => new ProjectMediaVM
                    {
                        url = m.url,
                        order = m.sort_order,
                        type =
                            m.media_type == MediaType.Image ? "image" :
                            m.media_type == MediaType.Iframe ? "iframe" :
                            // m.media_type == MediaType.Video ? "video" :
                            "document"
                    }).ToList();
                }

                if (vm.medias == null || vm.medias.Count == 0)
                {
                    if (!string.IsNullOrWhiteSpace(p.iframe))
                        vm.medias.Add(new ProjectMediaVM { type = "iframe", url = p.iframe, order = 0 });

                    if (!string.IsNullOrWhiteSpace(p.image))
                        vm.medias.Add(new ProjectMediaVM { type = "image", url = p.image, order = 1 });
                }

                return vm;
            }).ToList();

            return PartialView(model);
        }
    }
}
