package com.fujica.abk.component;

import com.fujica.abk.ResourceTable;
import com.fujica.abk.model.out.OrderRes;
import com.fujica.abk.utils.Log;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.app.Context;

import java.text.DecimalFormat;

/**
 * 订单列表项组件
 */
public class OrderItemComponent extends DirectionalLayout {
    private OrderRes orderData;
    private Component rootLayout;
    private OnItemSelectedListener onItemSelectedListener;
    private boolean isSelected = false;

    /**
     * 选中状态监听器
     */
    public interface OnItemSelectedListener {
        void onItemSelected(OrderItemComponent component, OrderRes order);
        void onItemDeselected(OrderItemComponent component);
    }

    /**
     * 构造函数（通过 OrderRes 对象创建）
     */
    public OrderItemComponent(Context context, OrderRes order) {
        super(context);
        this.orderData = order;
        initComponent(context);
        bindData();
    }

    /**
     * 初始化组件
     */
    private void initComponent(Context context) {
        // 设置布局属性
        setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        setOrientation(VERTICAL);

        // 加载布局文件
        rootLayout = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_item_order, null, false);
        addComponent(rootLayout);
        
        // 初始化背景为普通状态
        updateBackground(false);

        // 设置整个列表项的点击事件
        setClickedListener(component -> {
            if (orderData != null) {
                // 如果已选中，则取消选中；否则选中
                if (isSelected) {
                    setSelected(false);
                    if (onItemSelectedListener != null) {
                        onItemSelectedListener.onItemDeselected(this);
                    }
                } else {
                    setSelected(true);
                    if (onItemSelectedListener != null) {
                        onItemSelectedListener.onItemSelected(this, orderData);
                    }
                }
            }
        });
    }

    /**
     * 绑定数据到 UI
     */
    private void bindData() {
        if (orderData == null || rootLayout == null) {
            return;
        }

        try {
            // 设置商品名称
            Text goodsNameText = (Text) rootLayout.findComponentById(ResourceTable.Id_text_goods_name);
            if (goodsNameText != null) {
                goodsNameText.setText(orderData.getGoodsName() != null ? orderData.getGoodsName() : "--");
            }

            // 设置状态
            Text statusText = (Text) rootLayout.findComponentById(ResourceTable.Id_text_status);
            if (statusText != null) {
                statusText.setText("已完成");
            }

            // 设置图标显示
            Boolean ykxf = orderData.getBizType() != null && orderData.getBizType() == 2;
            Component iconYkxf = rootLayout.findComponentById(ResourceTable.Id_icon_ykxf);
            Component iconTcjf = rootLayout.findComponentById(ResourceTable.Id_icon_tcjf);
            if (iconYkxf != null) {
                iconYkxf.setVisibility(ykxf ? VISIBLE : HIDE);
            }
            if (iconTcjf != null) {
                iconTcjf.setVisibility(ykxf ? HIDE : VISIBLE);
            }

            // 设置车场名称
            Text parkNameText = (Text) rootLayout.findComponentById(ResourceTable.Id_text_park_name);
            if (parkNameText != null) {
                parkNameText.setText(orderData.getParkName() != null ? orderData.getParkName() : "--");
            }

            // 设置支付时间
            Text timeText = (Text) rootLayout.findComponentById(ResourceTable.Id_text_pay_time);
            if (timeText != null) {
                timeText.setText(orderData.getPaySuccessTime() != null ? orderData.getPaySuccessTime() : "--");
            }

            // 设置金额
            Text amountText = (Text) rootLayout.findComponentById(ResourceTable.Id_text_amount);
            if (amountText != null) {
                DecimalFormat df = new DecimalFormat("0.00");
                double amount = orderData.getActualAmount() != null ? orderData.getActualAmount() : 0.0;
                amountText.setText("￥" + df.format(amount));
            }
        } catch (Exception e) {
            Log.error("绑定数据失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 更新背景
     */
    private void updateBackground(boolean selected) {
        if (rootLayout == null) {
            return;
        }
        try {
            int backgroundResId = selected 
                ? ResourceTable.Graphic_background_circle_8_top_selected
                : ResourceTable.Graphic_background_circle_8_top;
            Element element = ElementScatter.getInstance(getContext()).parse(backgroundResId);
            rootLayout.setBackground(element);
        } catch (Exception e) {
            Log.error("更新背景失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 设置选中状态
     */
    public void setSelected(boolean selected) {
        if (isSelected == selected) {
            return;
        }
        isSelected = selected;
        updateBackground(selected);
    }

    /**
     * 获取选中状态
     */
    public boolean isSelected() {
        return isSelected;
    }

    /**
     * 设置选中监听器
     */
    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.onItemSelectedListener = listener;
    }

    /**
     * 获取订单数据
     */
    public OrderRes getOrderData() {
        return orderData;
    }
}

