package taurus.util;

import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;
import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.plugin.redis.Redis;
import redis.clients.jedis.Jedis;

import java.util.*;

public class GroupDatas {

    public static TObject getData() {
        Jedis jedis0 = Redis.use("group1_db0").getJedis();
        Jedis jedis11 = Redis.use("group1_db11").getJedis();

        TObject param = TObject.newInstance();

        //存储所有有配置机器人的玩法id
        Set<Integer> pids = new HashSet<>();



        //存储可配机器人圈子
        Set<Integer> gids = new HashSet<>();


        TArray groupid = TArray.newInstance();
        TArray robotid = TArray.newInstance();
        TArray playid = TArray.newInstance();

        //圈子对应的房间

        try {
            //取出可用的机器人
            String sql = String.format("SELECT id FROM `account` WHERE jiqiren=9998 and start = 0");
            ITArray robotIds = DataBase.use().executeQueryByTArray(sql);

            for (int i = 0; i < robotIds.size(); i++) {
                ITObject robot = robotIds.getTObject(i);
                Integer robotId = robot.getInt("id");
                robotid.addInt(robotId);
            }


            //获取所有圈子
            Set<String> groupIds = jedis11.keys("group:*");
            for (String group : groupIds) {
                String groupId = group.substring(group.indexOf(":") + 1);
                String groupKey = "g{" + groupId + "}:play:*";
                Set<String> playIds = jedis11.keys(groupKey);

                //圈子下面的玩法
                for (String pid : playIds) {
                    String playId = pid.substring(pid.indexOf("play:") + 5);
                    String playKey = "g{" + groupId + "}:play:" + playId;

                    if (jedis11.hget(playKey, "leftover_robot") == null){
                        if (jedis11.hget(playKey, "shangxian_robot") != null){
                            String shangxian_robot = jedis11.hget(playKey, "shangxian_robot");
                            jedis11.hset(playKey, "leftover_robot", shangxian_robot);
                        }
                    }


                    String leftover_robot = "0";
                    if (jedis11.hget(playKey, "leftover_robot")!=null){
                        leftover_robot = jedis11.hget(playKey, "leftover_robot");
                    }
                    //有配置机器人的玩法
                    if ( Integer.parseInt(leftover_robot)!=0) {
                        pids.add(Integer.parseInt(playId));
                        gids.add(Integer.parseInt(groupId));
                    }
                }


            }

            Map<String, List<Integer>> groupRoomMap = new HashMap<>();
            for (Integer gid : gids){
                for (Integer pid : pids){
                    String key = "g{" + gid + "}:play:" + pid;

                    if (jedis11.hget(key, "groupId")!=null){
                        String groupId = jedis11.hget(key, "groupId");
                        if (gid==Integer.parseInt(groupId)){
                            groupRoomMap.computeIfAbsent(String.valueOf(gid), k -> new ArrayList<>()).add(pid);
                        }
                    }

                }

            }
            for (Map.Entry<String, List<Integer>> entry : groupRoomMap.entrySet()) {
                TObject groupPlay = TObject.newInstance();
                groupPlay.putTArray(entry.getKey(), TArray.newFromJsonData(entry.getValue().toString()));
                groupid.addTObject(groupPlay);
            }
            param.putTArray("groupid", groupid);
            param.putTArray("robotIds", robotid);


        } catch (Exception e) {
            System.out.println(e);
        } finally {
            jedis0.close();
            jedis11.close();
        }
        return param;
    }

}
