/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem;

import de.steamwar.bausystem.tracer.show.ShowModeParameterType;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper;
import de.steamwar.sql.BauweltMember;
import de.steamwar.sql.SteamwarUser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


public class Mapper {

    private Mapper() {
        throw new IllegalStateException("Utility Class");
    }

    public static void init() {
        SWCommandUtils.addMapper(ShowModeParameterType.class, showModeParameterTypesTypeMapper());
        SWCommandUtils.addMapper(BauweltMember.class, bauweltMemberTypeMapper());
    }

    private static TypeMapper<ShowModeParameterType> showModeParameterTypesTypeMapper() {
        Map<String, ShowModeParameterType> showModeParameterTypesMap = new HashMap<>();
        showModeParameterTypesMap.put("-water", ShowModeParameterType.WATER);

        showModeParameterTypesMap.put("-interpolatey", ShowModeParameterType.INTERPOLATE_Y);
        showModeParameterTypesMap.put("-interpolate-y", ShowModeParameterType.INTERPOLATE_Y);
        showModeParameterTypesMap.put("-interpolate_y", ShowModeParameterType.INTERPOLATE_Y);
        showModeParameterTypesMap.put("-y", ShowModeParameterType.INTERPOLATE_Y);

        showModeParameterTypesMap.put("-interpolatex", ShowModeParameterType.INTERPOLATE_XZ);
        showModeParameterTypesMap.put("-interpolate-x", ShowModeParameterType.INTERPOLATE_XZ);
        showModeParameterTypesMap.put("-interpolate_x", ShowModeParameterType.INTERPOLATE_XZ);
        showModeParameterTypesMap.put("-x", ShowModeParameterType.INTERPOLATE_XZ);

        showModeParameterTypesMap.put("-interpolatez", ShowModeParameterType.INTERPOLATE_XZ);
        showModeParameterTypesMap.put("-interpolate-z", ShowModeParameterType.INTERPOLATE_XZ);
        showModeParameterTypesMap.put("-interpolate_z", ShowModeParameterType.INTERPOLATE_XZ);
        showModeParameterTypesMap.put("-z", ShowModeParameterType.INTERPOLATE_XZ);

        showModeParameterTypesMap.put("-interpolatexz", ShowModeParameterType.INTERPOLATE_XZ);
        showModeParameterTypesMap.put("-interpolate-xz", ShowModeParameterType.INTERPOLATE_XZ);
        showModeParameterTypesMap.put("-interpolate_xz", ShowModeParameterType.INTERPOLATE_XZ);
        showModeParameterTypesMap.put("-xz", ShowModeParameterType.INTERPOLATE_XZ);

        showModeParameterTypesMap.put("-advanced", ShowModeParameterType.ADVANCED);
        showModeParameterTypesMap.put("-a", ShowModeParameterType.ADVANCED);
        showModeParameterTypesMap.put("advanced", ShowModeParameterType.ADVANCED);
        showModeParameterTypesMap.put("a", ShowModeParameterType.ADVANCED);

        List<String> tabCompletes = new ArrayList<>(showModeParameterTypesMap.keySet());
        return SWCommandUtils.createMapper(s -> showModeParameterTypesMap.getOrDefault(s, null), s -> tabCompletes);
    }

    private static TypeMapper<BauweltMember> bauweltMemberTypeMapper() {
        return SWCommandUtils.createMapper(s -> BauweltMember.getMembers(BauSystem.getOwnerID())
                                                             .stream()
                                                             .filter(m -> SteamwarUser.get(m.getMemberID()).getUserName().equals(s)).findFirst().orElse(null),
                                           s -> BauweltMember.getMembers(BauSystem.getOwnerID())
                                                             .stream()
                                                             .map(m -> SteamwarUser.get(m.getMemberID()).getUserName())
                                                             .collect(Collectors.toList()));
    }
}
