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

import com.alibaba.fastjson.JSONObject;
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.comcntorder.pojo.ComCntOrder;
import com.td.WaDa.game.comcntorder.service.ComCntOrderService;
import com.td.WaDa.game.complayers.pojo.ComPlayers;
import com.td.WaDa.game.complayers.vo.ComPlayersSimpleVo;
import com.td.WaDa.game.complayers.vo.ComPlayersVo;
import com.td.WaDa.game.complayers.service.ComPlayersService;
import com.td.WaDa.util.CopyUtil;
import com.td.WaDa.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
@RequestMapping("/game/comPlayers/")
public class ComPlayersController extends CommonController<ComPlayersVo, ComPlayers, Integer> {
    @Autowired
    private ComPlayersService comPlayersService;

    @Autowired
    private ComCntOrderService comCntOrderService;

    /**
     * 创建玩家
     *
     * @param userId
     * @return
     */
    //@PostMapping("createPlayer")
    //public Result<Map> createPlayer(@RequestParam(value = "userId") String userId,
    //                                @RequestParam(value = "name") String name,
    //                                @RequestParam(value = "avatar") String avatar) {
    //    //todo 疑问avatar获取渠道？
    //    ComPlayers player = comPlayersService.findByUserId(userId);
    //    if (player == null) {
    //        //player 不存在
    //        player = new ComPlayers();
    //        player.setUserId(userId);
    //        player.setAttributeType(0);
    //    }
    //    player.setAvatar(avatar);
    //    player.setName(name);
    //    ComPlayers addPlayer = comPlayersService.addComPlayers(player);
    //
    //    Map map = new HashMap();
    //    map.put("avatar", addPlayer.getAvatar());
    //    map.put("name", addPlayer.getName());
    //    map.put("attributeType", addPlayer.getAttributeType());
    //    return Result.of(map);
    //}

    /**
     * 获取玩家是否租赁过的信息
     *
     * @param userId
     * @return
     */
    @GetMapping("getPlayerExchangeState")
    public Result<Map> getPlayerExchangeState(@RequestParam(value = "userId") String userId) {
        ComPlayers player = comPlayersService.findByUserId(userId);
        if (player == null) {
            //player 不存在
            return Result.of(null, false, ResultEnum.PLAYER_DOES_NOT_EXIST.getMessage(), ResultEnum.PLAYER_DOES_NOT_EXIST.getCode());
        }

        Map map = new HashMap();
        map.put("isExchange", player.getIsExchange().equals(1) ? true : false);
        //返回一个是否租赁过的字段
        List<ComCntOrder> comCntOrderList = comCntOrderService.findByUserIdAndPayType(userId, 1);
        map.put("isLeased", comCntOrderList.size() > 0 ? true : false);
        return Result.of(map);
    }

    /**
     * 获取玩家信息
     *
     * @param userId
     * @return
     */
    //@GetMapping("getPlayer")
    //public Result<Map> getPlayer(@RequestParam(value = "userId") String userId) {
    //    ComPlayers player = comPlayersService.findByUserId(userId);
    //    if (player == null) {
    //        //player 不存在
    //        return Result.of(null, false, ResultEnum.PLAYER_DOES_NOT_EXIST.getMessage(), ResultEnum.PLAYER_DOES_NOT_EXIST.getCode());
    //    }
    //    Map map = new HashMap();
    //    map.put("playerId", player.getId());
    //    map.put("avatar", player.getAvatar());
    //    map.put("name", player.getName());
    //    map.put("attributeType", player.getAttributeType());
    //    return Result.of(map);
    //}

    /**
     * 获取玩家信息并且携带属性
     *
     * @param userId
     * @return
     */
    //@GetMapping("getPlayerAndBattleAttribute")
    //public Result<Map> getPlayerAndBattleAttribute(@RequestParam(value = "userId") String userId,
    //                                               @RequestParam(value = "name") String name,
    //                                               @RequestParam(value = "avatar") String avatar) {
    //    ComPlayers player = comPlayersService.findByUserId(userId);
    //    if (player == null) {
    //        // todo player 如果角色不存在的话，直接创建一个。
    //        player = new ComPlayers();
    //        player.setUserId(userId);
    //        player.setAttributeType(0);
    //        player.setAvatar(avatar);
    //        player.setName(name);
    //        player = comPlayersService.addComPlayers(player);
    //    }
    //    Map map = new HashMap();
    //    map.put("playerId", player.getId());
    //    map.put("avatar", player.getAvatar());
    //    map.put("name", player.getName());
    //    return Result.of(map);
    //}


