package com.wall.snap.wallpaper.toolsnap;

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

import androidx.annotation.NonNull;

import com.wall.snap.wallpaper.MysnapApp;
import com.wall.snap.wallpaper.mysnapbean.MysnapData;
import com.wall.snap.wallpaper.mysnapbean.MysnapData_;
import com.wall.snap.wallpaper.mysnapbean.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 ObectBoxManagersnap {

    private static BoxStore boxStore;

    private static Box<MysnapData> LikeBox;


    private static List<com.wall.snap.wallpaper.connectorsnap.OnUpdatesnapLike> list = new ArrayList<>();


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

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

        return LikeBox;
    }

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

    }

    public static DataSubscription setLikeDataListener(com.wall.snap.wallpaper.connectorsnap.OnLikesnapList listener) {
        Query<MysnapData> build = getObjectBoxLike().query()
                .equal(MysnapData_.isLike, true)
                .build();
        return build.subscribe(new DataSubscriptionList())
                .on(AndroidScheduler.mainThread())
                .observer(new DataObserver<List<MysnapData>>() {
                    @Override
                    public void onData(@NonNull List<MysnapData> data) {
                        Log.d(MysnapApp.TAG, "------data=" + data.size());
                        listener.onLikeList(data);
                    }
                });

    }


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

        objectBoxLike.put(boxLike);
        for (com.wall.snap.wallpaper.connectorsnap.OnUpdatesnapLike listener : list) {
            listener.onAdd(boxLike);
        }


    }


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

    }





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

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

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


}
