using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VRH.Common;
using Vrh.Web.Common.Lib;

namespace Vrh.Web.Reporting.Controllers
{
    #region HomeController class 
    public class HomeController : BaseController
    {
        /// <summary>
        /// Szükséges ahhoz, hogy csak alkalmazásnévvel indított URL is
        /// betaláljon a helyére.
        /// És amiatt is, hogy a Web.config (App.config) 'forms loginUrl="~/"'
        /// is találjon érvényes akciót.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return RedirectToAction("Welcome", "Layout", new { area = "Menu" });
        }

        #region iScheduler test actions

        #region CheckObjects
        private class ChekcListJSON
        {
            public string Value { get; set; }
            public string Text { get; set; }
            public bool Checked { get; set; }
        }

        public JsonResult CheckObjects(string xmlfile, string List, string GroupId)
        {
            System.Diagnostics.Debug.WriteLine("Vrh.iShceduler.HomeController.CheckObjects: xmlfile={0} List={1} GroupId={2}", xmlfile, List, GroupId);

            List<ChekcListJSON> resultlist = new List<ChekcListJSON>();
            try
            {
                List<SelectListItem> objectlist = new List<SelectListItem>();
                objectlist.Add(new SelectListItem() { Value = "RPack1", Text = "Report Pack 1." });
                objectlist.Add(new SelectListItem() { Value = "RPack2", Text = "Report Pack 2." });
                objectlist.Add(new SelectListItem() { Value = "RPack3", Text = "Report Pack 3." });
                objectlist.Add(new SelectListItem() { Value = "RPack4", Text = "Report Pack 4." });
                //objectlist.Add(new SelectListItem() { Value = "RPack5", Text = "Report Pack 5." });

                if (!String.IsNullOrEmpty(List))
                {
                    string[] idlist = List.Split(',');
                    foreach (string id in idlist)
                    {
                        SelectListItem obj = objectlist.FirstOrDefault(x => x.Value == id);
                        resultlist.Add(new ChekcListJSON() { Value = id, Text = obj == null ? "" : obj.Text, Checked = obj != null });
                    }
                }
            }
            catch (Exception ex)
            {
                resultlist.Add(new ChekcListJSON() { Value = "Error00", Text = ex.Message });
            }

            return Json(resultlist, JsonRequestBehavior.AllowGet);
        }
        #endregion CheckObjects

        #region ListObjects
        public JsonResult ListObjects(string query)
        {
            System.Diagnostics.Debug.WriteLine("Vrh.iShceduler.Controllers.HomeController.ListObjects: query={0}", query, null);

            List<SelectListJSON> baselist = new List<SelectListJSON>();
            List<SelectListJSON> resultlist = new List<SelectListJSON>();
            try
            {
                baselist.Add(new SelectListJSON() { Value = "RPack1", Text = "Report Pack 1." });
                baselist.Add(new SelectListJSON() { Value = "RPack2", Text = "Report Pack 2." });
                baselist.Add(new SelectListJSON() { Value = "RPack3", Text = "Report Pack 3." });
                baselist.Add(new SelectListJSON() { Value = "RPack4", Text = "Report Pack 4." });
                baselist.Add(new SelectListJSON() { Value = "RPack5", Text = "Report Pack 5." });

                if (System.String.IsNullOrEmpty(query))
                {
                    resultlist = baselist;
                }
                else
                {
                    query = query.ToLower();
                    foreach (SelectListJSON item in baselist)
                    {
                        if (item.Text.ToLower().Contains(query)) resultlist.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                resultlist.Add(new SelectListJSON() { Value = "", Text = "Error: " + ex.Message });
            }

            return Json(resultlist, JsonRequestBehavior.AllowGet);
        }
        #endregion ListObjects

        #region ListOperations
        public JsonResult ListOperations(string query)
        {
            System.Diagnostics.Debug.WriteLine("Vrh.iShceduler.Controllers.HomeController.ListOperations: query={0}", query, null);

            List<SelectListItem> baselist = new List<SelectListItem>();
            List<SelectListItem> resultlist = new List<SelectListItem>();
            try
            {
                baselist.Add(new SelectListItem() { Value = "Exec", Text = "Execute" });
                baselist.Add(new SelectListItem() { Value = "Oper2", Text = "Operation 2." });

                if (System.String.IsNullOrEmpty(query))
                {
                    resultlist = baselist;
                }
                else
                {
                    query = query.ToLower();
                    foreach (SelectListItem item in baselist)
                    {
                        if (item.Text.ToLower().Contains(query)) resultlist.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                resultlist.Add(new SelectListItem() { Value = "", Text = "Error: " + ex.Message });
            }

            return Json(resultlist, JsonRequestBehavior.AllowGet);
        }
        #endregion ListOperations

        #region ObjectEditor
        public ActionResult ObjectEditor(string xmlfile, string GroupId, string objectid, string bootboxid)
        {
            ViewBag.Message = $"xmlfile = '{xmlfile}'<br />GroupId = '{GroupId}'<br />objectid = '{objectid}'<br />bootboxid = '{bootboxid}'";
            return PartialView("Contact");
        }
        #endregion ObjectEditor

        #region ObjectManager
        public ActionResult ObjectManager(string xmlfile, string GroupId)
        {
            ViewBag.Message = $"xmlfile = '{xmlfile}'<br />GroupId = '{GroupId}'";
            return PartialView("ObjectManager");
        }
        #endregion ObjectManager

        #region Execute
        public JsonResult Execute(string xmlfile, string GroupId, string ObjectId, string TimeStamp)
        {
            ReturnInfoJSON rijson = new ReturnInfoJSON();
            System.Diagnostics.Debug.WriteLine($"xmlfile = '{xmlfile}', GroupId = '{GroupId}', Objectid = '{ObjectId}', TimeStamp = '{TimeStamp}'");
            rijson.ReturnMessage = $"xmlfile = {xmlfile}<br />GroupId = {GroupId}<br />Objectid = {ObjectId}<br />TimeStamp = {TimeStamp}";
            return Json(rijson, JsonRequestBehavior.AllowGet);
        }
        #endregion Execute


        #endregion iScheduler test actions
    }
    #endregion HomeController class 
}