package com.yono.messeripos.models;

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

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.Transformations;
import androidx.room.Dao;

import com.google.gson.Gson;
import com.yono.messeripos.CartActivity;
import com.yono.messeripos.MainActivity;
import com.yono.messeripos.api.client.Client;
import com.yono.messeripos.api.service.OrdersService;
import com.yono.messeripos.api.service.PaymentService;
import com.yono.messeripos.repositories.CartRepositories;
import com.yono.messeripos.repositories.PaymentPendingRepositories;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.response.OrdersResponse;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.yono.messeripos.CartActivity.statusPost;

public class MainViewModelsCart extends AndroidViewModel {
    private MutableLiveData<ProductCartModels> cart = new MutableLiveData<>();
    private LiveData<List<ProductCartModels>> productsLive;
    private Client client;

    private CartRepositories cartRepositories;
    private PaymentPendingRepositories paymentPendingRepositories;

    public void setCart(ProductCartModels product){this.cart.setValue(product);}

    public LiveData<List<ProductCartModels>> getCartProduct(){
        return productsLive;
    }

    public MainViewModelsCart(@NonNull Application application) {
        super(application);

        cartRepositories = new CartRepositories(application);
        paymentPendingRepositories = new PaymentPendingRepositories(application);

        productsLive = cartRepositories.getDataCart();
        client = new Client();

    }

    public void insertCart(ProductCartModels productCartModels){cartRepositories.insert(productCartModels);}

    public void updateCart(ProductCartModels productCartModels){cartRepositories.update(productCartModels);}

    public void deleteCart(ProductCartModels productCartModels){cartRepositories.delete(productCartModels);}

    public void updateQtyCart(long qty, long id){cartRepositories.updateQty(qty, id);}

    public void savePaymentPending(PaymentPending paymentPending){
        paymentPendingRepositories.insert(paymentPending);
    }

    public LiveData<List<PaymentPending>> getAllPaymentPending(){
        return paymentPendingRepositories.getPaymentPending();
    }

    public void insertPayments(){

    }

//    public void insertPayments(PaymentsModels paymentsModels){
//        PaymentService paymentService = client.Client(PaymentService.class);
//        paymentService.postFromCart(paymentsModels).enqueue(new Callback<DataResponse<PaymentsModels<List<VaNumberModels>>>>() {
//            @Override
//            public void onResponse(Call<DataResponse<PaymentsModels<List<VaNumberModels>>>> call, Response<DataResponse<PaymentsModels>> response) {
//
//            }
//
//            @Override
//            public void onFailure(Call<DataResponse<PaymentsModels>> call, Throwable t) {
//
//            }
//        });
//    }

    public void insertOrders(OrdersResponse ordersResponse){
        Client client = new Client();
        OrdersService ordersService = client.Client(OrdersService.class);

        ordersService.postOrders(ordersResponse).enqueue(new Callback<DataResponse>() {
            @Override
            public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
                Log.i("Response post orders", "onResponse: "+response.body());
                if (response.body() != null){
                    statusPost = true;
                    Log.i("Log didalam if act", "insertOrders: "+ statusPost);
                }else{
                    statusPost = false;
                    Log.i("Log dialam else act", "insertOrders: "+statusPost);
                }
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onFailure(Call<DataResponse> call, Throwable t) {
                Log.e("Error response post orders", "onFailure: ",t );
                statusPost = false;
                Log.i("Log didalam errror act", "insertOrders: "+statusPost);
            }
        });
    }

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

    public LiveData<List<ProductCartModels>> checkOrderProduct(long idProduct, String idOrders){
        return cartRepositories.getCartByOrderId(idOrders, idProduct);
    }

//    public LiveData<List<ProductCartModels>> getCartByOrderId(long id){
//        return cartRepositories.getDataById(id);
//    }

    public MutableLiveData<ProductCartModels> getCart(){
//        String js = new Gson().toJson(cartRepositories.getDataCart());
//        Log.d("Get data cart", "Response "+js);
        return cart;
    }
}
