package com.gh.common.util;

import android.support.v4.util.ArrayMap;

import com.gh.common.constant.Config;

import java.util.regex.Pattern;

public class TimestampUtils {

	// 基准时间
	private static final long BASE = 1426608000000L;

	private static ArrayMap<String, Integer> map;

	/*
	 * 根据CD获取时间戳
	 */
	public static long getTimestamp(String url, int cd) {
		if (map == null){
			map = new ArrayMap<>();
			map.put("^" + Config.HOST + "v2/version" + "$", 5);
			map.put("^" + Config.HOST + "v2/disclaimer" + "$", 10);
			map.put("^" + Config.HOST + "v2/stat/download" + "$", 15);
			map.put("^" + Config.HOST + "v2/version\\?version_name=.+\\&channel=.+" + "$", 20);
			map.put("^" + Config.HOST + "v1d45/news\\?type_group=.+\\&offset=.+\\&limit=20" + "$", 25);
			map.put("^" + Config.HOST + "v1d45/news/.+/digest" + "$", 30);
			map.put("^" + Config.HOST + "v1d45/news/.+/detail" + "$", 35);
			map.put("^" + Config.HOST + "v1d45/news/.+/suggestion" + "$", 40);
			map.put("^" + Config.HOST + "v1d45/game/remenkapai" + "$", 45);
			map.put("^" + Config.HOST + "v1d45/game/.+/news\\?limit=3" + "$", 50);
			map.put("^" + Config.HOST + "v1d45/game/.+/news\\?limit=20\\&offset=.+" + "$", 55);
			map.put("^" + Config.HOST + "v1d45/game/.+/news\\?limit=20\\&offset=.+\\&type=.+" + "$", 60);
			map.put("^" + Config.HOST + "v1d45/search/game/default" + "$", 65);
			map.put("^" + Config.HOST + "v1d45/search/game\\?keyword=.+" + "$", 70);
			map.put("^" + Config.HOST + "v1d45/search/news\\?game_id=.+\\&keyword=.+\\&page=.+\\&limit=20" + "$", 75);
			map.put("^" + Config.HOST + "v1d45/support/setting/ui" + "$", 80);
			map.put("^" + Config.HOST + "v1d45/support/setting/platform" + "$", 85);
			map.put("^" + Config.HOST + "v1d45/support/package/.+/game/digest" + "$", 90);
			map.put("^" + Config.HOST + "v1d45/support/package/unused\\?skip=.+" + "$", 95);
			map.put("^" + Config.HOST + "v1d45/support/package/update\\?package=.+" + "$", 100);
			map.put("^" + Config.HOST + "v1d45/support/package/latest\\?package=.+" + "$", 105);
			map.put("^" + Config.HOST + "v1d45/support/setting/download/.+/switch2\\?version=.+" + "$", 110);
			map.put("^" + Config.HOST + "v1d46/support/suggestion" + "$", 115);
			map.put("^" + Config.HOST + "v1d50/index/slides" + "$", 120);
			map.put("^" + Config.HOST + "v1d50/game/chajian" + "$", 125);
			map.put("^" + Config.HOST + "v1d50/game/tuijian" + "$", 130);
			map.put("^" + Config.HOST + "v1d50/game/column/.+\\?page=.+" + "$", 135);
			map.put("^" + Config.HOST + "v1d50/game/danjiyouxi\\?limit=20\\&offset=.+" + "$", 140);
			map.put("^" + Config.HOST + "v1d50/news/zixun\\?limit=10\\&offset=.+" + "$", 145);
			map.put("^" + Config.HOST + "v1d50/news/yuanchuang\\?limit=10\\&offset=.+" + "$", 150);
			map.put("^" + Config.HOST + "v2d0/game/.+/digest" + "$", 155);
			map.put("^" + Config.HOST + "v2d0/game/.+/detail" + "$", 160);
			map.put("^" + Config.HOST + "v2d0/game/.+/news_digest" + "$", 165);
			map.put("^" + Config.HOST + "v2d0/zixun/guanzhu\\?key=.+\\&limit=10\\&offset=.+" + "$", 170);
			map.put("^" + Config.HOST + "v2d0/zixun/guanzhu\\?game_id=.+\\&limit=10\\&offset=.+" + "$", 175);
			map.put("^" + Config.HOST + "v2d0/device/.+/concern" + "$", 180);
			map.put("^" + Config.HOST + "v2d0/device/.+/concern/.+" + "$", 185);
		}
		long base = BASE;
		for (String key : map.keySet()){
			if (Pattern.matches(key, url)) {
				Utils.log("url add base = " + url);
				base += map.get(key);
				break;
			}
		}
		long now = System.currentTimeMillis();
		Utils.log("url now timestamp = " + now);
		return (long) (Math.ceil((now - base) / cd) * cd + base);
	}

	/*
	 * 为url添加timestamp
	 */
	public static String addTimestamp(String url, int cd) {
		if (url.contains("?")) {
			String u = url + "&timestamp=" + getTimestamp(url, cd);
			Utils.log("url = " + u);
			return u;
		} else {
			String u = url + "?timestamp=" + getTimestamp(url, cd);
			Utils.log("url = " + u);
			return u;
		}
	}
	
	/*
	 * 去除url中的timestamp
	 */
	public static String removeTimestamp(String url) {
		int index = url.lastIndexOf("timestamp");
		String params = url.substring(index);
		//连接符
		String connector = url.substring(index - 1, index);
		String key = url.substring(0, index - 1);
		index = params.indexOf("&");
		if (index != -1) {
			key = key + connector + params.substring(index + 1);
		}
		return key;
	}

}
