package com.free.video.wallpaper.helper;

import android.content.Context;

import androidx.annotation.NonNull;

import com.free.video.wallpaper.listener.OnLikeUpdateListener;
import com.free.video.wallpaper.content.FavoriteData;
import com.free.video.wallpaper.content.FavoriteData_;
import com.free.video.wallpaper.content.MyObjectBox;
import com.free.video.wallpaper.content.ResultData;
import com.free.video.wallpaper.content.ResultData_;
import com.free.video.wallpaper.mypagesource.LikePS;
import com.free.video.wallpaper.mypagesource.MainPS;

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.reactive.DataObserver;
import io.objectbox.reactive.DataSubscription;
import io.objectbox.reactive.DataSubscriptionList;

public class ObjectBoxManager {

    private static BoxStore boxStore;

    private static Box<ResultData> dataBox;
    private static Box<FavoriteData> likeBox;

    private static DataSubscription observer;


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

    public static Box<ResultData> getDataBox() {
        if (dataBox == null) {
            dataBox = boxStore.boxFor(ResultData.class);
        }

        return dataBox;
    }

    public static Box<FavoriteData> getLikeBox() {
        if (likeBox == null) {
            likeBox = boxStore.boxFor(FavoriteData.class);
        }

        return likeBox;
    }

    public static DataSubscription setLikeUpdateListener(OnLikeUpdateListener listener) {
        Box<FavoriteData> likeBox = getLikeBox();
        Query<FavoriteData> build = likeBox.query()
                .build();
        return build.subscribe(new DataSubscriptionList())
                .on(AndroidScheduler.mainThread())
                .observer(new DataObserver<List<FavoriteData>>() {
                    @Override
                    public void onData(@NonNull List<FavoriteData> data) {
                        Commontool.logMsg("---OnLikeUpdateListener-------------" + data.size());
                        listener.onLikeUpdate(data);
                    }
                });

    }

    public static void insertDb(ResultData resultData) {
        Box<ResultData> objectBox = getDataBox();
        ResultData first = objectBox.query()
                .equal(ResultData_.wallpapertype, resultData.getWallpapertype())
                .equal(ResultData_.id, resultData.getId())
                .build()
                .findFirst();
        if (first == null) {
//            Common.logMsg("---insertDb------Wallpapertype------" + resultData.getWallpapertype() + "-------id=" + resultData.getId());
            objectBox.put(resultData);
        } else {
//            Common.logMsg("---insertDb-------------" + resultData.getWallpapertype() + "-------id=" + resultData.getId());
        }
    }


    public static void insertLike(FavoriteData favoriteData) {
        Box<FavoriteData> likeBox = getLikeBox();
        FavoriteData first = likeBox.query()
                .equal(FavoriteData_.id, favoriteData.getId())
                .build()
                .findFirst();
        if (first == null) {
            Commontool.logMsg("---insertLike-----------------id=" + favoriteData.getId());
            likeBox.put(favoriteData);
        } else {
            Commontool.logMsg("---insertLike-----------------id=" + favoriteData.getId());
        }
    }


    public static void deleteLike(int id) {
        Box<FavoriteData> likeBox = getLikeBox();
        FavoriteData first = likeBox.query()
                .equal(FavoriteData_.id, id)
                .build()
                .findFirst();
        if (first != null) {
            likeBox.remove(first);
            Commontool.logMsg("---deleteLike-----------------id=" + id);
        }


    }

    //
//    public static void deleteDownload(Data downloadData) {
//        Box<Data> objectBoxLike = getObjectBox();
//        String imId = downloadData.getImId();
//        Data boxLike = objectBoxLike.query()
//                .equal(Data_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
//                .build()
//                .findFirst();
//        if (boxLike != null) {
//            if (boxLike.isLike()) {
//                boxLike.setDownload(false);
//                objectBoxLike.put(boxLike);
//            } else {
//                objectBoxLike.remove(boxLike);
//            }
//            Log.d(MyWallpaper.TAG, "--------deleteDownload imId=" + imId);
//        }
//
//
//    }
//
//
    public static boolean queryIsLike(int id) {
        Box<FavoriteData> likeBox = getLikeBox();
        FavoriteData first = likeBox.query()
                .equal(FavoriteData_.id, id)
                .build()
                .findFirst();
        if (first != null) {
            return true;
        } else {
            return false;
        }
    }
    public static List<FavoriteData> queryAllLike() {
        Box<FavoriteData> likeBox = getLikeBox();
        return likeBox.query()
                .build()
                .find();
    }

    public static List<ResultData> queryData(int wallpaperType, int currentPage) {
        int offset = (currentPage - 1) * MainPS.pageSize;

        Box<ResultData> objectBoxLike = getDataBox();
        List<ResultData> data = objectBoxLike.query()
                .equal(ResultData_.wallpapertype, wallpaperType)
                .build()
                .find(offset, MainPS.pageSize);

        return data;
    }
    public static List<FavoriteData> queryLike(int currentPage) {
        int offset = (currentPage - 1) * LikePS.pageSize;
        Box<FavoriteData> likeBox = getLikeBox();
        List<FavoriteData> data = likeBox.query()
                .build()
                .find(offset, LikePS.pageSize);

        return data;
    }

    public static List<ResultData> queryAllData(int wallpaperType) {
        Box<ResultData> objectBoxLike = getDataBox();
        List<ResultData> data = objectBoxLike.query()
                .equal(ResultData_.wallpapertype, wallpaperType)
                .build()
                .find();

        return data;
    }
}
