package com.free.life.wallpaper.ox;

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

import androidx.annotation.NonNull;

import com.free.life.wallpaper.MyWallpaper;
import com.free.life.wallpaper.mylistener.OnUpdateLikeListener;
import com.free.life.wallpaper.mylistener.OnLikeListener;
import com.free.life.wallpaper.bean.Data;
import com.free.life.wallpaper.bean.Data_;
import com.free.life.wallpaper.bean.MyObjectBox;

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

    private static BoxStore boxStore;

    private static Box<Data> LikeBox;




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



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

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

        return LikeBox;
    }



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

    }



    public static void insertOrUpdateLike(Data boxLike) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        String imId = boxLike.getImId();
        Data first = objectBoxLike.query()
                .equal(Data_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first == null) {
            Log.d(MyWallpaper.TAG, "-----------添加11 isLike=" + imId);
            boxLike.setLike(true);
            objectBoxLike.put(boxLike);
            for(OnUpdateLikeListener listener:list){
                listener.onAdd(boxLike);
            }
        } else {
            if(!first.isLike()){
                first.setLike(true);
                objectBoxLike.put(first);
                for(OnUpdateLikeListener listener:list){
                    listener.onAdd(first);
                }
                Log.d(MyWallpaper.TAG, "-----------已经存在添加22 isLike=" + imId);
            }

        }

    }

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

    }


    public static void deleteLike(Data likeData) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        String imId = likeData.getImId();
        Data boxLike = objectBoxLike.query()
                .equal(Data_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (boxLike != null) {
            if(boxLike.isDownload()){
                boxLike.setLike(false);
                objectBoxLike.put(boxLike);
                for(OnUpdateLikeListener listener:list){
                    listener.onRemove(boxLike);
                }
            }else {
                objectBoxLike.remove(boxLike);
                for(OnUpdateLikeListener listener:list){
                    listener.onRemove(boxLike);
                }
            }
            Log.d(MyWallpaper.TAG, "--------deleteLike imId=" + imId);
        }


    }
    public static void deleteDownload(Data downloadData) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        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(String imId) {
        Box<Data> objectBoxLike = getObjectBoxLike();
        Data first = objectBoxLike.query()
                .equal(Data_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if(first!= null&& first.isLike()){
            return true;
        }else {
            return false;
        }
    }




}
