package com.wall.proshot.wallpapers.mytool;

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

import androidx.annotation.NonNull;

import com.wall.proshot.wallpapers.FreeWallPaper;
import com.wall.proshot.wallpapers.bean.Data;
import com.wall.proshot.wallpapers.myconnector.OnLikeList;
import com.wall.proshot.wallpapers.myconnector.OnUpdateLike;
import com.wall.proshot.wallpapers.bean.Data_;
import com.wall.proshot.wallpapers.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 MyObectBoxManager {

    private static BoxStore boxStore;

    private static Box<Data> LikeBox;


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


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

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

    }

    public static DataSubscription setLikeDataListener(OnLikeList 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(FreeWallPaper.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 (OnUpdateLike listener : list) {
            listener.onAdd(boxLike);
        }


    }


    public static void insertOrUpdateDownload(Data boxLike) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        String imId = boxLike.getImId();
        Data first = objectBoxLike.query()
                .equal(Data_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            Log.d(FreeWallPaper.TAG, "-----------添加11 Download=" + imId);
            boxLike.setDownload(true);
            objectBoxLike.put(boxLike);
        } else {
            first.setDownload(true);
            objectBoxLike.put(first);
            Log.d(FreeWallPaper.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); // 修复：offset=0（从第一条数据开始）
        Log.d(FreeWallPaper.TAG, "查询分类[" + name + "]结果：" + data.size() + "条");
        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;
        }
    }


}
