package com.example.fruitparty;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

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


import androidx.annotation.Nullable;


public class Category extends Activity {
    private List<Product>productList;
    public OnChangeListener onchangedListener;
    private List<String>productCategory=new ArrayList<>();
    private ListView titleList;
    private Adapter adapter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content);
        initData();
        init();
        SetDetailFragment fragment = new SetDetailFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.detail, fragment);
        transaction.commit();
        titleList.setOnItemClickListener((parent, view, position, id) -> {
            adapter.setSelectedPosition(position);
            adapter.notifyDataSetInvalidated();
            if (onchangedListener != null) {
                onchangedListener.changeText(productList.get(position));
            }
        });
   /*     titleList.setOnItemClickListener((parent, view, position, id) -> {
            adapter.setSelectedPosition(position);
            adapter.notifyDataSetInvalidated();
            if (onchangedListener != null) {
                onchangedListener.changeText(productList.get(position));
            }
        });
*/
    }

    public void setOnChangeListener(OnChangeListener onChangeListener) {
        this.onchangedListener = onChangeListener;
    }

    public interface OnChangeListener {
        void changeText(Product product);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        productList = new ArrayList<>();
        productCategory.add("西瓜");
        productCategory.add("桃子");
        productCategory.add("葡萄");
        productCategory.add("香蕉");
        productCategory.add("苹果");
        Product product = new Product();
        product.setImageUrlId(R.drawable.water);
        product.setProductName("西瓜");
//        product.setProductPrice(new BigDecimal("9.9"));
        product.setCharacterStory(" price:5元/斤     ");
        Product product1 = new Product();
        product1.setImageUrlId(R.drawable.taozi);
        product1.setProductName("桃子");
//        product1.setProductPrice(new BigDecimal("29.9"));
        product1.setCharacterStory("    price:8元/斤    ");
        Product product2 = new Product();
        product2.setImageUrlId(R.drawable.perple);
        product2.setProductName("葡萄");
//        product2.setProductPrice(new BigDecimal("19.9"));
        product2.setCharacterStory("  price:12元/斤      ");
        Product product3 = new Product();
        product3.setImageUrlId(R.drawable.banana);
        product3.setProductName("香蕉");
//        product3.setProductPrice(new BigDecimal("19.9"));
        product3.setCharacterStory("   price:4元/斤     ");
        Product product4 = new Product();
        product4.setImageUrlId(R.drawable.apple);
        product4.setProductName("苹果");
//        product4.setProductPrice(new BigDecimal("19.9"));
        product4.setCharacterStory("  price:6元/斤     ");
        productList.add(product);
        productList.add(product1);
        productList.add(product2);
        productList.add(product3);
        productList.add(product4);
    }


    /**
     * 初始化组件
     */
    private void init() {
        titleList = findViewById(R.id.list);
        adapter = new Adapter(productCategory, Category.this);
        titleList.setAdapter(adapter);
    }
}
