package com.yono.messeripos.models;

import android.util.Log;

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

import com.google.gson.Gson;
import com.yono.messeripos.api.client.Client;
import com.yono.messeripos.api.service.CategoryServise;
import com.yono.messeripos.api.service.ProductService;
import com.yono.messeripos.api.service.TransactionService;
import com.yono.messeripos.api.service.UsersService;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.utils.Utils;

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

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

public class MainViewModels extends ViewModel {
    MutableLiveData<DataResponse<List<ProductModels<CategoryModels>>>> productList;
    MutableLiveData<DataResponse<List<ProductModels>>> productByCategory;
    MutableLiveData<DataResponse<List<CategoryModels>>> categoryList;
    MutableLiveData<DataResponse<UsersModels>> usersLogin;
    MutableLiveData<DataResponse<UsersModels>> usersRegister;

    MutableLiveData<DataResponse<List<PaymentsModels<String>>>> transactions;

    private MainViewModels mainViewModels;

    Client client = new Client();
    Utils utils = new Utils();

    public MainViewModels() {
        productList = new MutableLiveData<>();
        categoryList = new MutableLiveData<>();
        productByCategory = new MutableLiveData<>();
        transactions = new MutableLiveData<>();

    }

    public LiveData<DataResponse<List<ProductModels<CategoryModels>>>> getProduct() {
        getProductList();
        return productList;
    }

    public LiveData<DataResponse<List<CategoryModels>>> getCategory() {
        getCategoryList();
        return categoryList;
    }

    public void postCategory(CategoryModels categoryModels) {
        saveCategory(categoryModels);
    }

    public LiveData<DataResponse<List<ProductModels>>> getProductByCategory(int id) {
        getDataProductByCategory(id);
        return productByCategory;
    }

    public LiveData<DataResponse<List<PaymentsModels<String>>>> getAllTransactions() {
        getDataTransaction();
        return transactions;
    }


    private void saveCategory(CategoryModels categoryModels) {
        CategoryServise categoryServise = client.Client(CategoryServise.class);
        categoryServise.addCategory(categoryModels).enqueue(new Callback<DataResponse<CategoryModels>>() {
            @Override
            public void onResponse(Call<DataResponse<CategoryModels>> call, Response<DataResponse<CategoryModels>> response) {
                Log.i("Add Categiry", "onResponse: " + utils.convertGson(response.body()));
            }

            @Override
            public void onFailure(Call<DataResponse<CategoryModels>> call, Throwable t) {
                Log.e("error add", "onFailure: ", t);
            }
        });
    }

    public void deleteProducts(int id) {
        ProductService productService = client.Client(ProductService.class);
        productService.deleteProducts(id).enqueue(new Callback<DataResponse<ProductModels>>() {
            @Override
            public void onResponse(Call<DataResponse<ProductModels>> call, Response<DataResponse<ProductModels>> response) {
                Log.i("Delete", "onResponse: " + utils.convertGson(response.body()));
            }

            @Override
            public void onFailure(Call<DataResponse<ProductModels>> call, Throwable t) {
                Log.e("error delete", "onFailure: ", t);
            }
        });
    }

    private void getDataTransaction() {
        TransactionService transactionService = client.Client(TransactionService.class);

        transactionService.getAllTransaction().enqueue(new Callback<DataResponse<List<PaymentsModels<String>>>>() {
            @Override
            public void onResponse(Call<DataResponse<List<PaymentsModels<String>>>> call,
                                   Response<DataResponse<List<PaymentsModels<String>>>> response) {
                Log.i("Get all transaction", "onResponse: " + utils.convertGson(response.body()));
                if (response.body() != null) {
                    transactions.setValue(response.body());
                } else {
                    transactions = null;
                }
            }

            @Override
            public void onFailure(Call<DataResponse<List<PaymentsModels<String>>>> call, Throwable t) {
                Log.e("Error get transaction", "onFailure: ", t);
                transactions = null;
            }
        });
    }