    //@PostMapping("playerPushInfo")
    //public Result<Map> playerPushInfoFun(@RequestParam(value = "userId") String userId,
    //                                     @RequestParam(value = "playerData") String playerData) {
    //    ComPlayers player = comPlayersService.findByUserId(userId);
    //    if (player == null) {
    //        // todo player 如果角色不存在的话，直接创建一个。
    //        player = new ComPlayers();
    //        player.setUserId(userId);
    //        player.setAttributeType(0);
    //        player.setName("");
    //        player.setData(playerData);
    //        player.setIsExchange(0);
    //        player.setCreateTime(new Date());
    //        player = comPlayersService.addComPlayers(player);
    //    } else {
    //        player.setData(playerData);
    //        player = comPlayersService.addComPlayers(player);
    //    }
    //    player.setUpdateTime(new Date());
    //    //log.info(playerData);
    //    Map map = new HashMap();
    //    map.put("playerData", player.getData());
    //    return Result.of(map);
    //}

    /**
     * 拉取数据
     *
     * @param userId
     * @return
     */
    @GetMapping("playerPullInfo")
    public Result<?> playerPullInfoFun(@RequestParam(value = "userId") String userId) {

        ComPlayers player = comPlayersService.findByUserId(userId);
        //判断是否为空，初始化一个基础数据
        if (player == null) {
            Date _nowDate = DateUtil.getNowDate();
            player = new ComPlayers();
            player.setUserId(userId);
            player.setName("");
            player.setAvatar("");
            player.setAttributeType(0);
            player.setDiamond(0d);
            player.setIsExchange(0);
            player.setCreateTime(_nowDate);
            player.setUpdateTime(_nowDate);
            comPlayersService.addComPlayers(player);
        }
        return Result.of(CopyUtil.copy(player, ComPlayersSimpleVo.class));
    }

