package com.jeesharp.business.modules.setting.application.support;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.setting.entity.CmsHomeBanner;
import com.jeesharp.business.modules.setting.entity.CmsHomeQuickGrid;
import com.jeesharp.business.modules.setting.entity.CmsSiteIcon;
import com.jeesharp.business.modules.setting.enums.HomeBannerPlatformEnum;
import com.jeesharp.business.modules.setting.enums.HomeBannerPositionEnum;
import com.jeesharp.business.modules.setting.application.support.HomeBannerJumpPathResolver;
import com.jeesharp.business.modules.setting.application.support.HomeQuickGridIntroLayoutSupport;
import com.jeesharp.business.modules.setting.enums.HomeQuickGridIntroJumpTypeEnum;
import com.jeesharp.business.modules.setting.enums.SiteIconCategoryEnum;
import com.jeesharp.business.modules.setting.service.HomeBannerService;
import com.jeesharp.business.modules.setting.service.HomeQuickGridService;
import com.jeesharp.business.modules.setting.service.SiteIconService;
import com.jeesharp.business.modules.setting.vo.MiniHomeBannerItemVO;
import com.jeesharp.business.modules.setting.vo.MiniHomeQuickNavItemVO;
import com.jeesharp.business.modules.setting.vo.MiniHomeSiteIconItemVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 小程序首页壳层内容拉取：直接依赖各子域 Service，避免壳层 Application 扇出多个 Application。
 *
 * @author jeesharp
 * @since 2026-06-09
 */
@Component
@AllArgsConstructor
public class MiniHomeShellContentSupport {

    private static final int SHELL_QUICK_NAV_LIMIT = 8;

    private final HomeQuickGridService homeQuickGridService;
    private final SiteIconService siteIconService;
    private final HomeBannerService homeBannerService;
    private final HomeBannerJumpPathResolver homeBannerJumpPathResolver;

    public List<MiniHomeQuickNavItemVO> listQuickNavForShell() {
        List<CmsHomeQuickGrid> rows = homeQuickGridService.listEnabledForShell(SHELL_QUICK_NAV_LIMIT);
        List<MiniHomeQuickNavItemVO> out = new ArrayList<>();
        for (CmsHomeQuickGrid e : rows) {
            MiniHomeQuickNavItemVO vo = new MiniHomeQuickNavItemVO();
            vo.setId(e.getId());
            vo.setNavCode(e.getNavCode());
            vo.setLabel(e.getTitle());
            vo.setIconUrl(StrUtil.nullToEmpty(e.getIconUrl()));
            vo.setLinkUrl(StrUtil.nullToEmpty(e.getLinkPath()));
            Integer jump = e.getIntroJumpType() == null ? HomeQuickGridIntroJumpTypeEnum.LIST.getValue() : e.getIntroJumpType();
            vo.setIntroJumpType(jump);
            vo.setIntroDetailLinkPath(StrUtil.nullToEmpty(e.getIntroDetailLinkPath()));
            vo.setIntroDetailArticleId(StrUtil.nullToEmpty(e.getIntroDetailArticleId()));
            vo.setIntroListLayout(HomeQuickGridIntroLayoutSupport.resolveListLayout(e));
            out.add(vo);
        }
        return out;
    }

    public List<MiniHomeSiteIconItemVO> listShellHomeIcons() {
        List<MiniHomeSiteIconItemVO> out = new ArrayList<>();
        out.addAll(toShellSiteIconItems(siteIconService.listEnabledByCategory(SiteIconCategoryEnum.HOME.getCode())));
        out.addAll(toShellSiteIconItems(
                siteIconService.listEnabledByCategoryAndIconCodePrefix(
                        SiteIconCategoryEnum.COMMON.getCode(),
                        "travel_entrance_")));
        return out;
    }

    public List<MiniHomeBannerItemVO> listHomeBannersForShell() {
        List<CmsHomeBanner> rows = homeBannerService.listEnabledForPortal(
                HomeBannerPlatformEnum.WECHAT_MINI.getValue(), HomeBannerPositionEnum.HOME.getValue());
        List<MiniHomeBannerItemVO> out = new ArrayList<>();
        for (CmsHomeBanner b : rows) {
            if (b.getImageUrl() == null || b.getImageUrl().isBlank()) {
                continue;
            }
            MiniHomeBannerItemVO it = new MiniHomeBannerItemVO();
            it.setId(b.getId());
            it.setTitle(b.getTitle());
            it.setImageUrl(b.getImageUrl());
            it.setJumpType(b.getJumpType());
            it.setBizType(b.getBizType());
            it.setBizId(b.getBizId());
            String resolved = homeBannerJumpPathResolver.resolveLinkUrl(b);
            if (StrUtil.isBlank(resolved)) {
                resolved = StrUtil.nullToEmpty(b.getLinkUrl()).trim();
            }
            it.setLinkUrl(StrUtil.isBlank(resolved) ? null : resolved);
            out.add(it);
        }
        return out;
    }

    public String buildNoticeMarquee(String suffix) {
        return buildNoticeMarquee(suffix, "");
    }

    public String buildNoticeMarqueeFallback(String suffix) {
        return buildNoticeMarquee(suffix, "");
    }

    private static String buildNoticeMarquee(String suffix, String firstTitle) {
        String suf = StrUtil.nullToEmpty(suffix);
        if (StrUtil.isNotBlank(firstTitle)) {
            return StrUtil.isBlank(suf) ? firstTitle : firstTitle + suf;
        }
        if (StrUtil.isNotBlank(suf)) {
            return suf.trim();
        }
        return "欢迎莅临";
    }

    private static List<MiniHomeSiteIconItemVO> toShellSiteIconItems(List<CmsSiteIcon> rows) {
        if (rows == null || rows.isEmpty()) {
            return List.of();
        }
        List<MiniHomeSiteIconItemVO> out = new ArrayList<>();
        for (CmsSiteIcon e : rows) {
            String u = StrUtil.trimToEmpty(e.getIconUrl());
            if (u.isEmpty()) {
                continue;
            }
            MiniHomeSiteIconItemVO vo = new MiniHomeSiteIconItemVO();
            vo.setIconCode(StrUtil.trimToEmpty(e.getIconCode()));
            vo.setIconUrl(u);
            if (StrUtil.isNotBlank(vo.getIconCode())) {
                out.add(vo);
            }
        }
        return out;
    }
}
