package com.ar.drawing.art.dataman;

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

import com.ar.drawing.art.listener.OnLikeListener;
import com.ar.drawing.art.utils.ArtApp;

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<MyBean> LikeBox;

    private static Box<ImportBean> 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<MyBean> getObjectBoxLike() {
        if (LikeBox == null) {
            LikeBox =  boxStore.boxFor(MyBean.class);
        }

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

        return ImportBox;
    }

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

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

            ImportBean importBean = new ImportBean();
            importBean.setUri(muri);
            objectBoxImport.put(importBean);
        } else {

        }

    }

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


    }

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


    }

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

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

    }


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

    }
}
