using System.Collections.Generic;
using System.Web.Mvc;
using Kreta.BusinessLogic.Helpers;
using Kreta.BusinessLogic.Helpers.SystemSettings;
using Kreta.BusinessLogic.Security;
using Kreta.Core;
using Kreta.Enums;
using Kreta.Enums.ManualEnums;
using Kreta.Framework;
using Kreta.Resources;
using Kreta.Web.Areas.Adminisztracio.ApiControllers;
using Kreta.Web.Areas.Adminisztracio.Models;
using Kreta.Web.Helpers;
using Kreta.Web.Models.EditorTemplates;
using Kreta.Web.Security;

namespace Kreta.Web.Areas.Adminisztracio.Controllers
{
    [MvcRoleClaimsAuthorize(true)]
    [MvcRolePackageDenyAuthorize(KretaClaimPackages.IsOnlyAlkalmozott.ClaimValue)]
    [MvcRolePackageAuthorize(KretaClaimPackages.Adminisztrator.ClaimValue)]
    public class SystemSettingsController : Controller
    {
        #region Properties

        public static string TanuloErtekelesMondatbankPopupName => "TanuloErtekelesMondatbank_Popup";
        public static string TanuloErtekelesMondatbankGridName => "TanuloErtekelesMondatbankGrid";

        public static string TanuloErtekelesMondatbankItemEditPopupName => "TanuloErtekelesMondatbankItem_Edit_Popup";
        public static string TanuloErtekelesMondatbankItemEditFormName => "TanuloErtekelesMondatbankItemEditForm";
        public static string TanuloErtekelesMondatbankItemTobbesEditPopupName => "TanuloErtekelesMondatbankItem_TobbesEdit_Popup";

        private static string AMIGroupName = StringResourcesUtil.GetString(2087);
        private static string HRModul = "HRModul";
        private static string TavolletGroupName = StringResourcesUtil.GetString(8867);

        #endregion Properties
        // GET: Adminisztracio/SystemSettings
        public ActionResult Index()
        {
            var model = new SystemSettingsModel
            {
                ChildModels = new List<PanelBarChildModel>(),
                PanelName = "SystemSettingsPanel",
                ExpandMode = Kendo.Mvc.UI.PanelBarExpandMode.Multiple,
                Animation = true
            };

            model.IsIntezmenySzakkepzoJuttatas = new IntezmenyHelper(ConnectionTypeExtensions.GetSessionConnectionType()).GetIntezmenyiAdatok().IsSzakkepzoJuttatas;

            var icHelper = new IntezmenyConfigHelper(ConnectionTypeExtensions.GetSystemConnectionType());
            var hrIsEnabled = icHelper.GetIntezmenyConfig<bool>(IntezmenyConfigModulEnum.HRModul, IntezmenyConfigTipusEnum.IsEnabled);

            using (var api = new SystemSettingsApiController())
            {
                var groupSettings = api.GetSystemSettings();

                foreach (var group in groupSettings.Values)
                {
                    if (!ClaimData.HasAmi && group.Title == AMIGroupName)
                        continue;

                    if (!hrIsEnabled && group.Title == TavolletGroupName)
                        continue;

                    List<object> childSettings = new List<object>();
                    foreach (var setting in group.Childs.Values)
                    {
                        childSettings.Add(setting);
                    }

                    model.ChildModels.Add(new PanelBarChildModel { PartialViewName = "SystemSettingsPanelBar", PartialViewTitle = group.Title, Data = childSettings, Enabled = group.Enabled });
                }
            }

            var specialisBeallitasokModel = new SpecialisBeallitasokModel
            {
                ChildModels = new List<PanelBarChildModel>(),
                PanelName = "SpecialisBeallitasokPanel",
                ExpandMode = Kendo.Mvc.UI.PanelBarExpandMode.Multiple,
                Animation = true
            };
            specialisBeallitasokModel.ChildModels.Add(new PanelBarChildModel { PartialViewName = "SpecialisBeallitasokPanelBar", PartialViewTitle = SystemSettingsResource.SpecialisBeallitasok });

            model.SpecialisBeallitasokModel = specialisBeallitasokModel;

            var rendszerszintuTorlesiLehetosegekModel = new RendszerszintuTorlesiLehetosegekModel
            {
                ChildModels = new List<PanelBarChildModel>(),
                PanelName = "RendszerszintuTorlesPanel",
                ExpandMode = Kendo.Mvc.UI.PanelBarExpandMode.Multiple,
                Animation = true
            };
            rendszerszintuTorlesiLehetosegekModel.ChildModels.Add(new PanelBarChildModel { PartialViewName = "RendszerszintuTorlesPanelBar", PartialViewTitle = SystemSettingsResource.RendszerszintuTorlesiLehetosegek });

            model.RendszerszintuTorlesiLehetosegekModel = rendszerszintuTorlesiLehetosegekModel;

            if (model.IsIntezmenySzakkepzoJuttatas)
            {
                var juttatasBeallitasokModel = new JuttatasBeallitasokModel
                {
                    ChildModels = new List<PanelBarChildModel>(),
                    PanelName = "JuttatasBeallitasokPanel",
                    ExpandMode = Kendo.Mvc.UI.PanelBarExpandMode.Multiple,
                    Animation = true,
                };
                juttatasBeallitasokModel.ChildModels.Add(new PanelBarChildModel { PartialViewName = "JuttatasBeallitasokPanelBar", PartialViewTitle = SystemSettingsResource.JuttatasBeallitasok });

                model.JuttatasBeallitasokModel = juttatasBeallitasokModel;
            }

            return View(model);
        }

