package com.apps.chillscreen.tool;


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

import com.apps.chillscreen.bean.MyObjectBox;
import com.apps.chillscreen.bean.Picture;
import com.apps.chillscreen.bean.Picture_;
import com.apps.chillscreen.listener.BookmarkListener;

import java.util.List;

import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.query.Query;
import io.objectbox.query.QueryBuilder;
import io.objectbox.reactive.DataSubscription;
import io.objectbox.reactive.DataSubscriptionList;

public class PictureManager {
    private static BoxStore boxStore;
    private static Box<Picture> boxPicture;

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

    private static Box<Picture> getBoxPicture() {
        if (boxPicture == null) {
            boxPicture = boxStore.boxFor(Picture.class);
        }
        return boxPicture;
    }

    public static void addPicture(Picture picture) {
        Box<Picture> boxPictureNow = getBoxPicture();
        String imId = picture.getImId();
        Picture first = boxPictureNow.query().equal(Picture_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE).build().findFirst();
        if (first == null) {
            Log.d("YYYYYY", "add picture id:" + imId);
            boxPictureNow.put(picture);
        } else {
            Log.d("YYYYYY", "picture exist");
        }
    }

    public static List<Picture> queryCover(String name) {
        Box<Picture> box = getBoxPicture();
        List<Picture> cover = box.query().equal(Picture_.categoryName, name, QueryBuilder.StringOrder.CASE_SENSITIVE).build().find(13, 1);
        Log.d("YYYYYY", "name is" + name + "cover is" + cover.get(0).getFullUrl());
        return cover;
    }

    public static DataSubscription setCollectListener(BookmarkListener listener) {
        Query<Picture> build = getBoxPicture().query()
                .equal(Picture_.isLike, true).build();
        //当调用 subscribe() 时，ObjectBox 会立即执行一次基础查询
        return build.subscribe(new DataSubscriptionList()).observer(data -> {
            Log.d("YYYYYY", "data size:" + data.size());
            listener.onCollectList(data);
        });
    }

    public static List<Picture> queryEachGenera(String name) {
        Box<Picture> box = getBoxPicture();
        List<Picture> pictures = box.query().equal(Picture_.categoryName, name, QueryBuilder.StringOrder.CASE_SENSITIVE).build().find();
        return pictures;
    }

    public static void updateCollect(Picture picture) {
        Box<Picture> box = getBoxPicture();
        box.put(picture);
    }

    public static boolean queryIsLike(String imId) {
        Box<Picture> box = getBoxPicture();
        Picture first = box.query()
                .equal(Picture_.imId, imId, QueryBuilder.StringOrder.CASE_SENSITIVE)
                .build()
                .findFirst();
        if (first != null && first.isLike()) {
            return true;
        } else {
            return false;
        }
    }

    public static List<Picture> search(String name) {
        Box<Picture> box = getBoxPicture();
        List<Picture> picture = box.query().contains(Picture_.description, name, QueryBuilder.StringOrder.CASE_SENSITIVE).build().find();
        return picture;
    }

    public static void insertOrUpdateDownload(Picture picture){
        Box<Picture> box=getBoxPicture();
        Picture first = box.query().equal(Picture_.imId, picture.getImId(), QueryBuilder.StringOrder.CASE_SENSITIVE).build().findFirst();
        if(first==null){
            picture.setDownload(true);
            box.put(picture);
            Log.d("download","insert a picture");
        } else if (!first.isDownload()) {
            first.setDownload(true);
            box.put(first);
            Log.d("download","update a picture");
        }
    }
}
