package taurus.util;

import com.data.cache.GroupMemberCache;
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 java.util.*;

public class Util {
	public final static Random random = new Random();
	static {
		random.setSeed(System.currentTimeMillis());
	}

	/**
	 * list to TArray
	 * @param list
	 * @return
	 */
	public static final ITArray toTArray(List<Integer> list) {
		ITArray result = new TArray();
		for (Integer card : list) {
			result.addInt(card);
		}
		return result;
	}

	/**
	 * call checkCard
	 * @param eventCard
	 * @param cardList
	 * @return
	 */
	@Deprecated
	public static boolean cardInList(int eventCard, List<Integer> cardList) {
		return checkCard(eventCard, cardList);
	}

	@Deprecated
	public static final ITArray toMPGroup(List<int[]> list) {
		ITArray result = new TArray();
		for (int[] card : list) {
			result.addInt(card[0]);
		}
		return result;
	}

	/**
	 * list to mod 100 list
	 * @param list
	 * @return
	 */
	public static final List<Integer> toModList(List<Integer> list){
		List<Integer> tem = new ArrayList<Integer>();
		for(Integer card : list) {
			tem.add(card % 100);
		}
		return tem;
	}
	
	/**
	 * TArray to mod 100 list
	 * @param list
	 * @return
	 */
	public static final List<Integer> toModList(ITArray list){
		List<Integer> tem = new ArrayList<>();
		for(int i=0;i<list.size();++i) {
			tem.add(list.getInt(i) % 100);
		}
		return tem;
	}
	
	/**
	 * 获取已自己为起点的位置
	 * @param self_seat
	 * @param seat
	 * @param people_num
	 * @return
	 */
	public static final int getPos(int self_seat, int seat, int people_num) {
		int cur_seat = self_seat;
		int tem = seat - (cur_seat - 1);
		if (tem <= 0)
			tem += people_num;
		return tem - 1;
	}

	/**
	 * 获取列表中牌数量
	 * @param eventCard
	 * @param cardList
	 * @return
	 */
	public static final int cardNum(int eventCard, List<Integer> cardList) {
		int result = 0;
		for (Integer card : cardList) {
			if (card == eventCard) {
				result += 1;
			}
		}
		return result;
	}

	/**
	 * 检测牌是否存在
	 * @param eventCard
	 * @param cardList
	 * @return
	 */
	public static final boolean checkCard(int eventCard, List<Integer> cardList) {
		for (Integer card : cardList) {
			if (card == eventCard) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 检测牌数量
	 * @param eventCard
	 * @param cardList
	 * @param num
	 * @return
	 */
	public static final boolean checkCard(int eventCard, List<Integer> cardList, int num) {
		int result = 0;
		for (Integer card : cardList) {
			if (card == eventCard) {
				result++;
				if (result == num)
					return true;
			}
		}
		return false;
	}

	/**
	 * 检测牌数量并将满足条件的删除
	 * @param eventCard
	 * @param cardList
	 * @param num
	 * @return
	 */
	public static final boolean checkCardAndRomve(int eventCard, List<Integer> cardList, int num) {
		if (checkCard(eventCard, cardList, num)) {
			removeCard(cardList, eventCard, num);
			return true;
		}
		return false;
	}

	/**
	 * 获取每张牌的数量MAP
	 * @param cardList
	 * @return
	 */
	public static final Map<Integer, Integer> getCardNumMap(List<Integer> cardList) {
		Map<Integer, Integer> result = new HashMap<Integer, Integer>();
		for (Integer card : cardList) {
			if (!result.containsKey(card)) {
				result.put(card, 1);
			} else {
				int num = result.get(card);
				result.put(card, (num + 1));
			}
		}
		return result;
	}

	/**
	 * 检测牌数量
	 * @param map
	 * @param card
	 * @param num
	 * @return
	 */
	public static boolean cardNum(Map<Integer, Integer> map, int card, int num) {
		Integer _num = map.get(card);
		if (_num != null && _num >= num) {
			return true;
		}
		return false;
	}

	/**
	 * 移除指定数量的牌
	 * @param cardList
	 * @param card
	 * @param count
	 */
	public static final void removeCard(List<Integer> cardList, int card, int count) {
		int curCount = 0;
		for (int i = 0; i < cardList.size(); i++) {
			if (cardList.get(i) == card) {
				cardList.remove(i);
				i--;
				curCount++;
			}

			if (count == curCount) {
				return;
			}
		}
	}

	static final public void addCard(ITArray opcard, int card, int num) {
		for (int i = 0; i < num; ++i) {
			opcard.addInt(card);
		}
	}

	static final public void addCard(List<Integer> cardList, int card, int num) {
		for (int i = 0; i < num; ++i) {
			cardList.add(card);
		}
	}

	/**
	 * 获取今天凌晨0点时间戳
	 * @return
	 */
	public static int todayTimeSec() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		int timeSec = (int) (cal.getTimeInMillis() / 1000);
		return timeSec;
	}

	/**
	 * 根据时间戳获取指定当天凌晨10位时间戳
	 * @param timeSec
	 * @return
	 */
	public static int todayTimeSec(Long timeSec) {
		Calendar cal = Calendar.getInstance();
		// 指定日期
		cal.setTime(new Date(timeSec));
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return (int) (cal.getTimeInMillis() / 1000);
	}

	/**
	 *   msg ps : param:{},param:{}
	 *           ==> param: abc,param:abced  
	 * @param msg
	 * @param params 
	 * @return
	 */
	public static String stringFormat(String msg, Object... params) {
		try {
			if (StringUtil.isNotEmpty(msg)) {
				msg = msg.replaceAll("\\{}", "\\%s");
				return String.format(msg, params);
			}
		} catch (Exception e) {
			return "";
		}
		return "";
	}
	
//	public static void countLog(String key,int num,Jedis jedis) {
//		String day_key = key + ":d"+DateUtils.getBeginDay();
//		String week_key = key + ":w"+DateUtils.getBeginWeek();
//		String month_key = key + ":m"+DateUtils.getBeginMonth();
//		jedis.incrBy(day_key,num);
//		jedis.expire(day_key, 11*3600*24);
//		jedis.incrBy(week_key,num);
//		jedis.expire(week_key, 15*3600*24);
//		jedis.incrBy(month_key,num);
//		jedis.expire(month_key, 63*3600*24);
//	}
	
	/**
	 * 获取玩家体力值
	 * @param gid
	 * @param uid
	 * @return
	 */
	public static long readRedisHp(int gid,int uid) {
		String key = GroupMemberCache.genKey(gid, uid);
		String hp = Redis.use("group1_db10").hget(key, "hp");
		long cur_hp = StringUtil.isNotEmpty(hp) ? Long.parseLong(hp) : 0;
		return cur_hp;
	}
}
