package com.example.myapplication;

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

import androidx.annotation.Nullable;


import com.google.android.material.slider.RangeSlider;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class CategoryActivity extends Activity {
    public RangeSlider.OnChangeListener onchangeListener
    private List<Product>productList;
    private List<String>productCategory = new ArrayList<>();
    private ListView titleList;
    private MyAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_category);
        initData();
        init();
        setDetailFragment fragment = new SetDetailFragment();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.category_detail,fragment);
        transaction.commit();
        titleList.setOnItemClickListener((parent,view,position,id) - >{
            adapter.setSelectedPosition(position);
            adapter.notifyDataSetInvalidated();
        if (onchangeListener!=null) {
            onchangeListener.changeText(productList.get(position));
        }
        });
    }
    public void setOnChangeListener(OnChangeListener onChangeListener){
        this.onchangeListener=onChangeListener;
    }
    public interface OnChageListener{
        void changeText(Product product);
    }
    private void initData(){
        productList=new ArrayList<>();
        productCategory.add("橘子");
        productCategory.add("橙子");
        productCategory.add("柚子");
        Product product=new Product();
        product.setImageUrlId(R.drawable.juzi);
        product.setProductName("橘子");
        product.setProductPrice(new BigDecimal("9.9"));
        Product product1=new Product();
        product.setImageUrlId(R.drawable.chengzi);
        product.setProductName("橙子");
        product.setProductPrice(new BigDecimal("29.9"));
        Product product2=new Product();
        product.setImageUrlId(R.drawable.youzi);
        product.setProductName("柚子");
        product.setProductPrice(new BigDecimal("19.9"));
        productList.add(product);
        productList.add(product1);
        productList.add(product2);
    }
    private void init(){
        titleList=findViewById(R.id.category_title_list);
        adapter=new MyAdapter(productCategory,CategoryActivity.this);
        titleList.setAdapter(adapter);
    }
}
