package com.light.prank.sound.app.dataListener;

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 objectBoxMan {

    private static BoxStore store;

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

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

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

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


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

    }

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

    }

    public static void addLike(PrankBean PrankBean) {
        PrankBean.setLike(true);
        objectBoxMan.getBoxBean().put(PrankBean);
    }

    public static void removeLike(PrankBean PrankBean) {
        PrankBean.setLike(false);
        objectBoxMan.getBoxBean().put(PrankBean);

    }

}
