package com.hotwj.platform.reportStatistics.provider.security;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hotwj.platform.common.mapper.PublicMapper;
import com.hotwj.platform.driverManagement.driver.mapper.HotDriverMapper;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import com.hotwj.platform.securityManagement.securityMeeting.domain.vo.HotSecurityMeetingVo;
import com.hotwj.platform.securityManagement.securityMeeting.service.IHotSecurityMeetingService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.OssService;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@RequiredArgsConstructor
public class OfflineMeetingProvider implements IReportDataProvider {

    private final IHotSecurityMeetingService securityMeetingService;
    private final PublicMapper publicMapper;
    private final OssService ossService;
    private final HotDriverMapper driverMapper;

    @Override
    public String getTemplateCode() {
        return "templates/securityManage/offlineMeeting.html.vm";
    }

    @Override
    public String getReportTitle() {
        return "线下会议记录表";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        Long meetingId = MapUtil.getLong(params, "meetingId");
        Map<String, Object> ctx = new HashMap<>();
        if (meetingId == null) {
            return ctx;
        }
        HotSecurityMeetingVo meeting = securityMeetingService.queryById(meetingId);
        if (meeting == null) {
            return ctx;
        }
        // 处理数据
        Map<String, Object> mMap = new HashMap<>();
        mMap.put("meetingTopic", StringUtils.blankToDefault(meeting.getMeetingTopic(), ""));
        mMap.put("startTime", meeting.getStartTime());
        mMap.put("endTime", meeting.getEndTime());
        mMap.put("location", StringUtils.blankToDefault(meeting.getLocation(), ""));
        mMap.put("hostName", StringUtils.blankToDefault(meeting.getHostName(), ""));
        mMap.put("recorderName", StringUtils.blankToDefault(meeting.getRecorderName(), ""));

        // 参会人员
        String participantNames = "";
        String pIds = meeting.getParticipantIds();
        if (StringUtils.isNotBlank(pIds)) {
            List<String> ids = Arrays.stream(pIds.split(","))
                    .filter(StringUtils::isNotBlank)
                    .map(String::trim)
                    .filter(StringUtils::isNotBlank)
                    .toList();
            if (!ids.isEmpty()) {
                try {
                    String names = publicMapper.selectAttendeeNamesByIds(ids);
                    if (StringUtils.isNotBlank(names)) {
                        participantNames = names;
                    }
                } catch (Exception ignored) {
                }
            }
        }
        mMap.put("participantNames", StringUtils.blankToDefault(participantNames, ""));

        // 会议内容
        mMap.put("meetingContent", StringUtils.blankToDefault(meeting.getMeetingContent(), ""));

        // 下月计划
        mMap.put("nextMonthPlan", StringUtils.blankToDefault(meeting.getNextMonthPlan(), ""));

        ctx.put("meeting", mMap);

        // 图片
        List<String> imgUrls = new ArrayList<>();
        if (StringUtils.isNotBlank(meeting.getPhotoAttachments())) {
            String photoJson = meeting.getPhotoAttachments();
            try {
                if (JSONUtil.isTypeJSON(photoJson)) {
                    JSONObject json = JSONUtil.parseObj(photoJson);
                    if (!json.isEmpty()) {
                        for (Object val : json.values()) {
                            String url = resolveMaybeOssUrl(String.valueOf(val));
                            if (StringUtils.isNotBlank(url)) {
                                imgUrls.add(url);
                            }
                        }
                    }
                } else {
                    for (String s : photoJson.split(",")) {
                        String url = resolveMaybeOssUrl(s);
                        if (StringUtils.isNotBlank(url)) {
                            imgUrls.add(url);
                        }
                    }
                }
            } catch (Exception e) {
                // ignore
            }
        }
        ctx.put("imgUrls", imgUrls);

        return ctx;
    }

    private String resolveMaybeOssUrl(String ossIdOrUrl) {
        if (StringUtils.isBlank(ossIdOrUrl)) {
            return null;
        }
        try {
            Long ossId = Long.parseLong(ossIdOrUrl);
            return ossService.selectUrlByIds(String.valueOf(ossId));
        } catch (NumberFormatException e) {
            return ossIdOrUrl;
        }
    }
}
