using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TelerikMvcApp3.Models;

namespace TelerikMvcApp3.Controllers
{
    public class HomeController : Controller
    {
        static Dictionary<int, string> people = new Dictionary<int, string>();
        static HomeController()
        {
            for (int i = 0; i < 1000; ++i)
            {
                people[i] = "People " + i;
            }
        }

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult CustomerPage(int id = 0)
        {
            if (id > 0)
            {
                var test = new TestModel();
                test.Address = id.ToString() + " Address";
                test.CustomerID = id;
                test.CustomerName = people[id];
                test.JobID = id;

                return View(test);
            }
            return View();
        }

        [HttpPost]
        public ActionResult CustomerPage(TestModel model)
        {
            return View(model);
        }

        public JsonResult GetCustomers(string text, string additional)
        {            
            var queryPeople = from person in people
                              where person.Value.Contains(text.ToUpper())
                              select person;

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

            foreach (var person in queryPeople)
            {
                items.Add(new SelectListItem() { Value = person.Key.ToString(), Text = person.Value });                
                if (items.Count > 15)
                    break;
            }
            return Json(items, JsonRequestBehavior.AllowGet);
        }

        private static Random random = new Random();
        public static string RandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }

        //public ActionResult CustomerPage(int id = 3)
        //{
        //    if (id > 0)
        //    {
        //        var test = new TestModel();
        //        test.Address = id.ToString() + " Address";
        //        test.CustomerID = id;
        //        test.CustomerName = "Person " + id.ToString();
        //        test.JobID = id * 5;

        //        return View(test);
        //    }
        //    return View();
        //}

        //[HttpPost]
        //public ActionResult CustomerPage(TestModel model)
        //{
        //    return View(model);
        //}

        //public JsonResult GetCustomers(string text)
        //{

        //    List<SelectListItem> returnStrings = new List<SelectListItem>();
        //    returnStrings.Add(new SelectListItem() { Text = "Person 1", Value = "1" });
        //    returnStrings.Add(new SelectListItem() { Text = "Person 2", Value = "2" });
        //    returnStrings.Add(new SelectListItem() { Text = "Person 3", Value = "3" });
        //    returnStrings.Add(new SelectListItem() { Text = "Person 4", Value = "4" });
        //    returnStrings.Add(new SelectListItem() { Text = "Person 5", Value = "5" });
        //    returnStrings.Add(new SelectListItem() { Text = "Person 6", Value = "6" });
        //    returnStrings.Add(new SelectListItem() { Text = "Person 7", Value = "7" });
        //    returnStrings.Add(new SelectListItem() { Text = "Person 8", Value = "8" });

        //    return Json(returnStrings, JsonRequestBehavior.AllowGet);
        //}
    }
}
