package com.prank.simulator.sounds.helper;

import android.content.Context;

import com.prank.simulator.sounds.data.ChildData;
import com.prank.simulator.sounds.data.ChildData_;
import com.prank.simulator.sounds.data.MyObjectBox;

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<ChildData> getBoxBean() {
        return store.boxFor(ChildData.class);
    }


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

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

    public static boolean checkLike(ChildData childData) {
        Query<ChildData> query = Db.getBoxBean().query(ChildData_.name.equal(childData.getName())).build();
        ChildData data = query.findFirst();
        query.close();
        if (data != null) {
            return data.isLike();
        } else {
            return false;
        }

    }

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

    }

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

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

    }

}
