package com.paint.trace.drawing.db;

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

import com.paint.trace.drawing.alllistener.OnLikeListener;
import com.paint.trace.drawing.other.AssetData;

import com.paint.trace.drawing.other.AssetData_;
import com.paint.trace.drawing.other.ImportData;

import com.paint.trace.drawing.other.ImportData_;
import com.paint.trace.drawing.other.MyObjectBox;
import com.paint.trace.drawing.view.MyApplication;

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

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

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

        return ImportBox;
    }

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

            AssetData assetData = new AssetData();
            assetData.setPath(mPath);
            objectBoxLike.put(assetData);
            for(OnLikeListener listener:list){
                listener.onAddLike();
            }


        } else {

        }

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

            ImportData importData = new ImportData();
            importData.setUri(muri);
            objectBoxImport.put(importData);
        } else {

        }

    }



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


    }

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

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

    }


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

    }




}
