package com.gh.gamecenter.feature.utils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gh.gamecenter.common.constant.RouteConsts;
import com.gh.gamecenter.core.provider.IPackagesManagerProvider;
import com.gh.gamecenter.feature.BuildConfig;
import com.gh.gamecenter.feature.entity.ApkEntity;
import com.gh.gamecenter.feature.entity.GameEntity;
import com.lightgame.utils.Utils;

import java.util.List;

import io.reactivex.functions.Function;

/**
 * Created by khy on 10/05/17.
 */

public class ApkActiveUtils {

    public static void filterHideApk(GameEntity gameEntity) {
        try {
            if (gameEntity == null || gameEntity.getOriginalApk().size() == 0) return;
            List<ApkEntity> apkList = gameEntity.getOriginalApk();
            for (int i = 0; i < apkList.size(); i++) {
                ApkEntity apkEntity = apkList.get(i);
                String packageName = apkEntity.getPackageName();
                String id = gameEntity.getId();
                IPackagesManagerProvider provider = (IPackagesManagerProvider)ARouter.getInstance().build(RouteConsts.provider.packagesManager).navigation();
                if (!apkEntity.isActive() && !provider.isCanPluggable(id, packageName)) {
                    apkList.remove(i);
                    i--;
                }
            }
        } catch (Exception e) {
            if (BuildConfig.DEBUG) {
                Utils.log("filter hide game apk throws exception:" + e.toString());
            }
        }
    }

    // 过滤隐藏apk包
    public static Function<List<GameEntity>, List<GameEntity>> filterMapperList = list -> {
        for (GameEntity gameEntity : list) {
            ApkActiveUtils.filterHideApk(gameEntity);
        }
        return list;
    };

    // 过滤隐藏apk包
    public static Function<GameEntity, GameEntity> filterMapper = list -> {
        ApkActiveUtils.filterHideApk(list);
        return list;
    };
}
