package com.newapp.paperwall.tools;

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

import androidx.annotation.NonNull;


import com.newapp.paperwall.BaseApp;
import com.newapp.paperwall.javabean.Data;
import com.newapp.paperwall.javabean.Data_;
import com.newapp.paperwall.javabean.MyObjectBox;
import com.newapp.paperwall.listener.LikeListListener;
import com.newapp.paperwall.listener.ChangeCollectListener;

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

    private static BoxStore boxStore;

    private static Box<Data> LikeBox;


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


    public static void init(Context context) {
        boxStore = MyObjectBox.builder().androidContext(context).build();
        Log.d("YYYYYYY","init success");
    }

    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(BaseApp.TAG, "-----------addData " + imId);
            objectBoxLike.put(data);
        }else {
            Log.d(BaseApp.TAG, "-----------忽略添加 ");
        }

    }

    public static DataSubscription setLikeDataListener(LikeListListener 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(BaseApp.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 (ChangeCollectListener listener : list) {
            listener.onAdd(boxLike);
        }


    }


    public static void insertOrUpdateDownload(Data boxLike) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        String imId = boxLike.getImId();
        Log.d("YYYYYYY","imId is "+imId);
        Data first = objectBoxLike.query()
                .equal(Data_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            Log.d(BaseApp.TAG, "-----------添加11 Download=" + imId);
            boxLike.setDownload(true);
            objectBoxLike.put(boxLike);
        } else {
            first.setDownload(true);
            objectBoxLike.put(first);
            Log.d(BaseApp.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(10, 1);
        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;
        }
    }


}
