package com.prank.happypranksounds.data.repository;

import android.os.Handler;
import android.os.Looper;

import androidx.lifecycle.LiveData;

import com.prank.happypranksounds.data.dao.HappyDataDao;
import com.prank.happypranksounds.data.entity.HappyData;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HappyRepository {
    private final HappyDataDao happyDataDao;
    private final ExecutorService executorService;

    public HappyRepository(HappyDataDao happyDataDao) {
        this.happyDataDao = happyDataDao;
        this.executorService = Executors.newSingleThreadExecutor();
    }

    public void insertAll(List<HappyData> happyDataList) {
        executorService.execute(() -> happyDataDao.insertAll(happyDataList));
    }

    public void insert(HappyData happyData) {
        executorService.execute(() -> happyDataDao.insert(happyData));
    }

    public void update(HappyData happyData) {
        executorService.execute(() -> happyDataDao.update(happyData));
    }

    public void delete(HappyData happyData) {
        executorService.execute(() -> happyDataDao.delete(happyData));
    }

    public LiveData<List<HappyData>> getFavorite() {
        return happyDataDao.getFavorite();
    }

    public LiveData<List<HappyData>> getFirstCategory() {
        return happyDataDao.getFirstCategory();
    }

    public LiveData<List<HappyData>> getCategoryList(String name) {
        return happyDataDao.getCategoryList(name);
    }

    public LiveData<List<HappyData>> getExternal() {
        return happyDataDao.getExternal();
    }

    public void getCountAsync(String uri, CountCallback callback) {
        executorService.execute(() -> {
            int count = happyDataDao.getCount(uri);
            new Handler(Looper.getMainLooper()).post(() -> callback.onCountFetched(count));
        });
    }

    public interface CountCallback {
        void onCountFetched(int count);
    }
}
