package com.ar.sketch.paint.drawing.dbmanager;

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

import com.ar.sketch.paint.drawing.api.OnLike;
import com.ar.sketch.paint.drawing.tool.MyArApp;

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

    private static Box<ImportData> ImportBox;

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

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

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

    private static Box<LocalData> getObjectBoxLike() {
        if (LikeBox == null) {
            LikeBox =  boxStore.boxFor(LocalData.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<LocalData> objectBoxLike = getObjectBoxLike();
        LocalData first = objectBoxLike.query()
                .equal(LocalData_.path, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            LocalData localData = new LocalData();
            localData.setPath(mPath);
            objectBoxLike.put(localData);
            for(OnLike 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 deleteImport(String mPath) {
        Box<ImportData> objectBoxImport = getObjectBoxImport();
        ImportData first = objectBoxImport.query()
                .equal(ImportData_.uri, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first != null) {
            objectBoxImport.remove(first);
            for(OnLike listener:list){
//                listener.onDeleteLike();
            }
            Log.d(MyArApp.TAG, "--------deleteImport imId=" + mPath);
        }


    }

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


    }

    public static List<LocalData> 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<LocalData> objectBoxLike = getObjectBoxLike();
        LocalData first = objectBoxLike.query()
                .equal(LocalData_.path, mPath, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        return first != null;

    }
}
