package com.prankster.fx.db;

import android.content.Context;

import java.util.List;

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

public class ObManager {

    private static BoxStore store;

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

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


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

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

    public static boolean checkLike(Bean bean) {
        Query<Bean> query = ObManager.getBoxBean().query(Bean_.name.equal(bean.getName())).build();
        Bean data = query.findFirst();
        query.close();
        if (data != null) {
            return data.isLike();
        } else {
            return false;
        }

    }

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

    }

    public static void addLike(Bean bean) {
        bean.setLike(true);
        ObManager.getBoxBean().put(bean);
    }

    public static void removeLike(Bean bean) {
        bean.setLike(false);
        ObManager.getBoxBean().put(bean);

    }

}
