package com.photo.wallpaper.category.bean;

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

import androidx.annotation.NonNull;

import com.photo.wallpaper.category.MyApp;
import com.photo.wallpaper.category.api.ApiOnUpdateLike;
import com.photo.wallpaper.category.api.ApiOnLike;

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

import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.android.AndroidScheduler;
import io.objectbox.query.Query;
import io.objectbox.query.QueryBuilder;
import io.objectbox.reactive.DataObserver;
import io.objectbox.reactive.DataSubscription;
import io.objectbox.reactive.DataSubscriptionList;

public class DataBaseHelper {

    private static BoxStore boxStore;

    private static Box<Data> LikeBox;


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


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

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

        return LikeBox;
    }

    public static void addData(Data data) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        String imId = data.getImId();
        Data first = objectBoxLike.query()
                .equal(Data_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            Log.d(MyApp.TAG, "-----------addData " + imId);
            objectBoxLike.put(data);
        }else {
            Log.d(MyApp.TAG, "-----------忽略添加 ");
        }

    }

    public static DataSubscription setLikeDataListener(ApiOnLike listener) {
        Query<Data> build = getObjectBoxLike().query()
                .equal(Data_.isLike, true)
                .build();
        return build.subscribe(new DataSubscriptionList())
                .on(AndroidScheduler.mainThread())
                .observer(new DataObserver<List<Data>>() {
                    @Override
                    public void onData(@NonNull List<Data> data) {
                        Log.d(MyApp.TAG, "------data=" + data.size());
                        listener.onLikeList(data);
                    }
                });

    }


    public static void updateLike(Data boxLike) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        String imId = boxLike.getImId();

        objectBoxLike.put(boxLike);
        for (ApiOnUpdateLike listener : list) {
            listener.onAdd(boxLike);
        }


    }


    public static void insertOrUpdateDownload(Data boxLike) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        String imId = boxLike.getImId();
        Data first = objectBoxLike.query()
                .equal(Data_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            Log.d(MyApp.TAG, "-----------添加11 Download=" + imId);
            boxLike.setDownload(true);
            objectBoxLike.put(boxLike);
        } else {
            first.setDownload(true);
            objectBoxLike.put(first);
            Log.d(MyApp.TAG, "-----------已经存在添加22 Download=" + imId);
        }

    }





    public static List<Data> search(String name) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        List<Data> data = objectBoxLike.query()
                .contains(Data_.description, name, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .find();
        return data;
    }
    public static List<Data> queryHome(String name) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        List<Data> data = objectBoxLike.query()
                .equal(Data_.categoryName, name, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .find(0, 5);
        return data;

    }
    public static List<Data> queryCategoryAll(String name) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        List<Data> data = objectBoxLike.query()
                .equal(Data_.categoryName, name, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .find(0, 200);
        return data;

    }
    public static boolean queryIsLike(String imId) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        Data first = objectBoxLike.query()
                .equal(Data_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first != null && first.isLike()) {
            return true;
        } else {
            return false;
        }
    }


}
