package com.bytedance.android.live_settings;

import com.bytedance.android.live.annotation.Group;
import com.bytedance.android.live.annotation.SettingsKey;
import com.bytedance.android.live_settings.repo.MockSettingsRepo;
import com.bytedance.android.live_settings.repo.SettingsRepo;
import com.bytedance.covode.number.Covode;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DataCenter {
    public static boolean mIsDebug;

    static {
        Covode.recordClassIndex(11177);
    }

    public static void clear() {
        SettingsValueProvider.INSTANCE.clear();
    }

    public static Map<String, Object> getAllValues() {
        return new HashMap(SettingsRepo.INSTANCE.getAll());
    }

    public static Class getConfigType(Class cls) {
        return getTargetField(cls).getType();
    }

    public static String getKey(Class cls) {
        try {
            SettingsKey annotation = cls.getAnnotation(SettingsKey.class);
            if (annotation != null) {
                return annotation.value();
            }
        } catch (Exception unused) {
        }
        return "";
    }

    public static void init(boolean z) {
        mIsDebug = z;
        SettingsRepo.INSTANCE.load();
        if (mIsDebug) {
            MockSettingsRepo.INSTANCE.load();
        }
    }

    public static Field getTargetField(Class cls) {
        Field[] declaredFields = cls.getDeclaredFields();
        int length = declaredFields.length;
        Field field = null;
        int i = 0;
        while (true) {
            if (i < length) {
                Field field2 = declaredFields[i];
                Group annotation = field2.getAnnotation(Group.class);
                if (annotation != null) {
                    if (annotation.isDefault()) {
                        field = field2;
                        break;
                    }
                    field = field2;
                }
                i++;
            } else if (field == null) {
                throw new RuntimeException("Can not find @Group in " + cls.getName());
            }
        }
        field.setAccessible(true);
        return field;
    }

    public static <T> T getDefaultValue(Class cls) {
        try {
            Field[] declaredFields = cls.getDeclaredFields();
            if (declaredFields != null) {
                ArrayList arrayList = new ArrayList();
                boolean z = false;
                Field field = null;
                for (Field field2 : declaredFields) {
                    Group annotation = field2.getAnnotation(Group.class);
                    if (annotation != null) {
                        arrayList.add(field2);
                        if (annotation.isDefault()) {
                            field = field2;
                        }
                    }
                }
                if (field == null && arrayList.size() == 1) {
                    field = (Field) arrayList.get(0);
                }
                if (field != null) {
                    field.setAccessible(true);
                    if ((field.getModifiers() & 8) == 8) {
                        z = true;
                    }
                    if (z) {
                        return field.get((Object) null);
                    }
                }
            }
            return null;
        } catch (Throwable th) {
            SettingsManager.INSTANCE.getMonitor().reportException("getDefaultValue", "exception: " + th.getMessage() + " ,class name: " + cls.getSimpleName());
            return null;
        }
    }

    public static boolean getBooleanValue(String str, boolean z) {
        if (str == null || str.length() == 0) {
            return z;
        }
        if (!mIsDebug || !MockSettingsRepo.INSTANCE.contains(str)) {
            return SettingsValueProvider.INSTANCE.getBooleanValue(str, z);
        }
        return MockSettingsRepo.INSTANCE.getBooleanValue(str, z);
    }

    public static double getDoubleValue(String str, double d) {
        if (str == null || str.length() == 0) {
            return d;
        }
        if (!mIsDebug || !MockSettingsRepo.INSTANCE.contains(str)) {
            return SettingsValueProvider.INSTANCE.getDoubleValue(str, d);
        }
        return MockSettingsRepo.INSTANCE.getDoubleValue(str, d);
    }

    public static float getFloatValue(String str, float f) {
        if (str == null || str.length() == 0) {
            return f;
        }
        if (!mIsDebug || !MockSettingsRepo.INSTANCE.contains(str)) {
            return SettingsValueProvider.INSTANCE.getFloatValue(str, f);
        }
        return MockSettingsRepo.INSTANCE.getFloatValue(str, f);
    }

    public static int getIntValue(String str, int i) {
        if (str == null || str.length() == 0) {
            return i;
        }
        if (!mIsDebug || !MockSettingsRepo.INSTANCE.contains(str)) {
            return SettingsValueProvider.INSTANCE.getIntValue(str, i);
        }
        return MockSettingsRepo.INSTANCE.getIntValue(str, i);
    }

    public static long getLongValue(String str, long j) {
        if (str == null || str.length() == 0) {
            return j;
        }
        if (!mIsDebug || !MockSettingsRepo.INSTANCE.contains(str)) {
            return SettingsValueProvider.INSTANCE.getLongValue(str, j);
        }
        return MockSettingsRepo.INSTANCE.getLongValue(str, j);
    }

    public static String[] getStringArrayValue(String str, String[] strArr) {
        if (str == null || str.length() == 0) {
            return strArr;
        }
        if (!mIsDebug || !MockSettingsRepo.INSTANCE.contains(str)) {
            return SettingsValueProvider.INSTANCE.getStringArrayValue(str, strArr);
        }
        return MockSettingsRepo.INSTANCE.getStringArrayValue(str, strArr);
    }

    public static String getStringValue(String str, String str2) {
        if (str == null || str.length() == 0) {
            return str2;
        }
        if (!mIsDebug || !MockSettingsRepo.INSTANCE.contains(str)) {
            return SettingsValueProvider.INSTANCE.getStringValue(str, str2);
        }
        return MockSettingsRepo.INSTANCE.getStringValue(str, str2);
    }

    public static <T> T getValueSafely(String str, Class cls, Object obj) {
        if (str == null || str.length() == 0) {
            return obj;
        }
        if (mIsDebug && MockSettingsRepo.INSTANCE.contains(str)) {
            return MockSettingsRepo.INSTANCE.getValue(str, cls);
        }
        T value = SettingsValueProvider.INSTANCE.getValue(str, cls);
        if (value == null) {
            return obj;
        }
        return value;
    }
}
