package com.wall.dream.wallpapers.GJ;

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

import androidx.annotation.NonNull;

import com.wall.dream.wallpapers.DreamWallpaper;
import com.wall.dream.wallpapers.data.DataBase;
import com.wall.dream.wallpapers.data.DataBase_;
import com.wall.dream.wallpapers.data.MyObjectBox;
import com.wall.dream.wallpapers.LianXi.OnUpdateDeLike;
import com.wall.dream.wallpapers.LianXi.OnLikeDeList;

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 ObectBoxManagerGJ {

    private static BoxStore boxStore;

    private static Box<DataBase> LikeBox;


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


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

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

        return LikeBox;
    }

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

    }

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

    }


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

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


    }


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

    }





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

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

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

    /**
     * 获取所有壁纸数据
     */
    public static List<DataBase> getAllWallpapers() {
        Box<DataBase> objectBoxLike = getObjectBoxLike();
        List<DataBase> data = objectBoxLike.query().build().find();
        return data;
    }

}
