using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;

namespace KendoWindowClose.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            ViewBag.Message = "Kendow Window Demo";

            return View();
        }

        public ActionResult AddDashboard(string DashboardName = "error")
        {
            if (DashboardName != "error")
            {
                // do something
            }

            return Content("Finished");
        }

        public ActionResult GetWidgetWindow(string WindowId)
        {
            //Pass a partial view to a kendo window
            //which configures the widget for the appropriate area
            if (WindowId == "1")
            {
                return PartialView("Window1");
            }
            return PartialView("Window1");
        }

        [HttpPost]
        [MultiButton(MatchFormKey = "Window1", MatchFormValue = "Cancel")]
        public ActionResult Cancel(FormCollection frmColl)
        {
            //Need a cancel button on form which doesn't have 
            // to refresh the original calling page!
            // refresh the page
            return View("Index");
        }

        [HttpPost]
        [MultiButton(MatchFormKey = "Window1", MatchFormValue = "Create")]
        public ActionResult Create(FormCollection frmColl)
        {
            // do something with the FormCollection !!!!!!
            //refresh the page
            return View("Index");
        }


    }

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public class MultiButtonAttribute : ActionNameSelectorAttribute
    {
        public string MatchFormKey { get; set; }
        public string MatchFormValue { get; set; }

        public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
        {
            return controllerContext.HttpContext.Request[MatchFormKey] != null &&
                controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue;
        }
    }
}
