using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FirstSteps.Data;
using FirstSteps.Models;

namespace FirstSteps.Controllers
{
    public class HotelController : Controller
    {
        //
        // GET: /Hotel/
        //      /Hotel/Index
        public ActionResult Index()
        {
            var rep = new HotelRepository();
            var hotels = rep.FindAll();

            return View(hotels);

        }

        //
        // /Hotel/Edit?id=17
        // /Hotel/Edit/17
        //
        // [HttpGet]
        public ActionResult Edit(int id)
        {
            var rep = new HotelRepository();
            var hotel = rep.FindById(id);

            var regionen = rep.GetRegionen();



            ViewBag.Regionen = CreateRegionSelectListItems(regionen);

            return View(hotel);

        }

        private static List<SelectListItem> CreateRegionSelectListItems(List<Region> regionen)
        {
            //IEnumerable<SelectListItem> selectList = new SelectList(regionen, "RegionId", "Bezeichnung");

            List<SelectListItem> items = new List<SelectListItem>();

            var emptyItem = new SelectListItem();
            emptyItem.Text = "";
            emptyItem.Value = "";
            items.Add(emptyItem);

            foreach (var r in regionen)
            {
                var item = new SelectListItem();
                item.Text = r.Bezeichnung;
                item.Value = r.RegionId.ToString();
                items.Add(item);
            }
            return items;
        }

        public ActionResult ValidateBez(string bezeichnung)
        {
            if (bezeichnung == "Hotel Schwichtenberg") {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Edit(Hotel h)
        {
            var rep = new HotelRepository();
            var regionen = rep.GetRegionen();
            ViewBag.Regionen = CreateRegionSelectListItems(regionen);


            if (!this.ModelState.IsValid)
            {
                return View();
            }

            rep.Save(h);

            return View(h);
        }

        public ActionResult Statistics()
        {
            var model = new StatistikModel();
            model.AnzahlHotels = 5;

            return PartialView(model);
        }

    }
}
