package org.springblade.datasource.transform.util;

import cn.hutool.core.map.CaseInsensitiveMap;
import org.springblade.datasource.transform.ColumnInfo;
import org.springblade.datasource.transform.general.GeneralModel;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

public class ListUtils {


    public static boolean equalLists(List<String> one, List<String> two) {
        if (one == null && two == null) {
            return true;
        }

        if ((one == null && two != null)
                || one != null && two == null
                || one.size() != two.size()) {
            return false;
        }

        //to avoid messing the order of the lists we will use a copy
        //as noted in comments by A. R. S.
        one = one.stream().map(v -> v.toUpperCase(Locale.ROOT)).collect(Collectors.toList());
        two = two.stream().map(v -> v.toUpperCase(Locale.ROOT)).collect(Collectors.toList());
//        two = new ArrayList<String>(two);

        Collections.sort(one);
        Collections.sort(two);
        return one.equals(two);
    }

    public static CaseInsensitiveMap<String, ColumnInfo> ColumnInfoListToMap(List<ColumnInfo> list) {
        Map<String, ColumnInfo> map = list.stream().collect(Collectors.toMap(ColumnInfo::getColumnName, v -> v, (a, b) -> a));
        CaseInsensitiveMap<String, ColumnInfo> caseInsensitiveMap = new CaseInsensitiveMap<>();
        caseInsensitiveMap.putAll(map);
        return caseInsensitiveMap;
    }

    public static CaseInsensitiveMap<String, ColumnInfo> GeneralModelListToMap(List<GeneralModel> list) {
        List<ColumnInfo> collect = list.stream().map(v -> {
            ColumnInfo columnInfo = new ColumnInfo();
            columnInfo.setColumnSize(v.getColumnSize());
            columnInfo.setDecimalDigits(v.getDecimalDigits());
            columnInfo.setColumnName(v.getColumnName());
            return columnInfo;
        }).collect(Collectors.toList());
        return ColumnInfoListToMap(collect);
    }
}
