package com.wall.turing.wallpapers.uutils;

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

import androidx.annotation.NonNull;

import com.wall.turing.wallpapers.WallpaperApplication;
import com.wall.turing.wallpapers.myselfbean.Wallpaper;
import com.wall.turing.wallpapers.myselfbean.Wallpaper_;
import com.wall.turing.wallpapers.myselfbean.MyObjectBox;
import com.wall.turing.wallpapers.connect.TuringUpdateLike;
import com.wall.turing.wallpapers.connect.TuringLikeList;

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

    private static BoxStore boxStore;

    private static Box<Wallpaper> LikeBox;


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


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

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

        return LikeBox;
    }

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

    }

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

    }


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

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


    }


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

    }





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

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

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


}
