package com.fujica.abk.utils;

import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import com.fujica.abk.ResourceTable;

/**
 * 自定义列表对话框
 * 
 * 使用示例：
 * <pre>
 * String[] options = {"选项1", "选项2", "选项3"};
 * new CustomListDialog(context)
 *     .setTitle("请选择")
 *     .setItems(options)
 *     .setOnItemClickListener((dialog, index) -> {
 *         // 处理选择
 *         dialog.dismiss();
 *     })
 *     .show();
 * </pre>
 * 
 * 特点：
 * - 使用 XML 布局文件，弹框宽度固定 280vp
 * - 列表项使用自定义布局，居中显示
 * - 背景半透明黑色遮罩，圆角白色卡片
 * - 支持链式调用，使用方便
 */
public class CustomListDialog {
    
    private Context context;
    private CommonDialog dialog;
    private Component rootView;
    private Text titleText;
    private ListContainer listContainer;
    private Button cancelButton;
    
    private String title = "请选择";
    private String[] items;
    private OnItemClickListener itemClickListener;
    private int selectedIndex = -1; // 记录当前选中项的索引，-1表示未选中
    
    /**
     * 列表项点击监听器接口
     */
    public interface OnItemClickListener {
        void onItemClick(CustomListDialog dialog, int index);
    }
    
    public CustomListDialog(Context context) {
        this.context = context;
        initDialog();
    }
    
    /**
     * 初始化对话框
     */
    private void initDialog() {
        dialog = new CommonDialog(context);
        
        // 加载自定义布局
        rootView = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_custom_list_dialog, null, false);
        
        // 查找组件
        titleText = (Text) rootView.findComponentById(ResourceTable.Id_dialog_title);
        listContainer = (ListContainer) rootView.findComponentById(ResourceTable.Id_list_container);
        cancelButton = (Button) rootView.findComponentById(ResourceTable.Id_btn_cancel);
        
        // 设置取消按钮点击事件
        cancelButton.setClickedListener(component -> dismiss());
        
        // 设置对话框内容
        dialog.setContentCustomComponent(rootView);
        
        // 设置对话框属性
        dialog.setTransparent(true); // 设置透明背景
        dialog.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, 
                      ComponentContainer.LayoutConfig.MATCH_PARENT);
    }
    
    /**
     * 设置标题
     */
    public CustomListDialog setTitle(String title) {
        this.title = title;
        if (titleText != null) {
            titleText.setText(title);
        }
        return this;
    }
    
    /**
     * 设置列表项
     */
    public CustomListDialog setItems(String[] items) {
        this.items = items;
        updateList();
        return this;
    }
    
    /**
     * 设置列表项点击监听器
     */
    public CustomListDialog setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
        return this;
    }
    
    /**
     * 设置当前选中项
     */
    public CustomListDialog setSelectedIndex(int index) {
        this.selectedIndex = index;
        updateList();
        return this;
    }
    
    /**
     * 获取当前选中项索引
     */
    public int getSelectedIndex() {
        return selectedIndex;
    }
    
    /**
     * 更新列表
     */
    private void updateList() {
        if (listContainer == null || items == null || items.length == 0) {
            return;
        }
        
        // 创建并设置适配器
        CustomListAdapter adapter = new CustomListAdapter(context, items, selectedIndex);
        listContainer.setItemProvider(adapter);
        
        // 设置列表项点击事件
        listContainer.setItemClickedListener((listContainer, component, position, id) -> {
            // 更新选中项
            selectedIndex = position;
            // 刷新列表以更新颜色
            updateList();
            
            if (itemClickListener != null) {
                itemClickListener.onItemClick(this, position);
            }
        });
    }
    
    /**
     * 显示对话框
     */
    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }
    
    /**
     * 关闭对话框
     */
    public void dismiss() {
        if (dialog != null) {
            dialog.hide();
        }
    }
    
    /**
     * 销毁对话框
     */
    public void destroy() {
        if (dialog != null) {
            dialog.destroy();
            dialog = null;
        }
    }
    
    /**
     * 自定义列表适配器
     */
    private static class CustomListAdapter extends BaseItemProvider {
        private Context context;
        private String[] items;
        private int selectedIndex; // 当前选中项索引
        
        public CustomListAdapter(Context context, String[] items, int selectedIndex) {
            this.context = context;
            this.items = items;
            this.selectedIndex = selectedIndex;
        }
        
        @Override
        public int getCount() {
            return items != null ? items.length : 0;
        }
        
        @Override
        public Object getItem(int position) {
            return items != null && position < items.length ? items[position] : null;
        }
        
        @Override
        public long getItemId(int position) {
            return position;
        }
        
        @Override
        public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
            ViewHolder holder;
            Component component = convertComponent;
            
            if (component == null) {
                component = LayoutScatter.getInstance(context)
                        .parse(ResourceTable.Layout_item_custom_list_dialog, null, false);
                holder = new ViewHolder();
                holder.textItem = (Text) component.findComponentById(ResourceTable.Id_text_item);
                holder.divider = component.findComponentById(ResourceTable.Id_divider);
                component.setTag(holder);
            } else {
                holder = (ViewHolder) component.getTag();
            }
            
            // 设置文本
            if (holder.textItem != null && items != null && position < items.length) {
                holder.textItem.setText(items[position]);
                
                // 根据是否选中设置文本颜色
                if (position == selectedIndex) {
                    // 选中项设置为橙色
                    holder.textItem.setTextColor(new ohos.agp.utils.Color(0xFFFF6600));
                } else {
                    // 未选中项设置为默认颜色
                    holder.textItem.setTextColor(new ohos.agp.utils.Color(0xFF333333));
                }
            }
            
            // 最后一项隐藏分割线
            if (holder.divider != null) {
                holder.divider.setVisibility(position == getCount() - 1 ? 
                        Component.HIDE : Component.VISIBLE);
            }
            
            return component;
        }
        
        /**
         * ViewHolder 模式
         */
        private static class ViewHolder {
            Text textItem;
            Component divider;
        }
    }
}

