package com.td.boss.game.combossesdescribe.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.td.boss.common.controller.*;
import com.td.boss.common.pojo.Result;
import com.td.boss.config.enums.ResultEnum;
import com.td.boss.game.combossesdescribe.pojo.ComBossesDescribe;
import com.td.boss.game.combossesdescribe.vo.BossesDescribeVo;
import com.td.boss.game.combossesdescribe.vo.ComBossesDescribeVo;
import com.td.boss.game.combossesdescribe.service.ComBossesDescribeService;
import com.td.boss.game.comequiptype.pojo.ComEquipType;
import com.td.boss.game.comequiptype.service.ComEquipTypeService;
import com.td.boss.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/game/comBossesDescribe/")
public class ComBossesDescribeController extends CommonController<ComBossesDescribeVo, ComBossesDescribe, Integer> {
    @Autowired
    private ComBossesDescribeService comBossesDescribeService;

    @Autowired
    private ComEquipTypeService comEquipTypeService;

    @GetMapping("getComBossesDescribeList")
    public Result<List<BossesDescribeVo>> getComBossesDescribeList() {
        List<ComBossesDescribe> comBossesDescribeList = comBossesDescribeService.findAllByDescId();
        Map map = new HashMap();
        if (comBossesDescribeList.size() == 0) {
            return Result.of(null, false, ResultEnum.BOSS_DESCRIBE_LIST_ZERO.getMessage(), ResultEnum.BOSS_DESCRIBE_LIST_ZERO.getCode());
        }

        //获取装备类型
        List<ComEquipType> comEquipTypeList = comEquipTypeService.findAllComEquipType();

        List<BossesDescribeVo> bossesDescribeVoList = comBossesDescribeList.stream().map(e -> {
            BossesDescribeVo bossesDescribeVo = new BossesDescribeVo();
            bossesDescribeVo.setAllHealth(e.getAllHealth());
            bossesDescribeVo.setBossesId(e.getBossesId());
            bossesDescribeVo.setDescribe(e.getDescribe());
            bossesDescribeVo.setName(e.getName());
            bossesDescribeVo.setLevelName(e.getLevelName());
            bossesDescribeVo.setLevel(e.getLevel());
            List<ComEquipType> result = new ArrayList<>();
            Map<Integer, Integer> rewardEquips = JSONObject.parseObject(e.getRewardEquips(), new TypeReference<Map<Integer, Integer>>() {
            });
            Set<Map.Entry<Integer, Integer>> entries = rewardEquips.entrySet();
            for (Map.Entry<Integer, Integer> entry : entries) {
                //key 是comEquipType中的装备类型，value 是代表装备level，暂时可不用
//                System.out.println(entry.getKey() + ":" + entry.getValue());
                result.add(comEquipTypeList.stream().filter(type -> type.getType().equals(entry.getKey())).findFirst().get());
            }
            bossesDescribeVo.setRewardEquips(result);
            return bossesDescribeVo;
        }).collect(Collectors.toList());

        return Result.of(bossesDescribeVoList);
    }
}
