package com.gh.gamecenter.manager;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.gh.common.util.PackageUtils;
import com.gh.gamecenter.entity.GameUpdateEntity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PackageManager {

	private Context context;
	private static ArrayList<GameUpdateEntity> updateList;
	private static ArrayMap<String, Boolean> installedMap;
	private static ArrayList<String> installedList;

	public PackageManager(Context context) {
		this.context = context;
	}

	/**
	 * 获取本地所有已安装的软件的包名的map
	 * 
	 * @return 已安装游戏列表
	 */
	public static ArrayMap<String, Boolean> getInstalledMap() {
		return installedMap;
	}
	
	public static ArrayList<String> getInstalledList() {
		return installedList;
	}

	/**
	 * 根据包名 判断是否安装该游戏
	 * 
	 * @param packageName
	 *            包名
	 * @return true 已安装 false 未安装
	 */
	public static boolean isInstalled(String packageName) {
		if (packageName == null) {
			return false;
		}
		if (installedMap == null) {
			return false;
		}
		Boolean b = installedMap.get(packageName);
		if (b != null) {
			return true;
		}
		return false;
	}

	/**
	 * 移除一个已安装的游戏
	 * 
	 * @param packageName
	 */
	public static void removeInstalled(String packageName) {
		installedMap.remove(packageName);
	}

	/**
	 * 添加一个已安装的游戏
	 * 
	 * @param packageName
	 */
	public static void addInstalled(String packageName) {
		installedMap.put(packageName, true);
	}

	public void initInstalledMap(ArrayList<String> list) {
		installedMap = new ArrayMap<>();
		installedList = new ArrayList<>();
		for (String str : list) {
			installedMap.put(str, true);
			installedList.add(str);
		}
	}

	public void getInstalledMapFromLocal() {
		installedMap = new ArrayMap<>();
		installedList = new ArrayList<>();
		ArrayList<String> list = PackageUtils.getAllPackageName(context);
		for (String str : list) {
			installedMap.put(str, true);
			installedList.add(str);
		}
	}

	/**
	 * 根据本地已安装的游戏列表，获取可更新游戏列表
	 * 
	 * @return 可更新游戏列表
	 */
	public static ArrayList<GameUpdateEntity> getUpdateList() {
		if (updateList == null) {
			updateList = new ArrayList<>();
		}
		return updateList;
	}

	public static int getPluggableSize() {
		if (updateList != null && updateList.size() != 0) {
			int pluggableSize = 0;
			for (GameUpdateEntity entity : updateList) {
				if (entity.isPluggable()) {
					pluggableSize++;
				}
			}
			return pluggableSize;
		}
		return 0;
	}

	public static boolean isCanUpdate(String packageName) {
		if (updateList != null && updateList.size() != 0) {
			for (GameUpdateEntity entity : updateList) {
				if (entity.getPackageName().equals(packageName)
						&& !entity.isPluggable()) {
					return true;
				}
			}
		}
		return false;
	}

	public static GameUpdateEntity getUpdateEntity(String packageName) {
		if (updateList != null && updateList.size() != 0) {
			for (GameUpdateEntity entity : updateList) {
				if (entity.getPackageName().equals(packageName)) {
					return entity;
				}
			}
		}
		return null;
	}

	public static boolean isContain(String packageName) {
		if (getUpdateList().isEmpty()) {
			return false;
		}
		for (int i = 0; i < getUpdateList().size(); i++) {
			if (getUpdateList().get(i).getPackageName().equals(packageName)) {
				return true;
			}
		}
		return false;
	}

	public static void addUpdate(GameUpdateEntity game) {
		if (!isContain(game.getPackageName())) {
			getUpdateList().add(game);
		}
	}

	public static void addUpdate(int index, GameUpdateEntity game) {
		if (getUpdateList().isEmpty() || !getUpdateList().get(0).getName().equals(game.getName())) {
			getUpdateList().add(index, game);
		}
	}
	
	public static void clearUpdateList() {
		getUpdateList().clear();
	}

	public static void setUpdateList(ArrayList<GameUpdateEntity> list) {
		getUpdateList().clear();
		for (int i = 0; i < list.size(); i++) {
			getUpdateList().add(list.get(i));
		}
	}
	
	public static void addUpdateList(ArrayList<GameUpdateEntity> list) {
		for (int i = 0; i < list.size(); i++) {
			getUpdateList().add(list.get(i));
		}
	}

	public static void removeUpdateList(String packageName) {
		for (int i = 0, size = getUpdateList().size(); i < size; i++) {
			if (packageName.equals(getUpdateList().get(i).getPackageName())) {
				getUpdateList().remove(i);
				break;
			}
		}
	}

	/**
	 * 获取所有已安装的软件的包名、版本(非系统应用)
	 *
	 * @return 已安装软件的包名的集合
	 */
	public JSONArray getAllPackageName2JSON() {
		FilterManager filterManager = new FilterManager(context);
		JSONArray jsonArray = new JSONArray();
		List<PackageInfo> packageInfos = context.getPackageManager()
				.getInstalledPackages(0);
		for (int i = 0, size = packageInfos.size(); i < size; i++) {
			PackageInfo packageInfo = packageInfos.get(i);
			if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
				if (!context.getPackageName().equals(packageInfo.packageName)) {
					if (!filterManager.isFilter(packageInfo.packageName)) {
						HashMap<String, String> params = new HashMap<>();
						params.put("package", packageInfo.packageName);
						if (TextUtils.isEmpty(packageInfo.versionName)) {
							params.put("version", "");
						} else {
							params.put("version", packageInfo.versionName);
						}
						Object obj = PackageUtils.getMetaData(context,
								packageInfo.packageName, "gh_version");
						String gh_version = null;
						if (obj != null && obj.getClass().equals(String.class)) {
							gh_version = (String) obj;
						}
						if (gh_version != null && gh_version.startsWith("gh")) {
							params.put("gh_version", gh_version.substring(2));
						} else {
							params.put("gh_version", "");
						}
						JSONObject jsonObject = new JSONObject(params);
						jsonArray.put(jsonObject);
					}
				}
			}
		}
		return jsonArray;
	}

	public static int getUpdateListSize() {
		return getUpdateList().size();
	}
}
