package org.aplas.myshop;

import static org.junit.Assert.assertTrue;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.widget.AppCompatButton;
import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.core.app.ActivityScenario;

import org.aplas.myshop.adapters.ProductsRecyclerAdapter;
import org.aplas.myshop.model.Product;
import org.aplas.myshop.model.User;
import org.aplas.myshop.sql.Database;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;

import com.google.android.material.textfield.TextInputEditText;

@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class TestAddProducttoCart extends ViewTest{
    ActivityScenario<LoginActivity> scenario;
    private EditText nama, password;
    private Button loginButton;
    private Database database;
    private User user;
    private Product product;
    private RecyclerView recyclerView;
    private ProductsRecyclerAdapter productsRecyclerAdapter;

    private int buyerId = 1;
    private String buyerName = "buyer1@test.com";
    private String buyerPassword = "password";
    private String buyerRole = "Buyer";

    private int productId = 1;
    private String productName = "TestProduct";
    private int productPrice = 20000;



    @Before
    public void setUp() {
        scenario = ActivityScenario.launch(LoginActivity.class);
        scenario.moveToState(Lifecycle.State.CREATED);
        scenario.onActivity(activity -> {
            database = new Database(activity);
            nama = activity.findViewById(R.id.textInputEditTextUsername);
            password = activity.findViewById(R.id.textInputEditTextPassword);
            loginButton = activity.findViewById(R.id.appCompatButtonLogin);
        });
    }

    @After
    public void tearDown() {
        scenario.close();
        database.close();
    }

    public Product addProduct(Database database) {
        Product addedProduct = new Product();
        addedProduct.setId(productId);
        addedProduct.setName(productName);
        addedProduct.setPrice(productPrice);
        database.addProduct(addedProduct);
        return  addedProduct;
    }

    public User buyerLogin(LoginActivity activity) {
        User buyerUser = new User();
        buyerUser.setId(buyerId);
        buyerUser.setName(buyerName);
        buyerUser.setPassword(buyerPassword);
        buyerUser.setRole(buyerRole);
        database.addUser(buyerUser);

        nama.setText(buyerName);
        password.setText(buyerPassword);
        loginButton.performClick();

        return buyerUser;
    }

    private BuyerDetailProductActivity startBuyerDetailProductActivity(LoginActivity activity) {
        product = addProduct(database);
        user = buyerLogin(activity);
        Intent intent = Shadows.shadowOf(activity).getNextStartedActivity();

        BuyerHomeActivity buyerHomeActivity = Robolectric.buildActivity(BuyerHomeActivity.class, intent).create().start().get();
        buyerHomeActivity.getDataFromSQLite();

        RecyclerView recyclerView = (RecyclerView) buyerHomeActivity.findViewById(R.id.recyclerViewProducts);
        ProductsRecyclerAdapter adapter = (ProductsRecyclerAdapter) recyclerView.getAdapter();
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        recyclerView.measure(0, 0);
        recyclerView.layout(0, 0, 100, 10000);

        View productView = recyclerView.getChildAt(0);

        productView.performClick();

        intent = Shadows.shadowOf(activity).getNextStartedActivity();
        return Robolectric.buildActivity(BuyerDetailProductActivity.class, intent).create().start().get();
    }

    @Test
    public void testAddProducttoCart() {
        scenario.onActivity(main -> {
           BuyerDetailProductActivity activity = startBuyerDetailProductActivity(main);
           TextInputEditText productQuantity = activity.findViewById(R.id.textInputEditTextProductQuantity);
           AppCompatButton addButton = activity.findViewById(R.id.ButtonAddToCart);

           productQuantity.setText("2");
           addButton.performClick();
//           assertTrue(database.checkCart(productId, productName, productPrice, buyerName));
           testItem(true, database.checkCart(productId, productName, productPrice, buyerName), "Add Product to Cart function doesn't seem working, please check your add to cart button on click function inside BuyerDetailProductActivity or check your addCart function inside database class", 3);
        });
    }

}
