package com.study.sketch.learning.database;

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

import com.study.sketch.learning.listener.OnFavoriteListener;
import com.study.sketch.learning.SketchingApp;

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

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

public class MyDb {

    private static BoxStore boxStore;

    private static Box<DataLocal> LikeBox;

    private static Box<BeanImport> ImportBox;

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

    public static void addListener(OnFavoriteListener 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<BeanImport> getObjectBoxImport() {
        if (ImportBox == null) {
            ImportBox =  boxStore.boxFor(BeanImport.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(OnFavoriteListener listener:list){
                listener.onAddLike();
            }
        } else {

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

            BeanImport beanImport = new BeanImport();
            beanImport.setUri(muri);
            objectBoxImport.put(beanImport);
        } else {

        }

    }

    public static void deleteImport(String mPath) {
        Box<BeanImport> objectBoxImport = getObjectBoxImport();
        BeanImport first = objectBoxImport.query()
                .equal(BeanImport_.uri, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first != null) {
            objectBoxImport.remove(first);
            for(OnFavoriteListener listener:list){
//                listener.onDeleteLike();
            }
            Log.d(SketchingApp.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(OnFavoriteListener listener:list){
                listener.onDeleteLike();
            }
            Log.d(SketchingApp.TAG, "--------deleteLike imId=" + mPath);
        }


    }

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

    public static List<BeanImport> queryAllImport() {
        return getObjectBoxImport().query()
                .build()
                .find();
    }
    public static boolean queryIsImport(String mPath) {
        Box<BeanImport> objectBoxImport = getObjectBoxImport();
        BeanImport first = objectBoxImport.query()
                .equal(BeanImport_.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;

    }
}
