using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using r14.Models;
using System.Web.Script.Serialization;
using System.ComponentModel;

namespace r14.Controllers
{
    public class HomeController : Controller
    {
        public static readonly List<MountainInfo> SevenSummits = new List<MountainInfo> {
            new MountainInfo { Name = "Everest", HeightInMeters = 8848 },
            new MountainInfo { Name = "Aconcagua", HeightInMeters = 6962 },
            new MountainInfo { Name = "Mount McKinley", HeightInMeters = 6194 },
            new MountainInfo { Name = "Kilimandżaro", HeightInMeters = 5895 },
            new MountainInfo { Name = "Elbrus", HeightInMeters = 5642 },
            new MountainInfo { Name = "Vinson Massif", HeightInMeters = 4892 },
            new MountainInfo { Name = "Carstensz Pyramid", HeightInMeters = 4884 }};

        public ActionResult SortSummits()
        {
            Response.Cookies.Add( new HttpCookie("IsAdmin", "false"));
            return View(SevenSummits);
        }

        private const int PageSize = 3;

        public ActionResult Summits([DefaultValue(1)] int page)
        {
            var allItems = SevenSummits;

            ViewData["pagingInfo"] = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = allItems.Count
            };

            return View(allItems.Skip((page - 1) * PageSize).Take(PageSize));
        } 


        public string DeleteItem(string item)
        {
            return "Usuwam " + HttpUtility.HtmlEncode(item);
        }

        private List<ListItem> l = new List<ListItem>();

        public ActionResult List()
        {
            Populate();

            return View(l);
        }

        private void Populate()
        {
            l.Add(new ListItem() { ItemID = 1, Name = "Ciabatta" });
            l.Add(new ListItem() { ItemID = 2, Name = "Francuska" });
            l.Add(new ListItem() { ItemID = 3, Name = "Czosnkowa" });
            l.Add(new ListItem() { ItemID = 4, Name = "Peshwari" });
            l.Add(new ListItem() { ItemID = 5, Name = "Pitta" });
        }

        //[HttpPost] // Pozwalamy wyłącznie na żądania POST (ta akcja powoduje zmianę danych) 
        //public JavaScriptResult DeleteItem(int itemID)
        //{
        //    Populate();

        //    var itemToDelete = l.Find(x => x.ItemID == itemID);
        //    // Dodać: Poinformowanie warstwy modelu o konieczności usunięcia obiektu "itemToDelete" 

        //    // Poinformowanie przeglądarki o konieczności zaktualizowania modelu DOM 
        //    var script = string.Format("OnItemDeleted({0}, {1})",
        //                               itemToDelete.ItemID,
        //                               JavaScriptEncode(itemToDelete.Name));
        //    return JavaScript(script);
        //}

        private static string JavaScriptEncode(string str)
        {
            // Kodowanie określonych znaków lub wyrażeń JavaScript może być niewłaściwe
            return new JavaScriptSerializer().Serialize(str);
        }

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