using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KendoMVC.Models;
using Kendo.Mvc.UI;
using KendoMVC.BL;
using System.Web.Script.Serialization;
using Kendo.Mvc.Extensions;
namespace KendoMVC.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Kendo Version Testing";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
        //public ActionResult GetStudent() { 

        //}

        public PartialViewResult ReturnPartial() {
            return PartialView("Error");
        }

        public ActionResult VersionTest()
        {
            ViewBag.Message = "Kendo Version Testing";
            return View();
        }
        public JsonResult GetBranchNames()
        {
            StudentDTO objDTO = new StudentDTO();
            //JsonResult objReturn = new JsonResult();
            //objReturn.Data =(object) StudentDTO.BRANCH_LIST.ToArray();
            return Json(StudentDTO.BRANCH_LIST, JsonRequestBehavior.AllowGet);
        }
         [HttpPost]
        public ActionResult SaveAndShowInitiator(StudentDTO obj, string BRANCH, string edtShortComment, string ADMISSION_DATE_VIEW)
        {
            if (obj.ADMISSION_DATE_VIEW.HasValue)
            {
                ViewBag.ADMISSION_DATE = Convert.ToDateTime(obj.ADMISSION_DATE_VIEW.ToString()).ToString("dd-MMM-yyyy");
            }
            obj.INITIAL_COMMENT = edtShortComment;
            obj.BRANCH = BRANCH;
            ViewBag.BRANCH = obj.BRANCH;
            ViewBag.NAME = obj.NAME;
            ViewBag.COMMENT = obj.INITIAL_COMMENT;
            ViewBag.Ajax = true;
            ViewBag.Message = "Kendo Version Testing";
            return View("VersionTest",obj);
        }
        public ActionResult ShowStudents(string BRANCH, string NAME, string ADMISSION_DATE, string COMMENT, [DataSourceRequest] DataSourceRequest request)
        {
            ViewBag.Ajax = false;
            if (string.IsNullOrEmpty(BRANCH) && string.IsNullOrEmpty(NAME) && string.IsNullOrEmpty(COMMENT) &&
                string.IsNullOrEmpty(ADMISSION_DATE))
                return null;
            List<StudentDTO> objStudentsArr = new List<StudentDTO>();
            int intRowCnt=0;
            if (Session["Students"] != null) {
                objStudentsArr = (List<StudentDTO>)Session["Students"];
            }
            if(objStudentsArr.Count!=0){
            intRowCnt = objStudentsArr.Count;
            }

            StudentDTO objDto = new StudentDTO();
            objDto.STUDENT_ID = intRowCnt + 1;
            objDto.BRANCH = BRANCH;
            objDto.NAME = NAME;
            objDto.ADMISSION_DATE = ADMISSION_DATE;
            objDto.ADMISSION_DATE_VIEW =Convert.ToDateTime(ADMISSION_DATE);
            objDto.INITIAL_COMMENT = COMMENT;
            if (objStudentsArr.Count==0)
            {
                objStudentsArr.Add(new StudentDTO
                {
                    STUDENT_ID = objDto.STUDENT_ID,
                    BRANCH = objDto.BRANCH,
                    NAME = objDto.NAME,
                    ADMISSION_DATE = objDto.ADMISSION_DATE,
                    ADMISSION_DATE_VIEW = objDto.ADMISSION_DATE_VIEW,
                    INITIAL_COMMENT = objDto.INITIAL_COMMENT
                });
            }
            else
            {
                objStudentsArr.Add(objDto);
            }
            Session["Students"] = objStudentsArr;
            return new CustomJsonResult(objStudentsArr.ToDataSourceResult(request));
        }
        public ActionResult Test_Editing_Read([DataSourceRequest] DataSourceRequest request)
        {
            try
            {
                StudentDTO objStudent = new StudentDTO();
                objStudent.STUDENT_ID = 1;
                objStudent.BRANCH = "Information Technology";
                objStudent.NAME = "Soumitra Kar";
                objStudent.INITIAL_COMMENT = "Hello India!!!!!!!";
                objStudent.ADMISSION_DATE_VIEW = new DateTime(2012, 12, 12);
                List<StudentDTO> objStudentsArr = new List<StudentDTO>();
                objStudentsArr.Add(objStudent);
                if (objStudentsArr != null)
                {
                    return Json(objStudentsArr.ToDataSourceResult(request));
                }
                else
                {
                    ModelState.AddModelError("1", "Error Uploading data.");
                    return Json(ModelState.ToDataSourceResult());
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("1", ex.Message);
                return Json(ModelState.ToDataSourceResult());
            }

        }
        public ActionResult Test_Updating_Read([DataSourceRequest] DataSourceRequest request)
        {

            ModelState.AddModelError("1", "Test!!");
            return Json(ModelState.ToDataSourceResult());

        }
    }
}
