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("陈奕迅");
        productCategory.add("周杰伦");
        productCategory.add("邓丽君");
        Product product = new Product();
        product.setImageUrlId(R.drawable.yi);

        product.setProductName("易烊千玺的明信片");
        product.setProductPrice(new BigDecimal("9.9"));

        Product product1 = new Product();
        product1.setImageUrlId(R.drawable.liuyifeu);
        product1.setProductName("刘亦菲的明信片");
        product1.setProductPrice(new BigDecimal("29.9"));
        Product product2 = new Product();
        product2.setImageUrlId(R.drawable.songysxuan);
        product2.setProductName("宋亚轩");
        product2.setProductPrice(new BigDecimal("19.9"));
        Product product3 = new Product();
        product3.setImageUrlId(R.drawable.liuyaow);
        product3.setProductName("刘耀文");
        product3.setProductPrice(new BigDecimal("19.9"));
        Product product4 = new Product();
        product4.setImageUrlId(R.drawable.maobiyi);
        product4.setProductName("毛不易");
        product4.setProductPrice(new BigDecimal("19.9"));
        Product product5 = new Product();
        product5.setImageUrlId(R.drawable.xiuzhiqian);
        product5.setProductName("薛之谦");
        product5.setProductPrice(new BigDecimal("19.9"));

        Product product6 = new Product();
        product6.setImageUrlId(R.drawable.chyixun);
        product6.setProductName("陈奕迅");
        product6.setProductPrice(new BigDecimal("19.9"));
        Product product7 = new Product();
        product7.setImageUrlId(R.drawable.zhoujiel);
        product7.setProductName("周杰伦");
        product7.setProductPrice(new BigDecimal("19.9"));
        Product product8 = new Product();
        product8.setImageUrlId(R.drawable.denglijun);
        product8.setProductName("邓丽君");
        product8.setProductPrice(new BigDecimal("19.9"));

        productList.add(product);
        productList.add(product1);
        productList.add(product2);
        productList.add(product3);
        productList.add(product4);
        productList.add(product5);
        productList.add(product6);
        productList.add(product7);
        productList.add(product8);
    }

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