using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using TelerikAspNetCoreApp170.Models;

namespace TelerikAspNetCoreApp170.Controllers
{
	public class HomeController : Controller
	{
        private Dictionary<int, string> resolutionColors = new Dictionary<int, string>() {
            {1,"#ccc"},
            {2,"#c00"}
        };

        public override void OnActionExecuting(ActionExecutingContext context)
		{
			if (!string.IsNullOrEmpty(context.HttpContext.Request.Query["culture"]))
			{
				CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(context.HttpContext.Request.Query["culture"]);
			}
			base.OnActionExecuting(context);
		}

		public IActionResult Index()
		{
			var model = new ElectricitySource[] {
				new ElectricitySource("Hydro", 22) { Explode = true },
				new ElectricitySource("Solar", 2),
				new ElectricitySource("Nuclear", 49),
				new ElectricitySource("Wind", 27)
			};

            return View(model);
		}

        [HttpPost]
        public ActionResult _WorldScreenResolution()
        {
            Thread.Sleep(5000);

            var screenResolutions = new ScreenResolutionRemoteDataViewModel[] {
                new ScreenResolutionRemoteDataViewModel() { Year = "2000", Resolution = "1024x768", Share = 25, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2000", Resolution = "Other", Share = 75, VisibleInLegend = false, OrderNumber = 2 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2001", Resolution = "1024x768", Share = 29, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2001", Resolution = "Other", Share = 71, VisibleInLegend = false, OrderNumber = 2 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2002", Resolution = "1024x768", Share = 34, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2002", Resolution = "Other", Share = 66, VisibleInLegend = false, OrderNumber = 2 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2003", Resolution = "1024x768", Share = 40, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2003", Resolution = "Other", Share = 60, VisibleInLegend = false, OrderNumber = 2 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2004", Resolution = "1024x768", Share = 47, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2004", Resolution = "Other", Share = 53, VisibleInLegend = false, OrderNumber = 2 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2005", Resolution = "1024x768", Share = 53, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2005", Resolution = "Other", Share = 47, VisibleInLegend = false, OrderNumber = 2 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2006", Resolution = "1024x768", Share = 57, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2006", Resolution = "Other", Share = 43, VisibleInLegend = false, OrderNumber = 2 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2007", Resolution = "1024x768", Share = 54, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2007", Resolution = "Other", Share = 46, VisibleInLegend = false, OrderNumber = 2 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2008", Resolution = "1024x768", Share = 48, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2008", Resolution = "Other", Share = 52, VisibleInLegend = false, OrderNumber = 2 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2009", Resolution = "1024x768", Share = 36, VisibleInLegend = false, OrderNumber = 1 },
                new ScreenResolutionRemoteDataViewModel() { Year = "2009", Resolution = "Other", Share = 64, VisibleInLegend = false, OrderNumber = 2 }
            }.ToList();

            var viewModel = new List<ScreenResolutionRemoteDataViewModel>();

            for (var i = 0; i < screenResolutions.Count; i++)
            {
                var data = screenResolutions[i];
                if (data.Year == "2005" && data.Resolution == "1024x768")
                {
                    data.Color = resolutionColors[2];
                }
                else if (data.Resolution == "Other")
                {
                    data.Color = resolutionColors[1];
                }

                viewModel.Add(data);
            }

            return Json(viewModel);
        }

	}
}
