package com.data.cache;

import java.util.Map.Entry;
import java.util.Set;

import com.data.bean.BaseBean;
import com.data.bean.GroupPlayBean;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;
import com.taurus.core.plugin.redis.Redis;

import com.taurus.core.util.StringUtil;
import redis.clients.jedis.Jedis;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GroupPlayCache extends BaseCache{
	private ITArray playList;
	private ITArray simpPlayList;
	private int groupId;
	private long last_time;
	private int pay_type;
	private int gameType;
	
	public GroupPlayCache(int groupId,int pay_type) {
		super("g{"+groupId+"}:play:","group1_db11");
		this.groupId = groupId;
		this.pay_type = pay_type;
		playList = TArray.newInstance();
		simpPlayList = TArray.newInstance();
	}

	@Override
	protected BaseBean newBean() {
		GroupPlayBean p = new GroupPlayBean();
		p.pay_type = pay_type;
		return p;
	}

	/**
	 * update play data.
	 * @param play_data
	 */
	public void updatePlay() {
		playList.clear();
		simpPlayList.clear();
		Set<Entry<Integer, BaseBean>> set = this.mapById.entrySet();
		for(Entry<Integer, BaseBean> entry : set) {
			GroupPlayBean gp =(GroupPlayBean)entry.getValue();
			if(!gp.del) {
				playList.addTObject(gp.data);
				simpPlayList.addTObject(gp.simp_data);
			}else {
				this.mapById.remove(gp.id);
				this.mapByKey.remove(gp.redis_key);
			}
		}
		
	}
	
	/**
	 * 获取玩法列表
	 * @return
	 */
	public final ITArray getPlayList(int lev,int uid){
		/*if(System.currentTimeMillis() - last_time < 180000) {
			if(lev < 3) {
				return playList;
			}else {
				return simpPlayList;
			}
		}*/

		last_time = System.currentTimeMillis();
		playList.clear();
		simpPlayList.clear();
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		Jedis jedis1 = Redis.use("group1_db1").getJedis();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			String gpids_key = GroupCache.genPidsKey(groupId);
			Set<String> pids = jedis11.zrangeByScore(gpids_key, 10, 11);
			String key = "g{"+this.groupId + "}:play:";

			String gm_key = GroupMemberCache.genKey(groupId, uid);
			String gameids = jedis10.hget(gm_key, "games");
			if (StringUtil.isNotEmpty(gameids)){
				String[] games = gameids.split(",");
				List<String> list = new ArrayList<>(Arrays.asList(games));
				for(String tem : pids) {
					String gp_key = key+tem;
					GroupPlayBean gp = this.getBean(gp_key);
					if(gp!=null) {
						if (!list.contains(gp.gameId+"")){
							String gkey =  "game:"+gp.gameId ;
							int gameType = Integer.parseInt(jedis1.hget(gkey,"gameType"));
							//gp.gameType = gameType;
							gp.data.putInt("gameType",gameType);
							playList.addTObject(gp.data);
							simpPlayList.addTObject(gp.simp_data);
						}

					}
				}
			}else{
				for(String tem : pids) {
					String gp_key = key+tem;
					GroupPlayBean gp = this.getBean(gp_key);
					if(gp!=null) {
						String gkey =  "game:"+gp.gameId ;
						int gameType = Integer.parseInt(jedis1.hget(gkey,"gameType"));
						//gp.gameType = gameType;
						gp.data.putInt("gameType",gameType);
						playList.addTObject(gp.data);
						simpPlayList.addTObject(gp.simp_data);
					}
				}
			}

		}finally {
			jedis11.close();
			jedis1.close();
			jedis10.close();
		}


//		if(lev < 3) {
//			return playList;
//		}else {
//			return simpPlayList;
//		}

		return playList;
	}
}
