﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SmartStoreNetCore.Core.Entities;
using SmartStoreNetCore.Core.Utilities.Interfaces;
using SmartStoreNetCore.Repositories.UnitOfWork;
using SmartStoreNetCore.Services.Interfaces;
using System.Linq;
using System.Threading.Tasks;

namespace SmartStoreNetCore.Web.ViewComponents
{
    public abstract class BaseViewComponent : ViewComponent
    {
        public BaseViewComponent(ISmartStoreWorkData context, IWebService webService, IDateTimeProvider dateTimeProvider, IMapper mapper)
        {
            this.Context = context;
            this.WebService = webService;
            this.DateTimeProvider = dateTimeProvider;
            this.Mapper = mapper;
        }

        public ISmartStoreWorkData Context { get; set; }

        public IWebService WebService { get; set; }

        public IDateTimeProvider DateTimeProvider { get; set; }

        public IMapper Mapper { get; set; }

        public Customer CurrentCustomer
        {
            get
            {
                var user = this.User;
                if (user.Identity.IsAuthenticated)
                {
                    return this.Context.Customers.FindAsync(c => c.UserName.Equals(user.Identity.Name)).Result.FirstOrDefault();
                }

                return null;
            }
        }

        public abstract Task<IViewComponentResult> InvokeAsync(object parameter);
    }
}
