using BlizzPosts.Frontend;
using BlizzPosts.Frontend.Extentions;
using BlizzPosts.Frontend.Models;
using BlizzPosts.Localisation;
using BlizzPosts.Services;
using BlizzPosts.Services.Domain;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;

namespace BlizzPosts.Frontend.Controllers
{
	public class TrackerController : Controller
	{
		private readonly ITrackerLogic _trackerService;

		private readonly ITrackerFilterService _trackerFilterService;

		public TrackerController(ITrackerLogic trackerService, ITrackerFilterService trackerFilterService)
		{
			this._trackerService = trackerService;
			this._trackerFilterService = trackerFilterService;
		}

		public ActionResult Index(string game, string lang, string p)
		{
			ActionResult actionResult;
			CookieGetterAndSetter.SetLanguageCookie(lang);
			bool showTopicCookie = true;
			bool showBlogEntryCookie = true;
			bool showTwitterCookie = false;
			bool flag = true;
			if (lang.ToLower().Trim() == "en")
			{
				showTopicCookie = CookieGetterAndSetter.GetShowTopicCookie() == "yes";
				showBlogEntryCookie = CookieGetterAndSetter.GetShowBlogEntryCookie() == "yes";
				showTwitterCookie = CookieGetterAndSetter.GetShowTwitterCookie() == "yes";
			}
			else
			{
				flag = false;
			}
			try
			{
				TrackerDo tracker = this._trackerService.GetTracker(game, lang, p, showBlogEntryCookie, showTopicCookie, showTwitterCookie);
				Tracker model = Tracker.GetModel(tracker, lang, game);
				model.IncludeTrackerTableFilters = flag;
				if (game.ToUpper() == "HEROES")
				{
					model.IncludeTrackerTableFilters = false;
				}
				model.IsTopicsShowing = showTopicCookie;
				model.IsBlogEntriesShowing = showBlogEntryCookie;
				model.IsTwitterShowing = showTwitterCookie;
				if (model.TrackerItems.Count == 0 && lang.ToUpper() != "EN")
				{
					tracker = this._trackerService.GetTracker(game, "EN", p, showBlogEntryCookie, showTopicCookie, showTwitterCookie);
					model = Tracker.GetModel(tracker, "EN", game);
					if (model.TrackerItems.Count == 0)
					{
						throw new HttpException(404, "The page you are looking for could not be found.");
					}
				}
				model.UserLanguageAbbrev = lang;
				if (game.ToLower() == "all")
				{
					model.PageTitle = SafeResourceFetcher.GetResource("HomePage", lang);
				}
				else
				{
					model.PageTitle = SafeResourceFetcher.GetResource(string.Concat("Game_", model.GameAbbrev.ToUpper()), lang);
				}
				model.RssLink = string.Format("/rss/{0}/{1}", game.ToLower(), lang.ToLower());
				actionResult = base.View(model);
			}
			catch (Exception exception1)
			{
				Exception exception = exception1;
				if (exception is ArgumentNullException || exception is ArgumentException)
				{
					throw new HttpException(404, "The page you are looking for could not be found.");
				}
				throw;
			}
			return actionResult;
		}

		public ActionResult TrackerFilter([ModelBinder(typeof(ShortGuidModelBinder))] ShortGuid trackerFilterId, string urlFriendlyName)
		{
			return base.View();
		}

		public ActionResult TrackerFilterCreate(string lang)
		{
			if (LanguageResource.IsSupportedInterfaceLanguage(lang))
			{
				return base.View();
			}
			return base.RedirectToAction("TrackerFilterCreate", new { lang = "en" });
		}

		public PartialViewResult TrackerTablePartial(string game, string lang, bool showTopics, bool showBlogEntries, bool showTwitter)
		{
			PartialViewResult partialViewResult;
			CookieGetterAndSetter.SetLanguageCookie(lang);
			CookieGetterAndSetter.SetShowTopicCookie((showTopics ? "yes" : "no"));
			CookieGetterAndSetter.SetShowBlogEntryCookie((showBlogEntries ? "yes" : "no"));
			CookieGetterAndSetter.SetShowTwitterCookie((showTwitter ? "yes" : "no"));
			try
			{
				TrackerDo tracker = this._trackerService.GetTracker(game, lang, "1", showBlogEntries, showTopics, showTwitter);
				Tracker model = Tracker.GetModel(tracker, lang, game);
				model.UserLanguageAbbrev = lang;
				if (model.TrackerItems.Count == 0 && lang.ToUpper() != "EN")
				{
					tracker = this._trackerService.GetTracker(game, "EN", "1", showBlogEntries, showTopics, showTwitter);
					model = Tracker.GetModel(tracker, "EN", game);
				}
				model.RssLink = string.Format("/rss/{0}/{1}", game.ToLower(), lang.ToLower());
				partialViewResult = this.PartialView("_Threads", model);
			}
			catch (Exception exception1)
			{
				Exception exception = exception1;
				if (exception is ArgumentNullException || exception is ArgumentException)
				{
					throw new HttpException(404, "The page you are looking for could not be found.");
				}
				throw;
			}
			return partialViewResult;
		}
	}
}