/*
 *   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.velocitycore.util;

import de.steamwar.messages.Chatter;
import de.steamwar.sql.BauweltMember;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.sql.UserConfig;
import de.steamwar.sql.UserPerm;
import lombok.experimental.UtilityClass;

@UtilityClass
public class BauLock {

    private static final String BAU_LOCK_CONFIG_NAME = "baulockstate";
    public static void setLocked(Chatter owner, BauLockState state) {
        UserConfig.updatePlayerConfig(owner.user().getId(), BAU_LOCK_CONFIG_NAME, state == BauLockState.OPEN ? null : state.name());
        owner.system("BAU_LOCKED_" + state.name());
    }

    public static boolean isLocked(SteamwarUser owner, SteamwarUser target) {
        if (owner.getId() == target.getId())
            return false;

        boolean locked;
        String state = UserConfig.getConfig(owner.getId(), BAU_LOCK_CONFIG_NAME);
        switch (state == null ? BauLockState.OPEN : BauLockState.valueOf(state)) {
            case NOBODY:
                locked = true;
                break;
            case SUPERVISOR:
                BauweltMember member = BauweltMember.getBauMember(owner.getId(), target.getId());
                locked = !member.isSupervisor();
                break;
            case SERVERTEAM:
                locked = !target.hasPerm(UserPerm.TEAM);
                break;
            case TEAM_AND_SERVERTEAM:
                if (target.hasPerm(UserPerm.TEAM)) {
                    return false;
                }
                locked = owner.getTeam() != target.getTeam();
                break;
            case TEAM:
                locked = owner.getTeam() != target.getTeam();
                break;
            case OPEN:
            default:
                locked = false;
        }

        return locked;
    }
}