        [HttpGet]
        [MvcRolePackageDenyAuthorize(KretaClaimPackages.IsSzirIntezmeny.ClaimValue)]
        public ActionResult OpenCsoportTipusListWindow()
        {
            PopUpModel pm = new PopUpModel(null, "CsoportTipusList_Edit_Popup");
            pm = pm.AddCancelBtn(pm, "SettingsHelper.csoportTipusListWindowClose");
            pm = pm.AddBtn(pm, "saveCsoportTipusList", CommonResource.Mentes, "SettingsHelper.saveCsoportTipusList");

            return PartialView(Constants.General.PopupView, pm);
        }

        [HttpGet]
        public ActionResult OpenGradeWeightWindow()
        {
            SystemSettingsApiController api = new SystemSettingsApiController();
            var model = api.GetGradeWeights();

            PopUpModel pm = new PopUpModel(model, "SetGradeWeights_Bevitel");
            pm = pm.AddCancelBtn(pm, "SettingsHelper.gradeWeightsWindowClose");
            pm = pm.AddBtn(pm, "saveGradeWeights", CommonResource.Mentes, "SettingsHelper.saveGradeWeights");
            return PartialView(Constants.General.PopupView, pm);
        }

        public ActionResult FejlecSzerkesztoPopUp()
        {
            PopUpModel pm;
            var helper = new IntezmenyHelper(ConnectionTypeExtensions.GetSessionConnectionType());

            var model = new FejlecLablecSzerkesztoModel(helper.GetIntezmenyFejlec()) { IsFejlec = true };
            pm = CreateFejlecLablecModel(model);

            return PartialView(Constants.General.PopupView, pm);
        }

        public ActionResult LablecSzerkesztoPopUp()
        {
            PopUpModel pm;
            var helper = new IntezmenyHelper(ConnectionTypeExtensions.GetSessionConnectionType());

            var model = new FejlecLablecSzerkesztoModel(helper.GetIntezmenyLablec());
            pm = CreateFejlecLablecModel(model);

            return PartialView(Constants.General.PopupView, pm);
        }

        private PopUpModel CreateFejlecLablecModel(FejlecLablecSzerkesztoModel model)
        {
            var pm = new PopUpModel(model, "FejlecLablecSzerkeszto");
            pm = pm.AddCancelBtn(pm, "function(){FejlecLablecSzerkesztoHelper.Cancel();}");
            pm = pm.AddDeleteBtn(pm, "function(){FejlecLablecSzerkesztoHelper.Torles();}");
            pm = pm.AddOkBtn(pm, "function(){FejlecLablecSzerkesztoHelper.Mentes();}");
            pm = pm.AddBtn(pm, "generate", CommonResource.Elonezet, "function(){FejlecLablecSzerkesztoHelper.Generalas();}");
            return pm;
        }

