using Lada.Framework.Data.Domains.Cms;
using Lada.Framework.Data.Mappings.DbContexts;
using Lada.Framework.Data.Repositories;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.EntityFrameworkCore;

namespace ladaexpress.vn.Infrastructures.Routes
{
    public class RouteTransformer : DynamicRouteValueTransformer
    {
        //private readonly IBus _bus;
        private readonly IRepository<UrlRecord> _urlRecordRepository;
        public RouteTransformer(IRepository<UrlRecord> urlRecordRepository)
        {

            _urlRecordRepository = urlRecordRepository;
        }

        public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
        {
            var controller = "Home";
            var action = "Index";
            var entityId = 0;


            if (!values.ContainsKey("slug"))
            {
                // No slug means home page
                values["controller"] = controller;
                values["action"] = action;
                return values;
            }
            var requestPath = values["slug"]?.ToString();
            if (string.IsNullOrEmpty(requestPath))
            {
                // Empty slug means home page
                values["controller"] = controller;
                values["action"] = action;
                return values;
            }

            switch (requestPath)
            {
                case "gioi-thieu":
                    controller = "Company";
                    action = "AboutUs";
                    break;
                //case "dieu-khoan-su-dung":
                //    controller = "Privacy";
                //    action = "ConditionOfUse";
                //    break;
                //case "chinh-sach-bao-mat":
                //    controller = "Privacy";
                //    action = "SecurityPrivacy";
                //    break;
                case "tin-tuc":
                    controller = "Post";
                    action = "Index";
                    break;
                case "tuyen-dung":
                    controller = "JobPost";
                    action = "Index";
                    break;
                case "sitemap.xml":
                    controller = "UrlRecord";
                    action = "SiteMapXml";
                    break;
                case "lien-he":
                    controller = "Home";
                    action = "ContactUs";
                    break;
                case "dich-vu":
                    controller = "Product";
                    action = "Index";
                    break;
                case "lien-he-thanh-cong":
                    controller = "Lead";
                    action = "ThankYou";
                    break;
                case "robots.txt":
                    controller = "UrlRecord";
                    action = "Robot";
                    break;

                default:
                    //var urlRecord = await _bus.Rpc.RequestAsync<string, UrlRecordDetail>(requestPath);
                    var urlRecord = await _urlRecordRepository.Query().FirstOrDefaultAsync(u => u.Slug == requestPath);

                    //var urlRecord = _urlRecordService.GetUrlRecordBySlug(requestPath);
                    if (urlRecord != null && urlRecord.Id > 0)
                    {
                        controller = urlRecord.EntityName;
                        action = "Detail";
                        switch (urlRecord.EntityName)
                        {
                            case "Post":
                                entityId = urlRecord.PostId.HasValue ? urlRecord.PostId.Value : 0;
                                break;
                            case "JobPost":
                                entityId = urlRecord.JobPostId.HasValue ? urlRecord.JobPostId.Value : 0;
                                break;
                            case "Category":
                                entityId = urlRecord.CategoryId.HasValue ? urlRecord.CategoryId.Value : 0;
                                break;
                            case "Tag":
                                entityId = urlRecord.TagId.HasValue ? urlRecord.TagId.Value : 0;
                                break;
                            case "Product":
                                entityId = urlRecord.ProductId.HasValue ? urlRecord.ProductId.Value : 0;
                                break;
                            case "Topic":
                                entityId = urlRecord.TopicId.HasValue ? urlRecord.TopicId.Value : 0;
                                break;
                            default:
                                entityId = 0;
                                break;
                        }
                    }
                    else
                    {
                        controller = "Status";
                        action = "NotFound";
                    }

                    break;
            }
            values["controller"] = controller;
            values["action"] = action;
            values["id"] = entityId;
            return values;
        }
    }
}
