package cn.iocoder.byzs.module.web.controller.admin.blockly;

import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.byzs.module.blockly.controller.admin.blockly.vo.BlocklyPageReqVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyType.vo.BlocklyTypeListReqVO;
import cn.iocoder.byzs.module.blockly.controller.admin.blocklyType.vo.BlocklyTypeRespVO;
import cn.iocoder.byzs.module.blockly.dal.dataobject.blocklyType.BlocklyTypeDO;
import cn.iocoder.byzs.module.blockly.service.blockly.BlocklyService;
import cn.iocoder.byzs.module.blockly.service.blocklyType.BlocklyTypeService;
import cn.iocoder.byzs.module.web.controller.admin.blockly.vo.WebBlocklyTypeVO;
import cn.iocoder.byzs.module.web.controller.admin.blockly.vo.WebBlocklyVO;
import cn.iocoder.byzs.module.web.service.blockly.WebBlocklyServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;

@Tag(name = "Web-前端接口")
@RestController
@RequestMapping("/bjdxWeb/blockly")
@Validated
public class WebBlocklyController {

    @Resource
    private BlocklyService blocklyService;
    @Resource
    private WebBlocklyServiceImpl webBlocklyService;
    @Resource
    private BlocklyTypeService blocklyTypeService;

    @GetMapping("/getTypeTheme")
    @Operation(summary = "获得blockly主题")
    public CommonResult<List<BlocklyTypeRespVO>> getTypeTheme() {
        BlocklyTypeListReqVO listReqVO = new BlocklyTypeListReqVO().setCtTypeNode("0");
        List<BlocklyTypeDO> list = blocklyTypeService.getBlocklyTypeList(listReqVO);
        List<BlocklyTypeRespVO> listVo = BeanUtils.toBean(list, BlocklyTypeRespVO.class);
        return success(listVo);
    }

    @GetMapping("/getTypeByThemeId")
    @Operation(summary = "获得blockly课程类型-根据主题id")
    public CommonResult<List<WebBlocklyTypeVO>> getTypeByThemeId(@RequestParam("id") Long themeId) {
        List<WebBlocklyTypeVO> list = webBlocklyService.getTypeByThemeId(themeId);
        return success(list);
    }

    @GetMapping("/getBlocklyByTypeId")
    @Operation(summary = "获得blockly课程-根据课程类型id")
    public CommonResult<List<WebBlocklyVO>> getBlocklyByTypeId(@RequestParam("typeId") Long typeId) {
        List<WebBlocklyVO> blocklyVoList = webBlocklyService.getBlocklyVoByTypeId(typeId);
        return success(blocklyVoList);
    }

    @GetMapping("/getBlocklyTypeTree")
    @Operation(summary = "获取课程类型树结构(配置权限使用)")
    public CommonResult<List<TreeNode>> getBlocklyTypeTree() {
        // 获取所有课程类型
        List<BlocklyTypeDO> allBlocklyTypes = blocklyTypeService.getBlocklyTypeList(new BlocklyTypeListReqVO());
        // 获取所有课程
//        List<BlocklyPageReqVO> allBlocklys = blocklyService.getBlocklyList(new BlocklyPageReqVO());
        
        // 构建课程类型ID到课程类型的映射
        Map<Long, BlocklyTypeDO> blocklyTypeMap = new HashMap<>();
        for (BlocklyTypeDO blocklyType : allBlocklyTypes) {
            blocklyTypeMap.put(blocklyType.getId(), blocklyType);
        }
        
        // 构建课程类型树节点
        List<TreeNode> treeNodes = new ArrayList<>();
        
        // 处理根节点 (parentId = 0)
        for (BlocklyTypeDO blocklyType : allBlocklyTypes) {
            if (BlocklyTypeDO.CT_PARENT_ID_ROOT.equals(blocklyType.getCtParentId())) {
                TreeNode node = new TreeNode();
                node.setId(blocklyType.getId());
                node.setParentId(blocklyType.getCtParentId());
                node.setName(blocklyType.getCtType() + "（主题）");
                node.setType("主题");
                treeNodes.add(node);
            }
        }
        
        // 递归添加子节点
        for (TreeNode node : treeNodes) {
            addChildNodes(node, allBlocklyTypes, blocklyTypeMap);
        }
        
        return success(treeNodes);
    }
    
    /**
     * 递归添加子节点
     */
    private void addChildNodes(TreeNode parentNode, List<BlocklyTypeDO> allBlocklyTypes, Map<Long, BlocklyTypeDO> blocklyTypeMap) {
        List<TreeNode> children = new ArrayList<>();
        
        // 添加子课程类型
        for (BlocklyTypeDO blocklyType : allBlocklyTypes) {
            if (parentNode.getId().equals(blocklyType.getCtParentId())) {
                TreeNode childNode = new TreeNode();
                childNode.setId(blocklyType.getId());
                childNode.setParentId(blocklyType.getCtParentId());
                childNode.setName(blocklyType.getCtType());
                childNode.setType("blocklyType");
                children.add(childNode);
                
                // 递归添加子节点的子节点
//                addChildNodes(childNode, allBlocklyTypes, allBlocklys, blocklyTypeMap);
            }
        }
        
        // 添加关联的课程
//        for (BlocklyPageReqVO blockly : allBlocklys) {
//            if (parentNode.getId().equals(blockly.getBcType())) {
//                TreeNode childNode = new TreeNode();
//                childNode.setId(blockly.getId());
//                childNode.setParentId(blockly.getBcType());
//                childNode.setName(blockly.getBcName());
//                childNode.setType("blockly");
//                children.add(childNode);
//            }
//        }
        
        parentNode.setChildren(children);
    }
    
    /**
     * 树节点类，用于表示课程类型树结构
     */
    @Data
    public static class TreeNode {
        private Long id;
        private Long parentId;
        private String name;
        private String type; // blocklyType 或 blockly
        private List<TreeNode> children = new ArrayList<>();
    }
}