package com.zkjc.common.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;

import androidx.annotation.NonNull;

import com.jess.arms.base.App;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.base.delegate.AppDelegate;
import com.jess.arms.base.delegate.AppLifecycles;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.Preconditions;

import java.util.List;

public class ZkjcBaseApp extends Application implements App {
    public SharedPreferences sharedPreferences;
    private AppLifecycles mAppDelegate;
    public static ZkjcBaseApp instance;
    public float fontScale;

    public static ZkjcBaseApp getInstance() {
        return instance;
    }
    /**
     * 这里会在 {@link BaseApplication#onCreate} 之前被调用,可以做一些较早的初始化
     * 常用于 MultiDex 以及插件化框架的初始化
     *
     * @param base
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        if (mAppDelegate == null)
            this.mAppDelegate = new AppDelegate(base);
        this.mAppDelegate.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
        if (mAppDelegate != null)
            this.mAppDelegate.onCreate(this);
        sharedPreferences = getSharedPreferences("sys", MODE_PRIVATE);
        fontScale = getFontScale();
    }

    /**
     * 在模拟环境中程序终止时会被调用
     */
    @Override
    public void onTerminate() {
        super.onTerminate();
        if (mAppDelegate != null)
            this.mAppDelegate.onTerminate(this);
    }

    /**
     * 将 {@link AppComponent} 返回出去, 供其它地方使用, {@link AppComponent} 接口中声明的方法所返回的实例, 在 {@link #getAppComponent()} 拿到对象后都可以直接使用
     *
     * @return AppComponent
     * @see ArmsUtils#obtainAppComponentFromContext(Context) 可直接获取 {@link AppComponent}
     */
    @NonNull
    @Override
    public AppComponent getAppComponent() {
        Preconditions.checkNotNull(mAppDelegate, "%s cannot be null", AppDelegate.class.getName());
        Preconditions.checkState(mAppDelegate instanceof App, "%s must be implements %s", mAppDelegate.getClass().getName(), App.class.getName());
        return ((App) mAppDelegate).getAppComponent();
    }
    public static float getFontScale() {
        float fontScale = 1.0f;
        if (instance != null) {
            fontScale = instance.sharedPreferences.getFloat("font_size", 1.0f);
        }
        return fontScale;
    }
  /*  public static void setAppFontSize(float fontScale) {
        if (instance != null) {
            List<Activity> activityList = AppManager.getAppManager().getActivityList();
            if (activityList != null) {
                for (Activity activity : activityList) {
                *//* if (activity instanceof ZkjcBaseActivity) {
                        continue;
                    }*//*
                    Resources resources = activity.getResources();
                    if (resources != null) {
                        android.content.res.Configuration configuration = resources.getConfiguration();
                        configuration.fontScale = fontScale;
                        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
                        activity.recreate();
                        if (fontScale != instance.fontScale) {
                            instance.fontScale = fontScale;
                            instance.sharedPreferences.edit().putFloat("font_size", fontScale).apply();
                        }
                    }
                }
            }
        }
    }*/
}
