package com.example.petstoremobile.viewmodels;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.petstoremobile.dtos.CouponDTO;
import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.repositories.CouponRepository;
import com.example.petstoremobile.utils.Resource;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.lifecycle.HiltViewModel;

@HiltViewModel
public class CouponListViewModel extends ViewModel {
    private final CouponRepository repository;
    private final MutableLiveData<List<CouponDTO>> coupons = new MutableLiveData<>(new ArrayList<>());
    private final MutableLiveData<Boolean> isLoading = new MutableLiveData<>(false);

    @Inject
    public CouponListViewModel(CouponRepository repository) {
        this.repository = repository;
    }

    public LiveData<List<CouponDTO>> getCoupons() { return coupons; }
    public LiveData<Boolean> getIsLoading() { return isLoading; }

    public void loadCoupons(int page, int size, Boolean active, String discountType, String sort) {
        isLoading.setValue(true);
        repository.getAllCoupons(page, size, active, discountType, sort).observeForever(resource -> {
            if (resource != null) {
                if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
                    coupons.setValue(resource.data.getContent());
                    isLoading.setValue(false);
                } else if (resource.status == Resource.Status.ERROR) {
                    isLoading.setValue(false);
                }
            }
        });
    }

    public LiveData<Resource<Void>> bulkDeleteCoupons(List<Long> ids) {
        return repository.bulkDeleteCoupons(ids);
    }
}
