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

import com.alibaba.fastjson.JSONObject;
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.combosses.pojo.ComBosses;
import com.td.boss.game.combosses.vo.BossesVo;
import com.td.boss.game.combosses.vo.ComBossesVo;
import com.td.boss.game.combosses.service.ComBossesService;
import com.td.boss.game.combossesbattle.pojo.ComBossesBattle;
import com.td.boss.game.combossesbattle.service.ComBossesBattleService;
import com.td.boss.game.comgeneralattributes.pojo.ComGeneralAttributes;
import com.td.boss.game.comgeneralattributes.service.ComGeneralAttributesService;
import com.td.boss.game.comusers.pojo.ComUsers;
import com.td.boss.game.comusers.service.ComUsersService;
import com.td.boss.game.comusers.vo.ComUsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/game/comBosses/")
public class ComBossesController extends CommonController<ComBossesVo, ComBosses, Integer> {
    @Autowired
    private ComBossesService comBossesService;

    @Autowired
    private ComGeneralAttributesService comGeneralAttributesService;

    @Autowired
    private ComBossesBattleService comBossesBattleService;

    @Autowired
    private ComUsersService comUsersService;


    @GetMapping("getTotalKill")
    public Result<Map> getTotalKill(@RequestParam("bossId") Integer bossId) {
        ComBosses comBosses = comBossesService.findById(bossId);
        Map map = new HashMap();
        if (comBosses == null) {
            return Result.of(null, false, ResultEnum.BOSS_DOES_NOT_EXIST.getMessage(),ResultEnum.BOSS_DOES_NOT_EXIST.getCode());
        }
        map.put("totalKill", comBosses.getAllHealth());
        return Result.of(map);
    }

    /**
     * 获取世界boss 属性，后台控制显示
     *
     * @param bossId
     * @return
     */
    @GetMapping("getWorldBossAttribute")
    public Result<Map> getWorldBossAttribute(@RequestParam("bossId") Integer bossId) {
        ComBosses comBosses = comBossesService.findById(bossId);
        Map map = new HashMap();
        if (comBosses == null) {
            return Result.of(null, false, ResultEnum.BOSS_DOES_NOT_EXIST.getMessage(),ResultEnum.BOSS_DOES_NOT_EXIST.getCode());
        }

        map.put("allHealth", comBosses.getAllHealth());
        map.put("restHealth", comBosses.getRestHealth());
        map.put("allTime", comBosses.getAllTime());
        map.put("restTime", comBosses.getRestTime());
        map.put("attributeType", comBosses.getAttributeType());

        return Result.of(map);
    }
    /**
     * 获取战斗中boss属性
     *
     * @param attributeType
     * @return
     */
    @GetMapping("getBattleBossAttribute")
    public Result<ComGeneralAttributes> getBattleBossAttribute(@RequestParam("attributeType") Integer attributeType) {

        ComGeneralAttributes comBosses = comGeneralAttributesService.findByType(attributeType);
        if (comBosses == null) {
            return Result.of(null, false, ResultEnum.BOSS_DOES_NOT_EXIST.getMessage(),ResultEnum.BOSS_DOES_NOT_EXIST.getCode());
        }
        return Result.of(comBosses);
    }

    /**
     * 获取全部 boss 总属性
     * @return
     */
    @GetMapping("getBossAllInfo")
    public Result<List<BossesVo>> getBossAllInfo() {
        List<ComBosses>  comBossesList = comBossesService.findAll();
        if (comBossesList.size() == 0) {
            return Result.of(null, false, ResultEnum.BOSS_DOES_NOT_EXIST.getMessage(),ResultEnum.BOSS_DOES_NOT_EXIST.getCode());
        }

       List<BossesVo> bossesVoList = comBossesList.stream().map(e->{
           BossesVo bossesVo = new BossesVo();
           //获取战斗相关属性
           ComGeneralAttributes generalAttributes = comGeneralAttributesService.findByType(e.getAttributeType());
           bossesVo.setName(e.getName());
           bossesVo.setAllHealth(e.getAllHealth());
           bossesVo.setRestHealth(e.getRestHealth());
           bossesVo.setAllTime(e.getAllTime());
           bossesVo.setRestTime(e.getRestTime());
           bossesVo.setAttributes(generalAttributes);
           return bossesVo;
        }).collect(Collectors.toList());

        return Result.of(bossesVoList);
    }

    /**
     * 进入战斗模式
     * @param userId
     * @param playerId
     * @param bossId
     * @return
     */
    @GetMapping("intoCombat")
    @Transactional
    public Result<Map> intoCombat(@RequestParam("userId") String userId,
                                    @RequestParam("playerId") Integer playerId,
                                    @RequestParam("bossId") Integer bossId) {
        //todo 目前用悲观锁处理com_boss的属性问题
        //1.判断用户体力是否充足
        ComUsersVo comUsersVo = comUsersService.findByUserId(userId);
        if(comUsersVo.getEnergy()<=0){
            return Result.of(null,false,ResultEnum.USER_ENERGY_IS_INSUFFICIENT.getMessage(),ResultEnum.USER_ENERGY_IS_INSUFFICIENT.getCode());
        }
        //2.进入格斗，comBoss 表中，即世界boss血量减一，进入战斗状态
        //计算boss，获取boss属性时候
        ComBosses comBosses = comBossesService.intoCombat(bossId);
        if (comBosses == null) {
            return Result.of(null, false, ResultEnum.BOSS_DOES_NOT_EXIST.getMessage(), ResultEnum.BOSS_DOES_NOT_EXIST.getCode());
        }
        if (comBosses.getAllHealth() <= 0) {
            return Result.of(null, false, ResultEnum.BOSS_HEALTH_IS_ZERO.getMessage(), ResultEnum.BOSS_HEALTH_IS_ZERO.getCode());
        }
        //更新体力值
        comUsersVo.setEnergy(comUsersVo.getEnergy() -1);
        comUsersService.save(comUsersVo);
        //3.bosses产生战斗时候，创建一个新数据
        ComBossesBattle comBossesBattle = new ComBossesBattle();
        comBossesBattle.setPlayerId(playerId);
        comBossesBattle.setIsState(0);//（1：成功，0：失败）默认是失败状态，击败boss后设置成功状态
        comBossesBattle.setIsBattle(0); //（1:战斗结束，0：战斗持续中）
        comBossesBattle.setReduceHp(1);
        comBossesBattle.setReduceEnergy(1); //消耗一点体力
        Long timeTemp = System.currentTimeMillis();
        BigInteger bigInt = new BigInteger(timeTemp.toString());
        comBossesBattle.setStartTime(bigInt);
        comBossesBattle = comBossesBattleService.addComBossesBattle(comBossesBattle);

        //返回部分参数
        Map userMap = new HashMap();
        userMap.put("energy",comUsersVo.getEnergy());
        Map battleStateMap = new HashMap();
        battleStateMap.put("isState",comBossesBattle.getIsState());
        battleStateMap.put("isBattle",comBossesBattle.getIsBattle());
        battleStateMap.put("reduceHP",comBossesBattle.getReduceHp());
        battleStateMap.put("reduceEnergy",comBossesBattle.getReduceEnergy());
        battleStateMap.put("startTime",comBossesBattle.getStartTime());
        Map map = new HashMap();
        map.put("userInfo",userMap);
        map.put("battleState",battleStateMap);
//        map.put("bosses",comBosses);

        return Result.of(map);
    }

}
