package com.photo.keyboard.themes.manager;

import android.content.Context;


import com.photo.keyboard.themes.data.MyObjectBox;
import com.photo.keyboard.themes.data.ThemeData;
import com.photo.keyboard.themes.data.ThemeData_;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.query.QueryBuilder;
import io.objectbox.reactive.DataSubscription;

public class DbManager {

    private static BoxStore boxStore;

    private static Box<ThemeData> dataBox;


    private static DataSubscription observer;

//    private static List<OnUpdateListener> list = new ArrayList<>();
//
//    public static void addOnAddOrRemoveListener(OnUpdateListener listener) {
//        list.add(listener);
//    }

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

    public static Box<ThemeData> getObjectBox() {
        if (dataBox == null) {
            dataBox = boxStore.boxFor(ThemeData.class);
        }

        return dataBox;
    }


//    public static DataSubscription setLikeDataListener(OnLikeListener listener) {
//        Query<ThemeData> build = getObjectBox().query()
//                .build();
//        return build.subscribe(new DataSubscriptionList())
//                .on(AndroidScheduler.mainThread())
//                .observer(new DataObserver<List<ThemeData>>() {
//                    @Override
//                    public void onData(@NonNull List<ThemeData> data) {
//                        listener.onLikeList(data);
//                    }
//                });
//
//    }

    public static void addLike(ThemeData boxLike) {
        Box<ThemeData> objectBoxLike = getObjectBox();
        String imId = boxLike.getResId();
        ThemeData first = objectBoxLike.query()
                .equal(ThemeData_.resId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            objectBoxLike.put(boxLike);
//            for (OnUpdateListener listener : list) {
//                listener.onAdd(boxLike);
//            }

        }

    }


    public static void deleteLike(ThemeData likeData) {
        Box<ThemeData> objectBoxLike = getObjectBox();
        String imId = likeData.getResId();
        ThemeData boxLike = objectBoxLike.query()
                .equal(ThemeData_.resId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (boxLike != null) {
            objectBoxLike.remove(boxLike);
//            for (OnUpdateListener listener : list) {
//                listener.onRemove(boxLike);
//            }
        }


    }


    public static boolean queryIsLike(String resId) {
        Box<ThemeData> objectBoxLike = getObjectBox();
        ThemeData first = objectBoxLike.query()
                .equal(ThemeData_.resId, resId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first != null) {
            return true;

        } else return false;


    }

    public static List<ThemeData> searchAllLike() {
        Box<ThemeData> objectBoxLike = getObjectBox();
        List<ThemeData> data = objectBoxLike.query()
                .build()
                .find();
        return data;
    }

}
