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.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)
public class DriverNameTranslationImpl 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(",");
            if (split.length == 1) {
                HotDriver entity = mapper.selectById(trimmed);
                return entity != null ? entity.getName() : null;
            }
            List<String> names = 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()) {
                    names.add(entity.getName());
                }
            }
            if (names.isEmpty()) {
                return null;
            }
            return String.join(",", names);
        } else if (key instanceof Long id) {
            HotDriver entity = mapper.selectById(String.valueOf(id));
            return entity != null ? entity.getName() : null;
        }
        return null;
    }
}
