package com.live.mylivewallpaper.help;

import android.content.Context;

import androidx.annotation.NonNull;

import com.live.mylivewallpaper.data.LikeData;
import com.live.mylivewallpaper.data.LikeData_;
import com.live.mylivewallpaper.data.MyObjectBox;
import com.live.mylivewallpaper.data.ResultData;
import com.live.mylivewallpaper.data.ResultData_;
import com.live.mylivewallpaper.listener.OnLikeUpdateListener;
import com.live.mylivewallpaper.paging.MyLikeSource;
import com.live.mylivewallpaper.paging.MyPagingSource;

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

    private static BoxStore boxStore;

    private static Box<ResultData> dataBox;
    private static Box<LikeData> 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<LikeData> getLikeBox() {
        if (likeBox == null) {
            likeBox = boxStore.boxFor(LikeData.class);
        }

        return likeBox;
    }

    public static DataSubscription setLikeUpdateListener(OnLikeUpdateListener listener) {
        Box<LikeData> likeBox = getLikeBox();
        Query<LikeData> build = likeBox.query()
                .build();
        return build.subscribe(new DataSubscriptionList())
                .on(AndroidScheduler.mainThread())
                .observer(new DataObserver<List<LikeData>>() {
                    @Override
                    public void onData(@NonNull List<LikeData> data) {
                        Common.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(LikeData likeData) {
        Box<LikeData> likeBox = getLikeBox();
        LikeData first = likeBox.query()
                .equal(LikeData_.id, likeData.getId())
                .build()
                .findFirst();
        if (first == null) {
            Common.logMsg("---insertLike-----------------id=" + likeData.getId());
            likeBox.put(likeData);
        } else {
            Common.logMsg("---insertLike-----------------id=" + likeData.getId());
        }
    }


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

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

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

        return data;
    }
    public static List<LikeData> queryLike( int currentPage) {
        int offset = (currentPage - 1) * MyLikeSource.pageSize;
        Box<LikeData> likeBox = getLikeBox();
        List<LikeData> data = likeBox.query()
                .build()
                .find(offset, MyLikeSource.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;
    }
}
