package com.trade.common.core.dto;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.annotation.TableField;

import cn.hutool.core.collection.CollUtil;

/**
 * I18n基类
 */
public abstract class BaseI18nDto extends BaseDTO implements Serializable {

    /**
     * { "userName": [ { "language": "cn", "value": "张三" }, { "language": "en", "value": "Tom Zhang" } ] }
     */
    @TableField(exist = false)
    private Map<String, List<Map<String, String>>> i18n;

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (i18n != null ? i18n.hashCode() : 0);
        return result;
    }

    public Map<String, List<Map<String, String>>> getI18n() {
        return i18n;
    }

    public void setI18n(Map<String, List<Map<String, String>>> i18n) {
        this.i18n = i18n;
    }

    /**
     * 获取字段，指定语言的值
     * 
     * @param value
     * @param language
     * @return
     */
    public String getI18nValue(String value, String language) {
        if (CollUtil.isEmpty(i18n)) {
            return null;
        }
        List<Map<String, String>> values = i18n.get(value);
        if (CollUtil.isEmpty(values)) {
            return null;
        }
        return values.stream().filter(v -> v.get("language").equals(language)).findFirst().map(v -> v.get("value"))
                .orElse(null);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        if (!super.equals(o))
            return false;
        BaseI18nDto that = (BaseI18nDto) o;
        return Objects.equals(this.i18n, that.i18n);
    }
}
