package com.example.orangesale_04.activity;

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 androidx.annotation.Nullable;

import com.example.orangesale_04.R;
import com.example.orangesale_04.adapter.Adapter;
import com.example.orangesale_04.entity.Product;
import com.example.orangesale_04.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(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long 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("美妆");
        productCategory.add("洗护");
        productCategory.add("车品");
        Product product = new Product();
        product.setImageUrlId(R.drawable.cloth1);
        product.setProductName("jsister 2023春装专柜新款 JS女装时尚拼色短袖针织衫");
        product.setProductPrice(new BigDecimal("9.9"));
        Product product1 = new Product();
        product1.setImageUrlId(R.drawable.cloth2);
        product1.setProductName("E2023春季新款男士卫衣圆领刺绣宽松百搭套头衫ins情侣款外套上衣");
        product1.setProductPrice(new BigDecimal("29.9"));
        Product product2 = new Product();
        product2.setImageUrlId(R.drawable.pill);
        product2.setProductName("浙江医药维生素E软胶囊30粒心脑血管流产不孕正品药");
        product2.setProductPrice(new BigDecimal("19.9"));
        Product product3 = new Product();
        product3.setImageUrlId(R.drawable.meizhuang);
        product3.setProductName("棉花糖三角粉扑气垫海绵美妆蛋化妆干湿两用粉饼粉底液专用不吃粉");
        product3.setProductPrice(new BigDecimal("8.8"));
        Product product4 = new Product();
        product4.setImageUrlId(R.drawable.xihu);
        product4.setProductName("Mamakids婴儿泡沫洁肤液460ml洗发液370ml宝宝洗护");
        product4.setProductPrice(new BigDecimal("9.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);
    }
}