    private void getDataProductByCategory(int id) {
        CategoryServise categoryServise = client.Client(CategoryServise.class);

        categoryServise.getCategoryById(id).enqueue(new Callback<DataResponse<List<ProductModels>>>() {
            @Override
            public void onResponse(Call<DataResponse<List<ProductModels>>> call,
                                   Response<DataResponse<List<ProductModels>>> response) {
                String js = new Gson().toJson(response.body());
                Log.i("Get data by category", "onResponse: " + js);
                if (response != null) {
                    productByCategory.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<DataResponse<List<ProductModels>>> call, Throwable t) {
                Log.e("Error!", "onFailure: ", t);
            }
        });
    }

    public MutableLiveData<DataResponse<UsersModels>> checkLogin(UsersModels usersModels) {
        usersLogin = new MutableLiveData<>();
        PeriksaLogin(usersModels);
        return usersLogin;
    }

    public MutableLiveData<DataResponse<UsersModels>> postRegis(UsersModels usersModels) {
        usersRegister= new MutableLiveData<>();
        CreateRegister(usersModels);
        return usersRegister;
    }


    private void PeriksaLogin(UsersModels usersModels) {
        UsersService usersService = client.Client(UsersService.class);

        //cek login
        usersService.checkLogin(usersModels).enqueue(new Callback<DataResponse<UsersModels>>() {
            @Override
            public void onResponse(Call<DataResponse<UsersModels>> call, Response<DataResponse<UsersModels>> response) {
                if (response.body() != null) {
                    usersLogin.setValue(response.body());
                }
                String js = new Gson().toJson(response.body());
                Log.d("Periksa login", "Response " + js);
            }

            @Override
            public void onFailure(Call<DataResponse<UsersModels>> call, Throwable t) {
                Log.e("Error Login", "Response " + t.getMessage());
            }
        });

    }

    private void CreateRegister(UsersModels usersModels){

        UsersService usersService = client.Client(UsersService.class);
        //post register
        usersService.postRegister(usersModels).enqueue(new Callback<DataResponse<UsersModels>>() {
            @Override
            public void onResponse(Call<DataResponse<UsersModels>> call, Response<DataResponse<UsersModels>> response) {
                Log.i("REGISTER", "onResponse: "+utils.convertGson(response.body()));
                usersRegister.setValue(response.body());
            }

            @Override
            public void onFailure(Call<DataResponse<UsersModels>> call, Throwable t) {
                Log.e("Error Register","Response"+t.getMessage());
            }
        });
    }


    private void getProductList() {
        ProductService productService = client.Client(ProductService.class);
        Log.i("TAG", "getProductList: dari get product");
        productService.getProducts().enqueue(new Callback<DataResponse<List<ProductModels<CategoryModels>>>>() {
            @Override
            public void onResponse(Call<DataResponse<List<ProductModels<CategoryModels>>>> call, Response<DataResponse<List<ProductModels<CategoryModels>>>> response) {
                Log.i("Get products", "onResponse: " + utils.convertGson(response.body()));
                if (response.body() != null) {
                    productList.setValue(response.body());
                } else {
                    productList.setValue(null);
                }
            }

            @Override
            public void onFailure(Call<DataResponse<List<ProductModels<CategoryModels>>>> call, Throwable t) {
                Log.e("Error get product", "Response " + t.getMessage());

            }
        });
    }

    private void getCategoryList() {
        CategoryServise categoryServise = client.Client(CategoryServise.class);

        categoryServise.getCategory().enqueue(new Callback<DataResponse<List<CategoryModels>>>() {
            @Override
            public void onResponse(Call<DataResponse<List<CategoryModels>>> call, Response<DataResponse<List<CategoryModels>>> response) {
                String js = new Gson().toJson(response.body());
                Log.d("Get Data Category", "Response " + js);
                if (response.body() != null) {
                    categoryList.setValue(response.body());
                } else {
                    categoryList.setValue(null);
                }
            }

            @Override
            public void onFailure(Call<DataResponse<List<CategoryModels>>> call, Throwable t) {
                Log.e("Error get product", "Response " + t.getMessage());
            }
        });
    }
}
