package com.fujica.abk.component;

import com.fujica.abk.ResourceTable;
import com.fujica.abk.model.response.ParkNearRes;
import com.fujica.abk.utils.Log;
import ohos.agp.components.*;
import ohos.app.Context;

/**
 * 停车场详情组件
 */
public class ParkDetailComponent extends DirectionalLayout {
    private Component rootLayout;
    private ParkNearRes parkData;

    /**
     * 构造函数
     */
    public ParkDetailComponent(Context context) {
        super(context);
        initComponent(context);
    }

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

        // 加载布局文件
        rootLayout = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_park_detail, null, false);
        addComponent(rootLayout);
    }

    /**
     * 设置停车场数据并更新UI
     */
    public void setParkData(ParkNearRes park) {
        this.parkData = park;
        bindData();
    }

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

        try {
            // 设置停车场名称
            Text nameText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_park_name);
            if (nameText != null && parkData.getParkName() != null) {
                nameText.setText(parkData.getParkName());
            }

            // 设置地址
            Text addressText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_address);
            if (addressText != null) {
                addressText.setText(parkData.getAddress() != null ? parkData.getAddress() : "--");
            }

            // 设置距离
            Text distanceText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_distance);
            if (distanceText != null) {
                String distanceStr = parkData.getDistance() != null ?
                        String.format("%.1fkm", parkData.getDistance() / 1000.0) : "0km";
                distanceText.setText(distanceStr);
            }

            // 设置总车位
            Text totalPlaceText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_total_place);
            if (totalPlaceText != null && parkData.getTotalPlace() != null) {
                totalPlaceText.setText(String.valueOf(parkData.getTotalPlace()));
            }

            // 设置剩余车位
            Text residuePlaceText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_residue_place);
            if (residuePlaceText != null && parkData.getResiduePlace() != null) {
                residuePlaceText.setText(String.valueOf(parkData.getResiduePlace()));
            }

            // 设置免费时长
            Text freeTimeText = (Text) rootLayout.findComponentById(ResourceTable.Id_detail_free_time);
            if (freeTimeText != null) {
                freeTimeText.setText(parkData.getFreeTime() != null ? parkData.getFreeTime() + "分" : "--");
            }
        } catch (Exception e) {
            Log.error("绑定停车场详情数据失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

