package com.wall.exquisite.wallpaper;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.up.uploadlibrary.UpLoadManager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import kotlin.Unit;
import kotlin.jvm.functions.Function2;

// 导入你的收藏工具类


/**
 * 壁纸应用的全局Application类，负责初始化全局工具、预加载数据
 */
public class WallpaperApplication extends Application {
    private static WallpaperApplication instance;
    private ExecutorService mExecutorService;
    private Handler mMainHandler;
    // 全局单例的收藏工具
    private static CollectionManager favoriteManager;
    public static final String TAG = "WallpaperApplication";

    @Override
    public void onCreate() {
        super.onCreate();
        UpLoadManager.INSTANCE.init(this, TAG, new Function2<String, String, Unit>() {
            @Override
            public Unit invoke(String s, String s2) {
                return null;
            }
        });
        instance = this;
        // 初始化基础工具（主线程）
        initBasicTools();
        // 异步执行耗时数据准备
        initDataAsync();
    }

    private void initBasicTools() {
        mMainHandler = new Handler(Looper.getMainLooper());
        mExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        Log.d("WallpaperApplication", "基础工具初始化完成");
    }

    private void initDataAsync() {
        mExecutorService.execute(new Runnable() {
            @Override
            public void run() {
                // 初始化收藏工具
                initFavoriteManager();
                // 预加载收藏数据
                preloadFavoriteData();

                // 回调主线程标记完成
                mMainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        onDataInitComplete();
                    }
                });
            }
        });
    }

    /**
     * 初始化全局收藏工具
     */
    private void initFavoriteManager() {
        Log.d("WallpaperApplication", "开始初始化收藏工具...");
        favoriteManager = new CollectionManager(getApplicationContext());
        Log.d("WallpaperApplication", "收藏工具初始化完成");
    }

    /**
     * 预加载收藏数据
     */
    private void preloadFavoriteData() {
        Log.d("WallpaperApplication", "开始预加载收藏数据...");
        if (favoriteManager != null) {
            favoriteManager.getFavoriteItems();
            Log.d("WallpaperApplication", "收藏数据预加载完成，数量：" + favoriteManager.getFavoriteItems().size());
        }
    }

    private void onDataInitComplete() {
        Log.d("WallpaperApplication", "所有数据准备工作完成！");
    }

    // 获取全局Application实例
    public static WallpaperApplication getInstance() {
        return instance;
    }

    // 获取全局收藏工具实例
    public static CollectionManager getFavoriteManager() {
        return favoriteManager;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        if (mExecutorService != null && !mExecutorService.isShutdown()) {
            mExecutorService.shutdown();
        }
    }
}