package com.example.wallisyn.tool;

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

import androidx.annotation.NonNull;

import com.example.wallisyn.MyApp;
import com.example.wallisyn.mybean.MyData;
import com.example.wallisyn.mybean.MyData_;
import com.example.wallisyn.mybean.MyObjectBox;
import com.example.wallisyn.connector.OnUpdateLike;
import com.example.wallisyn.connector.OnLikeList;

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

    private static BoxStore boxStore;

    private static Box<MyData> LikeBox;


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


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

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

        return LikeBox;
    }

    public static void addData(MyData data) {
        Box<MyData> objectBoxLike = getObjectBoxLike();
        String imId = data.getImId();
        MyData first = objectBoxLike.query()
                .equal(MyData_.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(OnLikeList listener) {
        Query<MyData> build = getObjectBoxLike().query()
                .equal(MyData_.isLike, true)
                .build();
        return build.subscribe(new DataSubscriptionList())
                .on(AndroidScheduler.mainThread())
                .observer(new DataObserver<List<MyData>>() {
                    @Override
                    public void onData(@NonNull List<MyData> data) {
                        Log.d(MyApp.TAG, "------data=" + data.size());
                        listener.onLikeList(data);
                    }
                });

    }


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

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


    }


    public static void insertOrUpdateDownload(MyData boxLike) {
        Box<MyData> objectBoxLike = getObjectBoxLike();
        String imId = boxLike.getImId();
        MyData first = objectBoxLike.query()
                .equal(MyData_.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<MyData> search(String name) {
        Box<MyData> objectBoxLike = getObjectBoxLike();
        List<MyData> data = objectBoxLike.query()
                .contains(MyData_.description, name, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .find();
        return data;
    }
    public static List<MyData> queryHome(String name) {
        Box<MyData> objectBoxLike = getObjectBoxLike();
        List<MyData> data = objectBoxLike.query()
                .equal(MyData_.categoryName, name, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .find(10, 1);
        return data;

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

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


}