        #region Tanuló Értékelés Mondatbank

        [HttpGet]
        [MvcValidateAjaxAntiForgeryToken]
        [MvcRolePackageDenyAuthorize(KretaClaimPackages.IsSzirIntezmeny.ClaimValue)]
        public ActionResult OpenTanuloErtekelesMondatbankPopup()
        {
            PopUpModel popUpModel = new PopUpModel(new TanuloErtekelesMondatbankSearchModel(), TanuloErtekelesMondatbankPopupName);
            popUpModel = popUpModel.AddBtn(popUpModel, "TanuloErtekelesMondatbankCancelButton", CommonResource.Megse, "TanuloErtekelesMondatbankHelper.tanuloErtekelesMondatbankPopupCancel", "BtnCancel");
            return PartialView(Constants.General.PopupView, popUpModel);
        }

        [HttpPost]
        [MvcValidateAjaxAntiForgeryToken]
        public ActionResult OpenTanuloErtekelesMondatbankItemEditPopup(int? id)
        {
            bool isModify = id.IsEntityId();
            TanuloErtekelesMondatbankItemModel model;
            if (isModify)
            {
                var helper = new TanuloErtekelesMondatbankHelper(ConnectionTypeExtensions.GetSessionConnectionType());
                model = new TanuloErtekelesMondatbankItemModel(helper.GetTanuloErtekelesMondatbankItemById(id.Value));
            }
            else
            {
                model = new TanuloErtekelesMondatbankItemModel();
            }

            var systemSettingsHelper = new SystemSettingsHelper(ConnectionTypeExtensions.GetSessionConnectionType());
            model.NemzetiDokumentumNyelvek = systemSettingsHelper.GetSystemSettingValue<List<int>>(RendszerBeallitasTipusEnum.Nemzeti_Nyelvi_Dokumentum_Nyelvek);

            var popupModel = new PopUpModel(model, TanuloErtekelesMondatbankItemEditPopupName);
            popupModel = popupModel.AddCancelBtn(popupModel, "TanuloErtekelesMondatbankHelper.tanuloErtekelesMondatbankItemEditPopupCancel");
            popupModel = popupModel.AddOkBtn(popupModel, "TanuloErtekelesMondatbankHelper.tanuloErtekelesMondatbankItemEditPopupSave");
            if (isModify)
            {
                popupModel.AddDeleteBtn(popupModel, "TanuloErtekelesMondatbankHelper.tanuloErtekelesMondatbankItemEditPopupDelete");
            }

            return PartialView(Constants.General.PopupView, popupModel);
        }

        public ActionResult OpenTobbesModTanuloErtekelesMondatbankPopUp(List<int> idList)
        {
            var model = new TanuloErtekelesMondatbankTobbesItemModel
            {
                SelectedIdList = idList,
            };

            var systemSettingsHelper = new SystemSettingsHelper(ConnectionTypeExtensions.GetSessionConnectionType());
            model.NemzetiDokumentumNyelvek = systemSettingsHelper.GetSystemSettingValue<List<int>>(RendszerBeallitasTipusEnum.Nemzeti_Nyelvi_Dokumentum_Nyelvek);

            var popupModel = new PopUpModel(model, TanuloErtekelesMondatbankItemTobbesEditPopupName);
            popupModel = popupModel.AddCancelBtn(popupModel, "TanuloErtekelesMondatbankHelper.tanuloErtekelesMondatbankItemEditPopupCancel");
            popupModel = popupModel.AddOkBtn(popupModel, "TanuloErtekelesMondatbankHelper.tanuloErtekelesMondatbankItemTobbesEditPopupSave");

            return PartialView(Constants.General.PopupView, popupModel);
        }

        #endregion Tanuló Értékelés Mondatbank
    }
}