package org.dromara.common.translation.core.impl;

import com.hotwj.platform.driverManagement.driver.domain.HotDriver;
import com.hotwj.platform.driverManagement.driver.mapper.HotDriverMapper;
import lombok.AllArgsConstructor;
import org.dromara.common.core.service.DictService;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.translation.annotation.TranslationType;
import org.dromara.common.translation.constant.TransConstant;
import org.dromara.common.translation.core.TranslationInterface;

import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@TranslationType(type = TransConstant.DRIVER_ID_TO_NAME_WITH_TYPE)
public class DriverNameWithTypeTranslationImpl implements TranslationInterface<String> {

    @Override
    public String translation(Object key, String other) {
        HotDriverMapper mapper = SpringUtils.getBean(HotDriverMapper.class);
        if (key instanceof String ids) {
            String trimmed = ids.trim();
            if (trimmed.isEmpty()) {
                return null;
            }
            String[] split = trimmed.split(",");
            List<String> result = new ArrayList<>();
            for (String part : split) {
                String id = part.trim();
                if (id.isEmpty()) {
                    continue;
                }
                HotDriver entity = mapper.selectById(id);
                if (entity == null || entity.getName() == null || entity.getName().isEmpty()) {
                    continue;
                }
                result.add(entity.getName() + "(" + mapPersonTypeLabel(entity.getPersonType()) + ")");
            }
            if (result.isEmpty()) {
                return null;
            }
            return String.join(",", result);
        } else if (key instanceof Long id) {
            HotDriver entity = mapper.selectById(String.valueOf(id));
            if (entity == null || entity.getName() == null || entity.getName().isEmpty()) {
                return null;
            }
            return entity.getName() + "(" + mapPersonTypeLabel(entity.getPersonType()) + ")";
        }
        return null;
    }

    private String mapPersonTypeLabel(String personType) {
        if (personType == null || personType.isEmpty()) {
            return "未知";
        }
        DictService dictService = SpringUtils.getBean(DictService.class);
        String dictLabel = dictService.getDictLabel("hot_person_type", personType);
        return (dictLabel == null || dictLabel.isEmpty()) ? personType : dictLabel;
    }
}
