package com.fujica.abk.component;

import com.fujica.abk.ResourceTable;
import com.fujica.abk.model.out.ParkNearRes;
import com.fujica.abk.utils.Log;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
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_park_name);
            if (nameText != null && parkData.getParkName() != null) {
                nameText.setText(parkData.getParkName());
            }

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

            // 设置距离
            Text distanceText = (Text) rootLayout.findComponentById(ResourceTable.Id_parking_address_1);
            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_text_total_place);
            if (totalPlaceText != null && parkData.getTotalPlace() != null) {
                totalPlaceText.setText(String.valueOf(parkData.getTotalPlace()));
            }

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

            // 设置免费时长
            Text freeTimeText = (Text) rootLayout.findComponentById(ResourceTable.Id_text_free_duration);
            if (freeTimeText != null) {
                freeTimeText.setText(parkData.getFreeTime() != null ? parkData.getFreeTime() + "分" : "--");
            }

            // 设置服务标签
            addServiceTags();

            // 设置收费信息
            Text chargeInfoText = (Text) rootLayout.findComponentById(ResourceTable.Id_charge_info);
            if (chargeInfoText != null) {
                String chargeInfo = parkData.getChargeInfo();
                chargeInfoText.setText(chargeInfo != null && !chargeInfo.isEmpty() ? chargeInfo : "暂无信息");
            }

            // 设置导航按钮点击事件
            Button btnNavigate = (Button) rootLayout.findComponentById(ResourceTable.Id_btn_navigate);
            if (btnNavigate != null) {
                btnNavigate.setClickedListener(component -> {
                    // TODO: 实现导航功能
                    Log.info("导航到停车场: " + parkData.getParkName());
                });
            }
        } catch (Exception e) {
            Log.error("绑定停车场详情数据失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 添加服务标签
     */
    private void addServiceTags() {
        DirectionalLayout tagsContainer = (DirectionalLayout) rootLayout.findComponentById(ResourceTable.Id_service_tags_container);
        if (tagsContainer == null) {
            return;
        }

        // 清空容器
        tagsContainer.removeAllComponents();

        // 在线支付
        if (parkData.getOnlinePayEnable() != null && parkData.getOnlinePayEnable() == 1) {
            tagsContainer.addComponent(createTag("在线支付"));
        }

        // 电子发票
//        if (parkData.getInvoiceEnable() != null && parkData.getInvoiceEnable() == 1) {
//            tagsContainer.addComponent(createTag("电子发票"));
//        }

        // 电子优惠券
        if (parkData.getReductionEnable() != null && parkData.getReductionEnable() == 1) {
            tagsContainer.addComponent(createTag("电子优惠券"));
        }

        // 去找车
//        if (parkData.getIsfindcar() != null && parkData.getIsfindcar() == 1) {
//            tagsContainer.addComponent(createTag("去找车"));
//        }

        // 预约车位
//        if (parkData.getReservationEnable() != null && parkData.getReservationEnable() == 1) {
//            tagsContainer.addComponent(createTag("预约车位"));
//        }

        // 如果没有标签，隐藏容器
        if (tagsContainer.getChildCount() == 0) {
            tagsContainer.setVisibility(Component.HIDE);
        } else {
            tagsContainer.setVisibility(Component.VISIBLE);
        }
    }

    /**
     * 创建标签组件
     */
    private Text createTag(String text) {
        Text tag = new Text(rootLayout.getContext());
        tag.setText(text);
        tag.setTextSize(11, Text.TextSizeType.FP);
        tag.setTextColor(new Color(0xFFFA6332));
        
        // 设置边框和圆角
        ShapeElement background = new ShapeElement();
        background.setShape(ShapeElement.RECTANGLE);
        background.setRgbColor(new RgbColor(255, 255, 255));
        background.setCornerRadius(3);
        background.setStroke(1, new RgbColor(250, 99, 50)); // #FA6332
        tag.setBackground(background);
        
        // 设置内边距
        tag.setPadding(8, 4, 8, 4);
        
        // 设置布局参数
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT
        );
        layoutConfig.setMarginRight(8);
        tag.setLayoutConfig(layoutConfig);
        
        return tag;
    }
}

