package com.paint.ar.drawing.ob;

import android.content.Context;
import android.util.Log;

import com.paint.ar.drawing.MyApp;
import com.paint.ar.drawing.api.OnLikeListener;

import java.util.ArrayList;
import java.util.List;

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

public class ObManager {

    private static BoxStore boxStore;

    private static Box<DataLocal> LikeBox;

    private static Box<DataImport> ImportBox;

    private static List<OnLikeListener> list = new ArrayList<>();

    public static void addListener(OnLikeListener listener) {
        list.add(listener);
    }

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

    private static Box<DataLocal> getObjectBoxLike() {
        if (LikeBox == null) {
            LikeBox =  boxStore.boxFor(DataLocal.class);
        }

        return LikeBox;
    }
    private static Box<DataImport> getObjectBoxImport() {
        if (ImportBox == null) {
            ImportBox =  boxStore.boxFor(DataImport.class);
        }

        return ImportBox;
    }

    public static void insertOrUpdateLike(String mPath) {
        Box<DataLocal> objectBoxLike = getObjectBoxLike();
        DataLocal first = objectBoxLike.query()
                .equal(DataLocal_.path, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            DataLocal dataLocal = new DataLocal();
            dataLocal.setPath(mPath);
            objectBoxLike.put(dataLocal);
            for(OnLikeListener listener:list){
                listener.onAddLike();
            }
        } else {

        }
    }
    public static void insertImport(String muri) {
        Box<DataImport> objectBoxImport = getObjectBoxImport();
        DataImport first = objectBoxImport.query()
                .equal(DataImport_.uri, muri, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {

            DataImport dataImport = new DataImport();
            dataImport.setUri(muri);
            objectBoxImport.put(dataImport);
        } else {

        }

    }

    public static void deleteImport(String mPath) {
        Box<DataImport> objectBoxImport = getObjectBoxImport();
        DataImport first = objectBoxImport.query()
                .equal(DataImport_.uri, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first != null) {
            objectBoxImport.remove(first);
            for(OnLikeListener listener:list){
//                listener.onDeleteLike();
            }
            Log.d(MyApp.TAG, "--------deleteImport imId=" + mPath);
        }


    }

    public static void deleteLike(String mPath) {
        Box<DataLocal> objectBoxLike = getObjectBoxLike();
        DataLocal boxLike = objectBoxLike.query()
                .equal(DataLocal_.path, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (boxLike != null) {
            objectBoxLike.remove(boxLike);
            for(OnLikeListener listener:list){
                listener.onDeleteLike();
            }
            Log.d(MyApp.TAG, "--------deleteLike imId=" + mPath);
        }


    }

    public static List<DataLocal> queryAllLike() {
        return getObjectBoxLike().query()
                .build()
                .find();
    }

    public static List<DataImport> queryAllImport() {
        return getObjectBoxImport().query()
                .build()
                .find();
    }
    public static boolean queryIsImport(String mPath) {
        Box<DataImport> objectBoxImport = getObjectBoxImport();
        DataImport first = objectBoxImport.query()
                .equal(DataImport_.uri, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        return first != null;

    }


    public static boolean queryIsLike(String mPath) {
        Box<DataLocal> objectBoxLike = getObjectBoxLike();
        DataLocal first = objectBoxLike.query()
                .equal(DataLocal_.path, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        return first != null;

    }
}
