package com.snap.wall.mytool;

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

import androidx.annotation.NonNull;

import com.snap.wall.App;
import com.snap.wall.adapter.OnLikelist;
import com.snap.wall.adapter.OnUpdatelike;
import com.snap.wall.bean.Information;
import com.snap.wall.bean.Information_;
import com.snap.wall.bean.MyObjectBox;

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

    private static BoxStore boxStore;

    private static Box<Information> LikeBox;


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


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

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

        return LikeBox;
    }

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

    }

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

    }


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

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


    }


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

    }





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

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

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


}
