package org.aplas.myshop;
import static org.junit.Assert.assertFalse;

import android.content.Intent;
import android.view.View;

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.CartsRecyclerAdapter;
import org.aplas.myshop.model.Cart;
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;

@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestDeleteCart extends ViewTest{
    ActivityScenario<SellerCartActivity> scenario;
    private Database database;

    private int cartId = 1;
    private String cartUsername = "buyercart@test.com";
    private int productId = 1;
    private String productName = "CartProductTest";
    private int productPrice = 25000;
    private int productQuantity = 3;
    private Cart cart;

    @Before
    public void setUp() {
        scenario = ActivityScenario.launch(SellerCartActivity.class);
        scenario.moveToState(Lifecycle.State.CREATED);
        scenario.onActivity(activity -> {
            database = new Database(activity);
        });
    }

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

    public Cart addCart(Database database) {
        Cart addedCart = new Cart();
        addedCart.setId(cartId);
        addedCart.setUsername(cartUsername);
        addedCart.setProductId(productId);
        addedCart.setProductName(productName);
        addedCart.setProductPrice(productPrice);
        addedCart.setProductQuantity(productQuantity);
        database.addCart(addedCart);
        return addedCart;
    }

    private SellerDetailCartActivity startSellerDetailCartActivity(SellerCartActivity activity) {
        cart = addCart(database);
        activity.getDataFromSQLite();
        RecyclerView recyclerView = activity.findViewById(R.id.recyclerViewCarts);
        CartsRecyclerAdapter adapter = (CartsRecyclerAdapter) recyclerView.getAdapter();
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

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

        View cartView = recyclerView.getChildAt(0);
        cartView.performClick();
        Intent intent = Shadows.shadowOf(activity).getNextStartedActivity();
        return Robolectric.buildActivity(SellerDetailCartActivity.class, intent).create().start().get();
    }

    @Test
    public void testDeleteCart() {
        scenario.onActivity(main -> {
            SellerDetailCartActivity activity = startSellerDetailCartActivity(main);
            AppCompatButton deleteButton = activity.findViewById(R.id.ButtonDeleteFromCart);
            deleteButton.performClick();

//            assertFalse(database.checkCart(productId, productName, productPrice, cartUsername));
            testItem(false, database.checkCart(productId, productName, productPrice, cartUsername), "Delete cart function doesn't seem working, please check your delete cart button on click function inside SellerDetailCartActivity or check your delete cart function inside the database class", 4);
        });
    }
}
