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.iScheduler.Report.Areas.iSchedulerReport
{
    /// <summary>
    /// A modul akcióinak és egyéb szolgáltatásai teszteléséhez hasznos osztály.
    /// </summary>
    public class TestController : BaseController
    {
        #region UserAdministration akciói

        #region GetRoleList
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userolegroup"></param>
        /// <param name="namefilter"></param>
        /// <param name="usernamelist"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public JsonResult GetRoleList(string userolegroup, string namefilter, string usernamelist, string query)
        {
            System.Diagnostics.Debug.WriteLine($"iSchedulerReport.Test.GetRoleList: userolegroup={userolegroup}, namefilter={namefilter}, usernamelist={usernamelist}, query={query}");

            List<SelectListJSON> baselist = new List<SelectListJSON>();
            List<SelectListJSON> resultlist = new List<SelectListJSON>();
            try
            {
                for (int i = 0; i < 10; i++)
                {
                    baselist.Add(new SelectListJSON() { Value = $"Rolegroup{i:00}", Text = $"Rolegroup{i:00}" });
                }

                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 GetRoleList

        #region GetUserList
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userolegroup"></param>
        /// <param name="namelist"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public JsonResult GetUserList(string userolegroup, string namelist, string query)
        {
            #region DEBUG
#if DEBUG
            string thisfn = string.Concat(nameof(TestController), ".", nameof(GetUserList), ": ");
            System.Diagnostics.Debug.WriteLine($"{thisfn}START userolegroup={userolegroup}, namefilter={namelist}, query={query}");
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
#endif
            #endregion DEBUG

            List<SelectListJSON> baselist = new List<SelectListJSON>();
            List<SelectListJSON> resultlist = new List<SelectListJSON>();
            try
            {
                bool isUserRoleGroup = false;
                if (bool.TryParse(userolegroup, out bool isURG)) isUserRoleGroup = isURG;
                var userService = new Vrh.Membership.UserService();
                baselist = userService.GetUserList(isUserRoleGroup, namelist);

                if (String.IsNullOrEmpty(query))
                {
                    resultlist = baselist;
                }
                else
                {
                    query = query.ToLower();
                    foreach (var item in baselist)
                    {
                        if (item.Text.ToLower().Contains(query)) resultlist.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                resultlist.Add(new SelectListJSON() { Value = "", Text = "Error: " + ex.Message });
            }
            #region DEBUG
#if DEBUG
            sw.Stop();
            System.Diagnostics.Debug.WriteLine($"{thisfn}END Elapsed time: {sw.ElapsedMilliseconds}ms");
#endif
            #endregion DEBUG
            return Json(resultlist, JsonRequestBehavior.AllowGet);
        }
        #endregion GetUserList

        #region CreateNewRole
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rolename"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CreateNewRole(string rolename)
        {
            ReturnInfoJSON rijson = new ReturnInfoJSON();
            System.Diagnostics.Debug.WriteLine($"iSchedulerReport.Test.CreateNewRole: rolename={rolename}");
            rijson.ReturnMessage = $"Felvitel sikeres! rolename = {rolename}";
            return Json(rijson, JsonRequestBehavior.AllowGet);
        }
        #endregion CreateNewRole

        #region DeleteRole
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rolename"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeleteRole(string rolename)
        {
            ReturnInfoJSON rijson = new ReturnInfoJSON();
            System.Diagnostics.Debug.WriteLine($"iSchedulerReport.Test.DeleteRole: rolename={rolename}");
            rijson.ReturnMessage = $"Törlés sikeres! rolename = {rolename}";
            return Json(rijson, JsonRequestBehavior.AllowGet);
        }
        #endregion DeleteRole

        #region Assignment
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mode">Lehetséges értékei:
        /// rolestorolegroups: szerepek hozzárendelése szerepkörökhöz
        /// rolestousers: szerepek hozzárendelése felhasználókhoz (közvetlen kapcsolat!)
        /// rolegroupstousers: szerepkörök hozzárendelése felhasználókhoz
        /// </param>
        /// <param name="namelist">szerep / szerepkör nevek listája (vesszővel elválasztva)</param>
        /// <param name="targetnamelist">szerepkör / felhasználó nevek listája (vesszővel elválasztva)</param>
        /// <returns>Egy returninfoJSON struktúra.</returns>
        [HttpPost]
        public JsonResult Assignment(string mode, string namelist, string targetnamelist)
        {
            ReturnInfoJSON rijson = new ReturnInfoJSON();
            System.Diagnostics.Debug.WriteLine($"iSchedulerReport.Test.Assignment: mode={mode}, namelist={namelist}, targetnamelist={targetnamelist}");
            rijson.ReturnMessage = $"Összerendelés sikeres! mode={mode}, namelist={namelist}, targetnamelist={targetnamelist}";
            return Json(rijson, JsonRequestBehavior.AllowGet);
        }
        #endregion Assignment

        #endregion UserAdministration akciói

        #region List
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlfile"></param>
        /// <param name="lcid"></param>
        /// <returns></returns>
        public JsonResult List(string xmlfile, string lcid)
        {
            System.Diagnostics.Debug.WriteLine($"iSchedulerReport.Test.ListObjects: xmlfile={xmlfile}, lcid={lcid}");

            List<SelectListJSON> baselist = new List<SelectListJSON>();
            List<SelectListJSON> resultlist = new List<SelectListJSON>();
            try
            {
                //for (int i = 0; i < 10; i++)
                //{
                //    string sufval = "/" + (i % 2 == 0 ? "hu-HU" : "en-US");
                //    string suftext = i % 2 == 0 ? " (hu-HU)" : " (en-US)";
                //    string sufvalpr = "/" + (i % 3 == 0 ? "*" : "presets#43");
                //    string suftextpr = i % 3 == 0 ? " (*)" : " (presets#43)";
                //    baselist.Add(new SelectListJSON() { Value = $"Downtime{i:00}{sufval}{sufvalpr}", Text = $"Downtime riport{i:00}{suftext}{suftextpr}" });
                //}

                //if (lcid == "*")
                //{
                //    resultlist = baselist;
                //}
                //else
                //{
                //    if (String.IsNullOrWhiteSpace(lcid)) lcid = VRH.Log4Pro.MultiLanguageManager.MultiLanguageManager.RelevantLanguageCode;
                //    foreach (SelectListJSON item in baselist)
                //    {
                //        if (item.Value.Contains(lcid)) resultlist.Add(item);
                //    }
                //}
                Vrh.OneReport.ReportService reportService = new OneReport.ReportService(HttpContext.Server.MapPath(WebCommon.SIGN_RELATIVE));
                resultlist = reportService.List(xmlfile, lcid);
            }
            catch (Exception ex)
            {
                resultlist.Add(new SelectListJSON() { Value = "", Text = "Error: " + ex.Message });
            }

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

        #region ListFormats action
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>

        public JsonResult ListFormats()
        {
            System.Diagnostics.Debug.WriteLine("iSchedulerReport.Test.ListFormats PING");

            List<SelectListJSON> resultlist = new List<SelectListJSON>();

            try
            {
                Vrh.OneReport.ReportService reportService = new OneReport.ReportService(HttpContext.Server.MapPath(WebCommon.SIGN_RELATIVE));
                resultlist = reportService.ListFormats();
            }
            catch (Exception ex)
            {
                resultlist.Add(new SelectListJSON() { Value = "", Text = "Error: " + ex.Message });
            }

            return Json(resultlist, JsonRequestBehavior.AllowGet);
        }

        #endregion ListFormats action
    }
}