package org.dromara.system.controller;

import cn.idev.excel.EasyExcel;
import cn.idev.excel.annotation.ExcelProperty;
import cn.idev.excel.context.WriteContext;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.ibatis.javassist.bytecode.AnnotationsAttribute;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.EnrollProjectVo;
import org.dromara.system.domain.vo.GameAthleteVo;
import org.dromara.system.service.IEnrollService;
import org.dromara.system.service.IGameEventProjectService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 报名
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/enroll")
public class EnrollController {

    private final IEnrollService enrollService;

    // @PostMapping("/importTemplate")
    // public void importTemplate(HttpServletResponse response) throws IOException {
    //     // 获取动态项目(赛事项目)
    //     Map<String, ArrayList<String>> projectMap = gameEventProjectService.mapProjectTypeAndProject();
    //     List<String> project = new ArrayList<>();
    //     for (Map.Entry<String, ArrayList<String>> entry : projectMap.entrySet()) {
    //         project.addAll(entry.getValue());
    //     }
    //     // 反射构建属性
    //     Class<EnrollProjectVo> enrollClass = EnrollProjectVo.class;
    //     String var = "var";
    //     AtomicInteger count = new AtomicInteger(1);
    //     for (String p : project) {
    //         try {
    //             Field newField = enrollClass.getDeclaredField(var + count.addAndGet(1));
    //             //给字段添加 @ExcelProperty 注解,value为p
    //         } catch (NoSuchFieldException e) {
    //             log.error("字段不存在:", p);
    //         }
    //     }
    //
    //     // 创建Excel工作簿
    //
    //
    //     // 输出文件到客户端
    //     ServletOutputStream out = response.getOutputStream();
    //     out.flush();
    //     out.close();
    // }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response, Long eventId) throws IOException {
        enrollService.downloadTemplate(response, eventId);
    }


    /**
     * 导入报名表
     */
    @PostMapping("/importData/{eventId}")
    public ResponseEntity<Map<String, Object>> importData(
        @RequestParam("file") MultipartFile file,
        @PathVariable Long eventId) {
        return enrollService.importData(file, eventId);
    }
}
