package com.hd.live.wall.wallpapers.utils;

import android.content.Context;

import androidx.annotation.NonNull;

import com.hd.live.wall.wallpapers.dbData.Collection;
import com.hd.live.wall.wallpapers.dbData.Collection_;
import com.hd.live.wall.wallpapers.dbData.MyObjectBox;
import com.hd.live.wall.wallpapers.dbData.WallData;
import com.hd.live.wall.wallpapers.dbData.WallData_;
import com.hd.live.wall.wallpapers.listener.ListenerLike;
import com.hd.live.wall.wallpapers.pages.PageSourceLike;
import com.hd.live.wall.wallpapers.pages.PageSourceMain;
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 dbManager {

    private static BoxStore boxStore;

    private static Box<WallData> dataBox;
    private static Box<Collection> likeBox;

    private static DataSubscription observer;


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

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

        return dataBox;
    }

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

        return likeBox;
    }

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

    }

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


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


    public static void deleteLike(int id) {
        Box<Collection> likeBox = getLikeBox();
        Collection first = likeBox.query()
                .equal(Collection_.id, id)
                .build()
                .findFirst();
        if (first != null) {
            likeBox.remove(first);
            helper.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<Collection> likeBox = getLikeBox();
        Collection first = likeBox.query()
                .equal(Collection_.id, id)
                .build()
                .findFirst();
        if (first != null) {
            return true;
        } else {
            return false;
        }
    }
    public static List<Collection> queryAllLike() {
        Box<Collection> likeBox = getLikeBox();
        return likeBox.query()
                .build()
                .find();
    }

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

        Box<WallData> objectBoxLike = getDataBox();
        List<WallData> data = objectBoxLike.query()
                .equal(WallData_.wallpapertype, wallpaperType)
                .build()
                .find(offset, PageSourceMain.pageSize);

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

        return data;
    }

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

        return data;
    }
}
