using AutoMapper;
using Lada.Framework.Data.Domains.Categories;
using Lada.Framework.Data.Domains.Finance.Quotations;
using Lada.Framework.Infrastructures.Helpers;
using Ladaer.BackEnd.Infrastructures.DTO.Items.Categories;
using Ladaer.BackEnd.Infrastructures.DTO.Items.Finances;
using Ladaer.BackEnd.Infrastructures.DTO.Models.Categories;
using Ladaer.BackEnd.Infrastructures.DTO.Models.Finances;
using Route = Lada.Framework.Data.Domains.Finance.Quotations.Route;

namespace Ladaer.BackEnd.Infrastructures.DTO.Profiles
{
    public class CategoriesMappingProfile : Profile
    {
        public CategoriesMappingProfile()
        {
            #region Location
            CreateMap<Location, LocationModel>();
            CreateMap<Location, LocationItem>()
                .ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.Type.GetDescription()))
                .ForMember(dest => dest.CreatedTime, opt => opt.MapFrom(src => src.CreatedTime.HasValue ? src.CreatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""))
                .ForMember(dest => dest.UpdatedTime, opt => opt.MapFrom(src => src.UpdatedTime.HasValue ? src.UpdatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""))
                .ForMember(dest => dest.CreatedBy, opt => opt.MapFrom(src => src.CreatedByUserName))
                .ForMember(dest => dest.UpdatedBy, opt => opt.MapFrom(src => src.UpdatedByUserName));
            CreateMap<Location, SugLocationItem>();
            CreateMap<LocationModel, Location>();
            #endregion

            #region Menu
            CreateMap<Menu, MenuItem>()
                .ForMember(dest => dest.CreatedTime, opt => opt.MapFrom(src => src.CreatedTime.HasValue ? src.CreatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""))
                .ForMember(dest => dest.UpdatedTime, opt => opt.MapFrom(src => src.UpdatedTime.HasValue ? src.UpdatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""))
                .ForMember(dest => dest.CreatedBy, opt => opt.MapFrom(src => src.CreatedByUserName))
                .ForMember(dest => dest.UpdatedBy, opt => opt.MapFrom(src => src.UpdatedByUserName))
                .ForMember(dest => dest.AppName, opt => opt.MapFrom(src => src.AppType.GetDescription()))
                .ForMember(dest => dest.Kind, opt => opt.MapFrom(src => src.Kind.GetDescription()))
                .ForMember(dest => dest.AllowRoles, opt => opt.MapFrom(src => string.IsNullOrEmpty(src.AllowRoles) ? "" : src.AllowRoles.RemoveFirstLastComma()))
                .ForMember(dest => dest.StatusStr, opt => opt.MapFrom(src => src.Status.GetDescription()));
            CreateMap<Menu, MenuTreeItem>();
            CreateMap<MenuModel, Menu>();
            CreateMap<Menu, MenuModel>()
                .ForMember(dest => dest.AllowRoles, opt => opt.MapFrom(src => string.IsNullOrEmpty(src.AllowRoles) ? "" : src.AllowRoles.RemoveFirstLastComma()));
            #endregion

            #region Route
            CreateMap<Route, RouteModel>();
            CreateMap<Route, RouteItem>()
                .ForMember(dest => dest.CreatedTime, opt => opt.MapFrom(src => src.CreatedTime.HasValue ? src.CreatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""))
                .ForMember(dest => dest.UpdatedTime, opt => opt.MapFrom(src => src.UpdatedTime.HasValue ? src.UpdatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""));
            CreateMap<RouteModel, Route>();
            #endregion

            #region EndPoints
            CreateMap<EndPoints, EndPointsModel>();
            CreateMap<EndPoints, EndPointsItem>()
                .ForMember(dest => dest.CreatedTimeDisplay, opt => opt.MapFrom(src => src.CreatedTime.HasValue ? src.CreatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""))
                .ForMember(dest => dest.UpdatedTimeDisplay, opt => opt.MapFrom(src => src.UpdatedTime.HasValue ? src.UpdatedTime.Value.ToString("dd/MM/yyyy HH:mm") : ""));
            CreateMap<EndPointsModel, EndPoints>();
            #endregion
        }
    }

    public static class CategoriesMappingExtensions
    {
        // Location extensions
        public static LocationModel ToModel(this Location entity)
            => entity.MapTo<Location, LocationModel>();

        public static LocationItem ToItem(this Location entity)
            => entity.MapTo<Location, LocationItem>();

        public static SugLocationItem ToSugItem(this Location entity)
            => entity.MapTo<Location, SugLocationItem>();

        public static Location ToEntity(this LocationModel model)
            => model.MapTo<LocationModel, Location>();

        // Menu extensions
        public static MenuModel ToModel(this Menu entity)
            => entity.MapTo<Menu, MenuModel>();

        public static MenuItem ToItem(this Menu entity)
            => entity.MapTo<Menu, MenuItem>();

        public static MenuTreeItem ToTreeItem(this Menu entity)
            => entity.MapTo<Menu, MenuTreeItem>();

        public static Menu ToEntity(this MenuModel model)
            => model.MapTo<MenuModel, Menu>();

        // Route extensions
        public static RouteModel ToModel(this Route entity)
            => entity.MapTo<Route, RouteModel>();

        public static RouteItem ToItem(this Route entity)
            => entity.MapTo<Route, RouteItem>();

        public static Route ToEntity(this RouteModel model)
            => model.MapTo<RouteModel, Route>();

        // EndPoints extensions
        public static EndPointsModel ToModel(this EndPoints entity)
            => entity.MapTo<EndPoints, EndPointsModel>();

        public static EndPointsItem ToItem(this EndPoints entity)
            => entity.MapTo<EndPoints, EndPointsItem>();

        public static EndPoints ToEntity(this EndPointsModel model)
            => model.MapTo<EndPointsModel, EndPoints>();
    }
}