package com.prank.app.color.dbdata;

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

    private static BoxStore store;

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

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

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

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


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

    }

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

    }

    public static void addLike(MyInfo info) {
        info.setLike(true);
        DbTool.getBoxBean().put(info);
    }

    public static void removeLike(MyInfo info) {
        info.setLike(false);
        DbTool.getBoxBean().put(info);

    }

}
