using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web.Mvc;
using Kreta.BusinessLogic.Classes.ExcelHelpers;
using Kreta.BusinessLogic.Helpers;
using Kreta.BusinessLogic.Security;
using Kreta.Framework;
using Kreta.Resources;
using Kreta.Web.Areas.Tantargy.ApiControllers;
using Kreta.Web.Areas.Tantargy.Models;
using Kreta.Web.Helpers;
using Kreta.Web.Models.EditorTemplates;
using Kreta.Web.Security;

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

        protected IKretaAuthorization Authorization { get; }

        #endregion

        public BaseTanmenetController(IKretaAuthorization authorization)
        {
            Authorization = authorization ?? throw new ArgumentNullException(nameof(authorization));
        }

        #region Popup actions

        [NonAction]
        public ActionResult OpenNewTanmenetWindow(NewTanmenetModel model)
        {
            var pm = new PopUpModel(model, "~/Areas/Tantargy/Views/Tanmenet/NewTanmenet_Bevitel.cshtml");
            pm = pm.AddCancelBtn(pm, "TanmenetHelper.newTanmenetCancel");
            pm = pm.AddBtn(pm, "openNewTanmenetGrid", CommonResource.Tovabb, "TanmenetHelper.openNewTanmenetGrid");
            return PartialView(Constants.General.PopupView, pm);
        }

        [NonAction]
        protected ActionResult OpenEditTanmenetGridWindow(TanmenetOrakModel model)
        {
            if (!string.IsNullOrWhiteSpace(model.FoglalkozasId_input))
            {
                model.TanmenetModalHeader = TantargyResource.UjTanmenet + model.FoglalkozasId_input;
            }
            else
            {
                model.TanmenetModalHeader = TantargyResource.Tanmenet + new BaseTanmenetApiController().GetTanmenetModalHeader(model.TantargyId, model.OsztalyId);
            }
            var pm = new PopUpModel(model, "~/Areas/Tantargy/Views/Tanmenet//EditTanmenetGrid_Bevitel.cshtml");
            pm = pm.AddBtn(pm, "TanmenetGridCancelBtn", AdminisztracioResource.Megsem, "TanmenetHelper.editTanmenetGridCancel");
            pm = pm.AddBtn(pm, "SaveTanmenet", CommonResource.Mentes, "TanmenetHelper.saveTanmenetOrak");
            return PartialView(Constants.General.PopupView, pm);
        }

        #endregion

        #region Export actions

        [NonAction]
        public string ExportTanmenet(int tantargyId, int osztalyId)
        {
            var memoryStream = GetTanmenetExport(tantargyId, osztalyId);

            if (memoryStream != null)
            {
                return Convert.ToBase64String(memoryStream.ToArray());
            }

            return ImportExportCommonResource.NincsElegendoAdatARendszerbenAzExportalashoz;
        }

        private MemoryStream GetTanmenetExport(int tantargyId, int osztalyId)
        {
            var manager = new ExcelExportManager();
            var exportFile = new ExcelExportItem();

            exportFile.AddColumn(0, TanmenetResource.Oraszam);
            exportFile.AddColumn(1, TanmenetResource.Tema);

            var rowIndex = 2;
            var tanmenetHelper = new TanmenetHelper(ConnectionTypeExtensions.GetSessionConnectionType());

            var ds = tanmenetHelper.GetTanmenetek(tantargyId, osztalyId, ClaimData.FelhasznaloId);

            foreach (var row in ds.Tables[0].AsEnumerable())
            {
                exportFile.AddCell(rowIndex, 0, row.Field<int>("Oraszam"));
                exportFile.AddCell(rowIndex, 1, row.Field<string>("Tema"));
                rowIndex++;
            }

            // mindenképp 2000-ig feltöltjük az excelt
            for (; rowIndex < 2001; rowIndex++)
            {
                exportFile.AddCell(rowIndex, 0, rowIndex);
                exportFile.AddCell(rowIndex, 1, string.Empty);
            }

            return manager.CreateExcelExport(new List<ExcelExportItem> { exportFile });
        }

        #endregion
    }
}