using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Core.Caching;
using Nop.Services.Configuration;
using Nop.Services.Media;
using Nop.Web.Areas.Admin.Models.HomePageBannerB2C;
using Nop.Web.Framework.Components;
using Nop.Web.Infrastructure.Cache;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Web.Components
{
    public class HomePageBannerB2CViewComponent : NopViewComponent
    {
        private readonly IStoreContext _storeContext;
        private readonly IStaticCacheManager _cacheManager;
        private readonly ISettingService _settingService;
        private readonly IPictureService _pictureService;
        private readonly IWebHelper _webHelper;

        public HomePageBannerB2CViewComponent(IStoreContext storeContext,
            IStaticCacheManager cacheManager,
            ISettingService settingService,
            IPictureService pictureService,
            IWebHelper webHelper)
        {
            _storeContext = storeContext;
            _cacheManager = cacheManager;
            _settingService = settingService;
            _pictureService = pictureService;
            _webHelper = webHelper;
        }

        public IViewComponentResult Invoke()
        {
            var nivoSliderSettings = _settingService.LoadSetting<HomePageBannerB2CSettings>(_storeContext.CurrentStore.Id);

            var banners = new List<PublicInfoB2CModel>();

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop1Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile1Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet1Id),
                Text = nivoSliderSettings.Text1,
                Link = nivoSliderSettings.Link1,
                AltText = nivoSliderSettings.AltText1,
            });

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop2Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile2Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet2Id),
                Text = nivoSliderSettings.Text2,
                Link = nivoSliderSettings.Link2,
                AltText = nivoSliderSettings.AltText2,
            });

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop3Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile3Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet3Id),
                Text = nivoSliderSettings.Text3,
                Link = nivoSliderSettings.Link3,
                AltText = nivoSliderSettings.AltText3,
            });

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop4Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile4Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet4Id),
                Text = nivoSliderSettings.Text4,
                Link = nivoSliderSettings.Link4,
                AltText = nivoSliderSettings.AltText4,
            });

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop5Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile5Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet5Id),
                Text = nivoSliderSettings.Text5,
                Link = nivoSliderSettings.Link5,
                AltText = nivoSliderSettings.AltText5,
            });

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop6Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile6Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet6Id),
                Text = nivoSliderSettings.Text6,
                Link = nivoSliderSettings.Link6,
                AltText = nivoSliderSettings.AltText6,
            });

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop7Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile7Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet7Id),
                Text = nivoSliderSettings.Text7,
                Link = nivoSliderSettings.Link7,
                AltText = nivoSliderSettings.AltText7,
            });

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop8Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile8Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet8Id),
                Text = nivoSliderSettings.Text8,
                Link = nivoSliderSettings.Link8,
                AltText = nivoSliderSettings.AltText8,
            });

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop9Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile9Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet9Id),
                Text = nivoSliderSettings.Text9,
                Link = nivoSliderSettings.Link9,
                AltText = nivoSliderSettings.AltText9,
            });

            banners.Add(new PublicInfoB2CModel
            {
                PictureDesktopUrl = GetPictureUrl(nivoSliderSettings.PictureDesktop10Id),
                PictureMobileUrl = GetPictureUrl(nivoSliderSettings.PictureMobile10Id),
                PictureTabletUrl = GetPictureUrl(nivoSliderSettings.PictureTablet10Id),
                Text = nivoSliderSettings.Text10,
                Link = nivoSliderSettings.Link10,
                AltText = nivoSliderSettings.AltText10,
            });

            if (banners.Count(x => !string.IsNullOrEmpty(x.PictureDesktopUrl)) == 0)
                //no pictures uploaded
                return Content("");

            return View(banners);
        }

        protected string GetPictureUrl(int pictureId)
        {
            var cacheKey = string.Format(NopModelCacheDefaults.PICTURE_URL_MODEL_KEY,
                pictureId, _webHelper.IsCurrentConnectionSecured() ? Uri.UriSchemeHttps : Uri.UriSchemeHttp);

            return _cacheManager.Get(cacheKey, () =>
            {
                //little hack here. nulls aren't cacheable so set it to ""
                var url = _pictureService.GetPictureUrl(pictureId, showDefaultPicture: false) ?? "";
                return url;
            });
        }
    }
}
