package com.prank.tool.sounds.app.data;

import android.content.Context;

import java.util.Collections;
import java.util.List;

import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.query.Query;

public class Db {

    private static BoxStore store;

    public static void init(Context context) {
        store = MyObjectBox.builder()
                .androidContext(context)
                .build();
    }

    public static Box<detail> getBoxBean() {
        return store.boxFor(detail.class);
    }

    public static List<detail> getAllLike() {
        Query<detail> query = Db.getBoxBean().query(detail_.isLike.equal(true)).build();
        List<detail> data = query.find();
        query.close();
        return data;
    }

    public static List<detail> getAllCustomization() {
        Query<detail> query = Db.getBoxBean().query(detail_.isCustomization.equal(true)).build();
        List<detail> data = query.find();
        query.close();
        return data;
    }


    public static List<detail> getMore() {
        Query<detail> query = Db.getBoxBean().query(detail_.isCustomization.equal(false)).build();
        List<detail> data = query.find();
        query.close();
        Collections.shuffle(data);
        return data.subList(0, 11);
    }
    public static boolean checkLike(detail detail) {
        Query<detail> query = Db.getBoxBean().query(detail_.name.equal(detail.getName())).build();
        detail data = query.findFirst();
        query.close();
        if (data != null) {
            return data.isLike();
        } else {
            return false;
        }

    }

    public static boolean checkName(String name) {
        Query<detail> query = Db.getBoxBean().query(detail_.name.equal(name)).build();
        detail data = query.findFirst();
        query.close();
        if (data != null) {
            return false;
        } else {
            return true;
        }

    }

    public static void addLike(detail detail) {
        detail.setLike(true);
        Db.getBoxBean().put(detail);
    }

    public static void removeLike(detail detail) {
        detail.setLike(false);
        Db.getBoxBean().put(detail);

    }

}
