package com.picture.ar.drawing.data;

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

import com.picture.ar.drawing.listener.LikeListener;
import com.picture.ar.drawing.utils.App;


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

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

public class MyDataBase {

    private static BoxStore boxStore;

    private static Box<BeanLocal> LikeBox;

    private static Box<BeanUpload> ImportBox;

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

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

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

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

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

        return ImportBox;
    }

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

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

            BeanUpload beanUpload = new BeanUpload();
            beanUpload.setUri(muri);
            objectBoxImport.put(beanUpload);
        } else {

        }

    }

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


    }

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


    }

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

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

    }


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

    }
}