    /**
     * 组合游戏数据
     *
     * @return
     */
    private String PlayerData() {
        JSONObject object = new JSONObject();
        //BFirstLoadGame: 1,//只要push数据，就证明已经开始第一次游戏了
        object.put("BFirstLoadGame", 0);
        //date
        object.put("GameYear", 0);
        object.put("GameMonth", 0);
        object.put("GameDay", 0);
        object.put("GameDate", "0000/00/01");

        /**
         * 新增 CNT 和 SNB
         */
        object.put("CNT", 0);
        object.put("SNB", 0);
        object.put("Golden", 2000);
        object.put("Diamond", 100);
        object.put("WorkerLV", 0);
        object.put("WorkerNum", 0);

        //this.CharacterInfoArray = [];
        object.put("WorkerCapacity", 5);
        object.put("TerritoryStateArray", new int[]{1, 1, 1, 1, 1, 1, 1, 1});//全开地图

        /**
         * 添加建筑往后添加，到时候ManageBuildings 脚本 会初始化根据顺序
         * BuildingStateArray---相关 GetBuildingStateArray 获取建筑状态
         * BuildingNumArray  ---相关 GetBuildingNumArray 获取建筑数量
         * BuildingFrameArray ---相关 ManageUI  添加面板是否解锁之类的相关预制
         */
        object.put("BuildingStateArray", new int[]{1, //公路
                1,  //路铲
                1,  //拆迁
                1,  //农舍
                1,  //单元楼
                1,  //别墅
                1, //农田
                1,//伐木场
                1, //矿坑
                1, //加工厂
                1,//冷饮摊
                1,//贩卖机
                1,//面包房
                1,//早餐车
                1, //饮茶店
                1,//点心店
                1, //美食店
                1,//西餐厅
                1, //花店
                1, //美发店
                1,//洋装店
                1, //珠宝店
                1, //电影院
                1,//路灯
                1, //绿化带
                1, //花坛
                1,//喷泉
                1, //警察局
                1,//游乐场
                1,  //蓝色城堡
                1, //粉色城堡
                1, //Holy Farmland
                1 //Holy Farmland seed
        });
        object.put("BuildingLockStateArray", new int[]{1, //公路
                1,  //路铲
                1,  //拆迁
                1,  //农舍
                0,  //单元楼
                0,  //别墅
                1, //农田
                0,//伐木场
                0, //矿坑
                1, //加工厂
                0,//冷饮摊
                0,//贩卖机
                0,//面包房
                0,//早餐车
                0, //饮茶店
                0,//点心店
                0, //美食店
                0,//西餐厅
                0, //花店
                0, //美发店
                0,//洋装店
                0, //珠宝店
                0, //电影院
                1,//路灯
                1, //绿化带
                1, //花坛
                0,//喷泉
                0, //警察局
                0,//游乐场
                0,  //蓝色城堡
                0, //粉色城堡
                1, //Holy Farmland
                1, //Holy Farmland seed
        });
        //建筑物数量
        object.put("BuildingNumArray", new int[]{
                        0, //公路
                        0,  //路铲
                        0,  //拆迁
                        4,  //农舍
                        1,  //单元楼
                        1,  //别墅
                        4, //农田
                        1,//伐木场
                        1, //矿坑
                        2, //加工厂
                        2,//冷饮摊
                        1,//贩卖机
                        1,//面包房
                        1,//早餐车
                        1, //饮茶店
                        1,//点心店
                        1, //美食店
                        1,//西餐厅
                        1, //花店
                        1, //美发店
                        1,//洋装店
                        1, //珠宝店
                        1, //电影院
                        1,//路灯
                        1, //绿化带
                        1, //花坛
                        1,//喷泉
                        1, //警察局
                        1,//游乐场
                        1,  //蓝色城堡
                        1,  //粉色城堡
                        0, //Holy Farmland
                        0, //Holy Farmland seed
                }
        );

        //买建筑费用
        // this.DiamondNumArray = AConfig.DiamondArray;
        int[] _diamondNumArray = new int[]{
                0,//公路
                0, //路铲
                0, //拆迁
                40, //农舍
                100, //单元楼
                1000, //别墅

                50, //农田
                500,//伐木场
                1000,//矿坑
                500,//加工厂

                100,//冷饮摊
                100,//贩卖机
                150,//面包房
                200,//早餐车
                200, //饮茶店
                300,//点心店
                300, //美食店
                350,//西餐厅
                350, //花店
                350, //美发店
                500,//洋装店
                5000, //珠宝店
                10000, //电影院
                100,//路灯
                100, //绿化带
                200, //花坛
                300,//喷泉
                1000, //警察局
                1000,//游乐场
                5000,  //蓝色城堡
                5000, //粉色城堡

                2500, //Holy Farmland cnt
                0 //Holy Farmland seed
        };
        //todo 钻石消耗默认值,设置为0
        //for(int i = 0;i<_diamondNumArray.length;i++) {
        //    _diamondNumArray[i]=0;
        //}
        //StringUtils.join(_diamondNumArray,"_")
        object.put("DiamondNumArray", _diamondNumArray);
        //StringUtils.join(new int[]{0, 0, 0, 0, 0, 0, 0},"_")
        object.put("EveryDayRewardsArray", new int[]{0, 0, 0, 0, 0, 0, 0});
        object.put("LastTimeEveryDayRewardsDate", "0000/00/00");
        object.put("LastTimeLuckDate", "0000/00/00");
        object.put("FoodTradeState", 1);
        object.put("WoodTradeState", 1);
        object.put("MineralTradeState", 1);

        //转盘分享给钻石金币
        object.put("shareGive", new int[]{200, 5});
        //签到分享给钻石金币
        object.put("signInGive", new int[]{200, 5});
        //每个月 给的 低保
        object.put("EveryGive", new int[]{500, 10});
        //公共分享
        object.put("publicGive", new int[]{200, 50});
        //每天抽奖次数
        object.put("LotteryTimes", 10);

        //object.put("TaskIconCountClick",-1);


        return object.toString();
    }
}
