package cn.iocoder.byzs.module.blockly.service.blocklyConfig;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.module.bjdx.controller.admin.courseconfig.vo.CourseConfigRespVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.coursequestion.vo.CourseQuestionRespVO;
import cn.iocoder.byzs.module.bjdx.service.coursequestion.CourseQuestionService;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyConfig.vo.BlocklyConfigPageReqVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyConfig.vo.BlocklyConfigRespVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyConfig.vo.BlocklyConfigSaveReqVO;
import cn.iocoder.byzs.module.blockly.dal.dataobject.blocklyConfig.BlocklyConfigDO;
import cn.iocoder.byzs.module.blockly.dal.mysql.blocklyConfig.BlocklyConfigMapper;
import cn.iocoder.byzs.module.blockly.service.blocklyConfig.BlocklyConfigService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.byzs.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.byzs.module.blockly.enums.ErrorCodeConstants.BLOCKLY_CONFIG_NOT_EXISTS;

/**
 * 课程配置 Service 实现类
 *
 * @author 博雅智算源码
 */
@Service
@Validated
public class BlocklyConfigServiceImpl implements BlocklyConfigService {

    @Resource
    private BlocklyConfigMapper blocklyConfigMapper;
    @Resource
    private CourseQuestionService questionService;

    @Override
    public Long createBlocklyConfig(BlocklyConfigSaveReqVO createReqVO) {
        // 插入
        BlocklyConfigDO blocklyConfig = BeanUtils.toBean(createReqVO, BlocklyConfigDO.class);
        blocklyConfigMapper.insert(blocklyConfig);
        // 返回
        return blocklyConfig.getId();
    }

    @Override
    public void updateBlocklyConfig(BlocklyConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateBlocklyConfigExists(updateReqVO.getId());
        // 更新
        BlocklyConfigDO updateObj = BeanUtils.toBean(updateReqVO, BlocklyConfigDO.class);
        blocklyConfigMapper.updateById(updateObj);
    }

    @Override
    public void deleteBlocklyConfig(Long id) {
        // 校验存在
        validateBlocklyConfigExists(id);
        // 删除
        blocklyConfigMapper.deleteById(id);
    }

    @Override
        public void deleteBlocklyConfigListByIds(List<Long> ids) {
        // 校验存在
        validateBlocklyConfigExists(ids);
        // 删除
        blocklyConfigMapper.deleteByIds(ids);
        }

    private void validateBlocklyConfigExists(List<Long> ids) {
        List<BlocklyConfigDO> list = blocklyConfigMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(BLOCKLY_CONFIG_NOT_EXISTS);
        }
    }

    private void validateBlocklyConfigExists(Long id) {
        if (blocklyConfigMapper.selectById(id) == null) {
            throw exception(BLOCKLY_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public BlocklyConfigDO getBlocklyConfig(Long id) {
        return blocklyConfigMapper.selectById(id);
    }

    @Override
    public List<BlocklyConfigRespVO> getBlocklyConfigQuestion(Long ccCourseId) {
        //课程配置列表
        BlocklyConfigPageReqVO blocklyConfigPageReqVO = new BlocklyConfigPageReqVO();
        blocklyConfigPageReqVO.setCcCourseId(Math.toIntExact(ccCourseId))
                .setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BlocklyConfigPageReqVO> blocklyConfigPageReqVOS = blocklyConfigMapper.selectBlocklyConfigPage(blocklyConfigPageReqVO);

        //课程配置列表转VO
        List<BlocklyConfigRespVO> blocklyConfigList = blocklyConfigPageReqVOS.stream()
                .map(doItem -> BeanUtils.toBean(doItem, BlocklyConfigRespVO.class))
                .toList();
        if (blocklyConfigList.isEmpty()) {return blocklyConfigList;}

        //设置课程配置-试题列表(引用)
        Map<Long, CourseQuestionRespVO> questionMap = questionService.setCourseQuestionList(blocklyConfigList.stream().map(BlocklyConfigRespVO::getCcQuestId).collect(Collectors.toList()));
        blocklyConfigList.forEach(item -> item.setBlocklyQuestion(questionMap.get(item.getCcQuestId())));


        return blocklyConfigList;
    }

    @Override
    public List<BlocklyConfigRespVO> getBlocklyConfigQuestionByCourseIds(List<Long> ccCourseIds) {

        List<BlocklyConfigRespVO> blocklyConfigList = blocklyConfigMapper.getBlocklyConfigQuestionByCourseIds(ccCourseIds);

        //设置课程配置-试题列表(引用)
        Map<Long, CourseQuestionRespVO> questionMap = questionService.setCourseQuestionList(blocklyConfigList.stream().map(BlocklyConfigRespVO::getCcQuestId).collect(Collectors.toList()));
        blocklyConfigList.forEach(item -> item.setBlocklyQuestion(questionMap.get(item.getCcQuestId())));

        return blocklyConfigList;
    }

    @Override
    public PageResult<BlocklyConfigPageReqVO> getBlocklyConfigPage(BlocklyConfigPageReqVO pageReqVO) {
        pageReqVO.setPageNo((pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize());
        List<BlocklyConfigPageReqVO> blocklyConfigPageReqVOS = blocklyConfigMapper.selectBlocklyConfigPage(pageReqVO);
        Integer count = blocklyConfigMapper.selectBlocklyConfigCount(pageReqVO);

        // 调用服务层方法获取分页结果
        return new PageResult<>(
                blocklyConfigPageReqVOS.stream()
                       .map(doItem -> BeanUtils.toBean(doItem, BlocklyConfigPageReqVO.class))
                       .collect(Collectors.toList()),
                (long) count
        );
    }

}