package com.example.register;

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.example.register.R;
import com.example.register.entity.Product;
import com.example.register.adapter.Adapter;
import com.example.register.fragment.SetDetailFragment;

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


public class CategoryActivity extends Activity {
    public OnChangeListener onchangedListener;
    private List<Product> productList;
    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_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 (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.rou);
        product.setProductName("肥肉饭");
        product.setProductPrice(new BigDecimal("19.9"));



        Product product1 = new Product();
        product1.setImageUrlId(R.drawable.haorou);
        product1.setProductName("严选套餐");
        product1.setProductPrice(new BigDecimal("29.9"));


        Product product2 = new Product();
        product2.setImageUrlId(R.drawable.xiaoshi);
        product2.setProductName("小食");
        product2.setProductPrice(new BigDecimal("9.9"));

        Product product3 = new Product();
        product3.setImageUrlId(R.drawable.shui);
        product3.setProductName("矿泉水");
        product3.setProductPrice(new BigDecimal("1.9"));


        Product product4 = new Product();
        product4.setImageUrlId(R.drawable.yinliao);
        product4.setProductName("雪碧");
        product4.setProductPrice(new BigDecimal("1.9"));


        productList.add(product);
        productList.add(product1);
        productList.add(product2);
        productList.add(product3);
        productList.add(product4);
    }

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