package com.ruoyi.web.modules.technology.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.technology.domain.OnlineTrainingCourseDetailVo;
import com.ruoyi.web.modules.technology.domain.OnlineTrainingCourseListVo;
import com.ruoyi.web.modules.technology.domain.OnlineTrainingCourseQuery;
import com.ruoyi.web.modules.technology.domain.OnlineTrainingCourseRow;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryItemVo;
import com.ruoyi.web.modules.technology.mapper.BizOnlineTrainingCourseMapper;
import com.ruoyi.web.modules.technology.service.IBizOnlineTrainingCourseService;
import com.ruoyi.web.modules.technology.support.OnlineTrainingCourseRules;

@Service
public class BizOnlineTrainingCourseServiceImpl implements IBizOnlineTrainingCourseService
{
    @Autowired
    private BizOnlineTrainingCourseMapper onlineTrainingCourseMapper;

    @Override
    public List<OnlineTrainingCourseListVo> selectOnlineTrainingCourseList(OnlineTrainingCourseQuery query)
    {
        OnlineTrainingCourseQuery effective = normalizeQuery(query);
        List<OnlineTrainingCourseRow> rows = onlineTrainingCourseMapper.selectOnlineTrainingCourseList(effective);
        if (rows == null || rows.isEmpty())
        {
            return Collections.emptyList();
        }
        List<OnlineTrainingCourseListVo> result = new ArrayList<>(rows.size());
        for (OnlineTrainingCourseRow row : rows)
        {
            result.add(toListVo(row));
        }
        return result;
    }

    @Override
    public OnlineTrainingCourseDetailVo selectOnlineTrainingCourseById(Long id)
    {
        if (id == null)
        {
            throw new ServiceException(OnlineTrainingCourseRules.NOT_FOUND_MSG);
        }
        OnlineTrainingCourseRow row = onlineTrainingCourseMapper.selectOnlineTrainingCourseById(id);
        if (row == null)
        {
            throw new ServiceException(OnlineTrainingCourseRules.NOT_FOUND_MSG);
        }
        return toDetailVo(row);
    }

    @Override
    public List<InformationCategoryItemVo> selectCourseTopics()
    {
        List<InformationCategoryItemVo> categories = onlineTrainingCourseMapper.selectDistinctCourseTopics();
        if (categories == null || categories.isEmpty())
        {
            return Collections.emptyList();
        }
        List<InformationCategoryItemVo> result = new ArrayList<>(categories.size());
        for (InformationCategoryItemVo item : categories)
        {
            if (item != null && StringUtils.isNotEmpty(item.getCode()))
            {
                result.add(item);
            }
        }
        return result;
    }

    private static OnlineTrainingCourseQuery normalizeQuery(OnlineTrainingCourseQuery query)
    {
        OnlineTrainingCourseQuery effective = query != null ? query : new OnlineTrainingCourseQuery();
        if (StringUtils.isNotEmpty(effective.getCourseTitle()))
        {
            effective.setCourseTitle(effective.getCourseTitle().trim());
        }
        if (StringUtils.isNotEmpty(effective.getCourseTopic()))
        {
            effective.setCourseTopic(effective.getCourseTopic().trim());
        }
        return effective;
    }

    private static OnlineTrainingCourseListVo toListVo(OnlineTrainingCourseRow row)
    {
        OnlineTrainingCourseListVo vo = new OnlineTrainingCourseListVo();
        vo.setId(row.getId());
        vo.setCourseTitle(row.getResourceName());
        vo.setCourseIntro(row.getIntroduction());
        vo.setCourseTopic(row.getCourseTopic());
        vo.setVideoFileName(OnlineTrainingCourseRules.resolveVideoFileName(row.getVideoFilePath()));
        vo.setCreateTime(row.getCreateTime());
        return vo;
    }

    private static OnlineTrainingCourseDetailVo toDetailVo(OnlineTrainingCourseRow row)
    {
        OnlineTrainingCourseDetailVo vo = new OnlineTrainingCourseDetailVo();
        vo.setId(row.getId());
        vo.setCourseTitle(row.getResourceName());
        vo.setCourseIntro(row.getIntroduction());
        vo.setCourseTopic(row.getCourseTopic());
        vo.setVideoFileUrl(row.getVideoFileUrl());
        vo.setVideoFilePath(row.getVideoFilePath());
        vo.setVideoFileName(OnlineTrainingCourseRules.resolveVideoFileName(row.getVideoFilePath()));
        vo.setCoverFileUrl(row.getCoverFileUrl());
        vo.setCoverFilePath(row.getCoverFilePath());
        vo.setPublishTime(row.getPublishTime());
        vo.setCreateTime(row.getCreateTime());
        vo.setUpdateTime(row.getUpdateTime());
        return vo;
    }
}
