package com.td.WaDa.game.comsetting.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.td.WaDa.common.controller.*;
import com.td.WaDa.common.pojo.Result;
import com.td.WaDa.config.enums.ResultEnum;
import com.td.WaDa.game.comsetting.pojo.ComSetting;
import com.td.WaDa.game.comsetting.vo.ComSettingVo;
import com.td.WaDa.game.comsetting.service.ComSettingService;
import com.td.WaDa.game.excelcharacterattri.vo.ExcelCharacterAttriSimpleVo;
import com.td.WaDa.game.excelcharacterattri.vo.ExcelCharacterAttriVo;
import com.td.WaDa.game.exceldrop.service.ExcelDropService;
import com.td.WaDa.game.exceldrop.vo.ExcelDropVo;
import com.td.WaDa.game.excelprop.service.ExcelPropService;
import com.td.WaDa.game.excelprop.vo.ExcelPropSimpleVo;
import com.td.WaDa.game.excelprop.vo.ExcelPropVo;
import com.td.WaDa.game.excelscene.service.ExcelSceneService;
import com.td.WaDa.game.excelscene.vo.ExcelSceneSimpleVo;
import com.td.WaDa.game.excelscene.vo.ExcelSceneVo;
import com.td.WaDa.util.CopyUtil;
import com.td.WaDa.util.ExcelDataUtil;
import com.td.WaDa.util.RedisSettingMap;
import com.td.WaDa.game.excelcharacterattri.pojo.ExcelCharacterAttri;
import com.td.WaDa.game.excelcharacterattri.service.ExcelCharacterAttriService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/game/comSetting/")
@Slf4j
public class ComSettingController extends CommonController<ComSettingVo, ComSetting, String> {
    @Autowired
    private ComSettingService comSettingService;
    @Autowired
    private RedisSettingMap redisSettingMap;

    @Autowired
    private ExcelCharacterAttriService excelCharacterAttriService;

    @Autowired
    private ExcelSceneService excelSceneService;

    @Autowired
    private ExcelPropService excelPropService;

    @Autowired
    private ExcelDropService excelDropService;

    /**
     * 获取游戏设置
     *
     * @param userId
     * @return
     */
    @GetMapping("getGameSetting")
    public Result<ComSettingVo> getGameSettingFunction(@RequestParam(value = "userId") String userId) {
        //获取数据库相关配置
        ComSettingVo comSettingVo = redisSettingMap.getComSettingMap();//comSettingService.get("1").getData();
        if (comSettingVo.equals(null)) {
            return Result.of(null, false, ResultEnum.SETTING_IS_NULL.getMessage(), ResultEnum.SETTING_IS_NULL.getCode());
        }
        return Result.of(comSettingVo);
    }


    @ResponseBody
    @RequestMapping(value = "/excelImport")
    public Result<?> importExcel(@RequestParam("file") MultipartFile file) {
        int count = 0;
        if (file.isEmpty()) {
            System.out.println("文件为空！");
            Result.of(count);
        }
        // 获取上传文件输入流
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (Exception e) {
            //return ResponseData.fail(ResponseCodeEnum.ERROR_PARAM_INVALID);
            e.printStackTrace();
        }
        log.info("file.getName():" + file.getOriginalFilename());
        ExcelReader excelReader = ExcelUtil.getReader(inputStream);
        //暂时按照名字区分

        switch (file.getOriginalFilename().replace(".xlsx","")) {
            case "UI界面":

                return null;
            case "帮助":

                return null;
            case "参数":

                return null;
            case "产出表":

                return null;
            case "场景":
                List<ExcelSceneVo> excelSceneVoList = ExcelDataUtil.setScene(excelReader);
                for (ExcelSceneVo objects : excelSceneVoList) {
                    excelSceneService.save(objects);
                }
                return Result.of(CopyUtil.copyList(excelSceneVoList, ExcelSceneSimpleVo.class));
            case "道具":
                List<ExcelPropVo> excelPropVoList = ExcelDataUtil.setProp(excelReader);
                for (ExcelPropVo objects : excelPropVoList) {
                    excelPropService.save(objects);
                }
                return Result.of(CopyUtil.copyList(excelPropVoList, ExcelPropSimpleVo.class));
            case "掉落":
                List<ExcelDropVo> excelDropVoList = ExcelDataUtil.setDrop(excelReader);
                for (ExcelDropVo objects : excelDropVoList) {
                    excelDropService.save(objects);
                }
                return Result.of(CopyUtil.copyList(excelDropVoList, ExcelPropSimpleVo.class));
            case "多语言":

                return null;
            case "挂机收益":

                return null;
            case "互动事件":

                return null;
            case "技能":

                return null;
            case "建筑升级":

                return null;
            case "角色等级":

                return null;
            case "角色生成":

                return null;
            case "角色属性":
                List<ExcelCharacterAttriVo> characterReaderList = ExcelDataUtil.setCharacterAttributes(excelReader);
                for (ExcelCharacterAttriVo objects : characterReaderList) {
                    excelCharacterAttriService.save(objects);
                }
                return Result.of(CopyUtil.copyList(characterReaderList, ExcelCharacterAttriSimpleVo.class));
            case "角色刷新":

                return null;
            case "路径":

                return null;
            case "模型":

                return null;
            case "文字":

                return null;
            case "引导":

                return null;
            case "资源":

                return null;

        }
        return Result.of(null);
    }

}
