package com.key.vibekeyboard;

import android.app.Application;
import android.content.Context;

import com.key.vibekeyboard.Room.Category;
import com.key.vibekeyboard.Room.MyDatabase;
import com.key.vibekeyboard.Room.WallpaperInfo;
import com.key.vibekeyboard.Utils.Mytool;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class AppApplication extends Application {

    public static AppApplication instance;
    public static List<Category> categories;

    public static final int DB_Version = 1;
    public static final String DB_Name = "image_database";
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        categories = Mytool.parseJsonToList("keyboard.json");

        Mytool.runIO(new Runnable() {
            @Override
            public void run() {

                MyDatabase.getInstance().wallpaperInfoDao().insertAll(getAlldataList());

            }
        });

    }

    public static AppApplication getInstance() {
        return instance;
    }

    public static Context getContext() {
        return instance.getApplicationContext();
    }

    public static List<Category> getCategories() {
        return categories;
    }

    public static List<WallpaperInfo> getAlldataList() {
        List<WallpaperInfo> otherdata = new ArrayList<>();
        List<Category> mydataList = getCategories();
        Set<String> set = new HashSet<>();
        for (Category mydata : mydataList) {
            List<WallpaperInfo> otherdataList = mydata.getList();
            for (WallpaperInfo otherdata1 : otherdataList) {
                String title = otherdata1.getTitle();
                if (!set.contains(title)) {
                    otherdata.add(otherdata1);
                    set.add(title);
                }
            }
        }

        return otherdata;
    }
}

