package com.yono.messeripos.repositories;

import android.app.Application;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;

import com.google.gson.Gson;
import com.yono.messeripos.daos.CartDaos;
import com.yono.messeripos.database.LocalDatabase;
import com.yono.messeripos.models.ProductCartModels;

import java.util.List;

public class CartRepositories {
    private CartDaos cartDaos;
    private LiveData<List<ProductCartModels>> products;

    public CartRepositories(Application application){
        LocalDatabase db = LocalDatabase.geDatabase(application);
        cartDaos = db.cartDaos();

        products = cartDaos.getCart();

        Log.d("Data cart", "Response "+products);
    }

    public void insert(ProductCartModels productCartModels){
        LocalDatabase.databaseWriterExecutor.execute(() -> cartDaos.insertProduct(productCartModels));
    }

    public void update(ProductCartModels productCartModels){
        LocalDatabase.databaseWriterExecutor.execute(() -> cartDaos.updateProduct(productCartModels));
    }

    public void delete(ProductCartModels productCartModels){
        LocalDatabase.databaseWriterExecutor.execute(() -> cartDaos.deleteProduct(productCartModels));
    }

    public void updateQty(long qty, long id){
        LocalDatabase.databaseWriterExecutor.execute(() ->cartDaos.updateCart(qty, id));
    }

    public LiveData<List<ProductCartModels>> getDataById(String id){
        return cartDaos.getCartById(id);
    }

    public LiveData<List<ProductCartModels>> getCartByOrderId(String idOrder, long idProduct){
        return cartDaos.getCartByOrderId(idOrder, idProduct);
    }

    public LiveData<List<ProductCartModels>> getDataCart(){
        return products;
    }
}
