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.ProductsRecyclerAdapter;
import org.aplas.myshop.model.Product;
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 TestDeleteProduct extends ViewTest{
    ActivityScenario<SellerHomeActivity> scenario;
    private Database database;
    private Product product;
    private RecyclerView recyclerView;
    private ProductsRecyclerAdapter productsRecyclerAdapter;
    private int productId = 1;
    private String productName = "TestProduct";
    private int productPrice = 20000;

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

    @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;
    }

    private SellerDetailProductActivity startSellerDetailProductActivity(SellerHomeActivity activity) {
        product = addProduct(database);
        activity.getDataFromSQLite();

        RecyclerView recyclerView = (RecyclerView) activity.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 intent = Shadows.shadowOf(activity).getNextStartedActivity();
        return Robolectric.buildActivity(SellerDetailProductActivity.class, intent).create().start().get();
    }

    @Test
    public void testDeleteProduct() {
        scenario.onActivity(main -> {
           SellerDetailProductActivity activity = startSellerDetailProductActivity(main);
           AppCompatButton removeButton = (AppCompatButton) activity.findViewById(R.id.ButtonRemoveProduct);
           removeButton.performClick();
//           assertFalse(database.checkProduct(productName, String.valueOf(productPrice)));
           testItem(false, database.checkProduct(productName, String.valueOf(productPrice)), "Delete product function doesn't seem working, please check your delete button on click function inside SellerDetailProductActivity or check your delete product function inside the database class", 4);
        });
    }
}
