package com.trace.ar.drawing.helper;

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

import com.trace.ar.drawing.ARApp;
import com.trace.ar.drawing.listener.OnAddOrRemoveListener;

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

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

public class ObjectBox {

    private static BoxStore boxStore;

    private static Box<FilePath> LikeBox;

    private static Box<ImportUri> ImportBox;

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

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

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

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

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

        return ImportBox;
    }

    public static void insertOrUpdateLike(String mPath) {
        Box<FilePath> objectBoxLike = getObjectBoxLike();
        FilePath first = objectBoxLike.query()
                .equal(FilePath_.path, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {

            FilePath filePath = new FilePath();
            filePath.setPath(mPath);
            objectBoxLike.put(filePath);
            for(OnAddOrRemoveListener listener:list){
                listener.onAddLike();
            }


        } else {

        }

    }


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

            ImportUri importUri = new ImportUri();
            importUri.setUri(muri);
            objectBoxImport.put(importUri);
        } else {

        }

    }



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


    }

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

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

    }


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

    }




}
