package com.example.petstoremobile.viewmodels;

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

import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.dtos.ProductDTO;
import com.example.petstoremobile.dtos.ProductSupplierDTO;
import com.example.petstoremobile.dtos.SupplierDTO;
import com.example.petstoremobile.repositories.ProductRepository;
import com.example.petstoremobile.repositories.ProductSupplierRepository;
import com.example.petstoremobile.repositories.SupplierRepository;
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 ProductSupplierDetailViewModel extends ViewModel {
    private final ProductSupplierRepository psRepository;
    private final ProductRepository productRepository;
    private final SupplierRepository supplierRepository;

    private boolean isEditing = false;
    private long editProductId = -1;
    private long editSupplierId = -1;

    private final MutableLiveData<List<ProductDTO>> productList = new MutableLiveData<>(new ArrayList<>());
    private final MutableLiveData<List<SupplierDTO>> supplierList = new MutableLiveData<>(new ArrayList<>());

    @Inject
    public ProductSupplierDetailViewModel(ProductSupplierRepository psRepository, ProductRepository productRepository, SupplierRepository supplierRepository) {
        this.psRepository = psRepository;
        this.productRepository = productRepository;
        this.supplierRepository = supplierRepository;
    }

    public void setEditMode(long productId, long supplierId) {
        this.isEditing = true;
        this.editProductId = productId;
        this.editSupplierId = supplierId;
    }

    public LiveData<Resource<ProductSupplierDTO>> loadProductSupplier() {
        return psRepository.getProductSupplierById(editProductId, editSupplierId);
    }

    public boolean isEditing() { return isEditing; }
    public long getEditProductId() { return editProductId; }
    public long getEditSupplierId() { return editSupplierId; }

    public LiveData<Resource<PageResponse<ProductDTO>>> loadProducts() {
        return productRepository.getAllProducts(null, null, 0, 200, "prodName");
    }

    public LiveData<Resource<PageResponse<SupplierDTO>>> loadSuppliers() {
        return supplierRepository.getAllSuppliers(0, 200, null, "supCompany");
    }

    public LiveData<Resource<ProductSupplierDTO>> saveProductSupplier(ProductSupplierDTO dto) {
        if (isEditing) {
            return psRepository.updateProductSupplier(editProductId, editSupplierId, dto);
        } else {
            return psRepository.createProductSupplier(dto);
        }
    }

    public LiveData<Resource<Void>> deleteProductSupplier() {
        return psRepository.deleteProductSupplier(editProductId, editSupplierId);
    }

    public void setProductList(List<ProductDTO> list) { productList.setValue(list); }
    public LiveData<List<ProductDTO>> getProductList() { return productList; }

    public void setSupplierList(List<SupplierDTO> list) { supplierList.setValue(list); }
    public LiveData<List<SupplierDTO>> getSupplierList() { return supplierList; }
}
