package com.haircut.pranks.app.manager;

import android.content.Context;

import com.haircut.pranks.app.data.ChildData;
import com.haircut.pranks.app.data.ChildData_;
import com.haircut.pranks.app.data.MyObjectBox;

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

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

public class DataManager {

    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 = DataManager.getBoxBean().query(ChildData_.isLike.equal(true)).build();
        List<ChildData> data = query.find();
        query.close();
        return data;
    }

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


    public static List<ChildData> getMore() {
        Query<ChildData> query = DataManager.getBoxBean().query(ChildData_.isCustomization.equal(false)).build();
        List<ChildData> data = query.find();
        query.close();
        Collections.shuffle(data);
        return data.subList(0, 11);
    }
    public static boolean checkLike(ChildData childData) {
        Query<ChildData> query = DataManager.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 = DataManager.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);
        DataManager.getBoxBean().put(childData);
    }

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

    }

}
