package com.phone.wall.paper.tools;

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

import androidx.annotation.NonNull;

import com.phone.wall.paper.WallpaperApp;
import com.phone.wall.paper.data.DataBean;
import com.phone.wall.paper.data.DataBean_;
import com.phone.wall.paper.data.MyObjectBox;
import com.phone.wall.paper.listener.OnLikeList;
import com.phone.wall.paper.listener.OnUpdateFavorite;

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

    private static BoxStore boxStore;

    private static Box<DataBean> LikeBox;
    private static List<OnUpdateFavorite> list = new ArrayList<>();
    public static void init(Context context) {
        boxStore = MyObjectBox.builder().androidContext(context).build();
    }

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

        return LikeBox;
    }

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

    }

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

    }


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

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


    }


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

    }





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

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

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


}
