package com.offline.music.playermp3.objectbox;

import android.content.Context;

import androidx.annotation.NonNull;

import com.offline.music.playermp3.api.LikeSongListener;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.javabean.BoxLikeSong;
import com.offline.music.playermp3.javabean.BoxLikeSong_;
import com.offline.music.playermp3.javabean.MyObjectBox;

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

    private static BoxStore boxStore;

    private static Box<BoxLikeSong> boxLikeSongBox;

    private static DataSubscription observer;


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

    public static Box<BoxLikeSong> getObjectBoxLike() {
        if (boxLikeSongBox == null) {
            return boxStore.boxFor(BoxLikeSong.class);
        }
        return boxLikeSongBox;
    }

    public static DataSubscription setLikeDataListener(LikeSongListener listener) {
        Query<BoxLikeSong> build = getObjectBoxLike().query().build();
        return build.subscribe(new DataSubscriptionList())
                .on(AndroidScheduler.mainThread())
                .observer(new DataObserver<List<BoxLikeSong>>() {
                    @Override
                    public void onData(@NonNull List<BoxLikeSong> data) {
                        CommonUtils.LogMsg("------ProfileFragment  11data=" + data.size());
                        listener.onLikeSongChange(data);
                    }
                });

    }

    public static void insertOrUpdateLike(BoxLikeSong boxLikeSong) {
        Box<BoxLikeSong> objectBoxLike = getObjectBoxLike();
        String videoId = boxLikeSong.getVideoId();
        BoxLikeSong first = objectBoxLike.query()
                .equal(BoxLikeSong_.videoId, videoId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            CommonUtils.LogErrorMsg("-----------添加 song=" + boxLikeSong.getSongName() + "---videoId=" + videoId);
            objectBoxLike.put(boxLikeSong);
        } else {
            CommonUtils.LogErrorMsg("-----------已经存在 song=" + boxLikeSong.getSongName() + "---videoId=" + videoId);
        }

    }


    public static void deleteLike(BoxLikeSong boxLikeSong) {
        Box<BoxLikeSong> objectBoxLike = getObjectBoxLike();
        String videoId = boxLikeSong.getVideoId();
        List<BoxLikeSong> boxLikeSongs = objectBoxLike.query()
                .equal(BoxLikeSong_.videoId, videoId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .find();
        for (BoxLikeSong song : boxLikeSongs) {
            objectBoxLike.remove(song);
            CommonUtils.LogErrorMsg("-------onLikeSongChange-deleteLike song=" + song.getSongName());
        }

    }

    public static List<BoxLikeSong> queryAllLike() {
        return getObjectBoxLike().query()
                .build()
                .find();
    }

    public static boolean queryIsLike(String videoId) {
        Box<BoxLikeSong> objectBoxLike = getObjectBoxLike();
        BoxLikeSong first = objectBoxLike.query()
                .equal(BoxLikeSong_.videoId, videoId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            return false;
        } else {
            return true;
        }

    }

}
