package com.prank.funny.pranks.tools;

import android.content.Context;

import com.prank.funny.pranks.bean.Info;
import com.prank.funny.pranks.bean.Info_;
import com.prank.funny.pranks.bean.MyObjectBox;

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

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

public class DataBase {

    private static BoxStore store;

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

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

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

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


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

    }

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

    }

    public static void addLike(Info info) {
        info.setLike(true);
        DataBase.getBoxBean().put(info);
    }

    public static void removeLike(Info info) {
        info.setLike(false);
        DataBase.getBoxBean().put(info);

    }

}
