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

import de.steamwar.sql.internal.Field;
import de.steamwar.sql.internal.SelectStatement;
import de.steamwar.sql.internal.Statement;
import de.steamwar.sql.internal.Table;
import lombok.Getter;

import java.util.*;

public class BauweltMember {
    private static final Map<Integer, BauweltMember> memberCache = new HashMap<>();

    public static void clear() {
        memberCache.clear();
    }

    private static final Table<BauweltMember> table = new Table<>(BauweltMember.class);
    private static final SelectStatement<BauweltMember> getMember = table.select(Table.PRIMARY);
    private static final SelectStatement<BauweltMember> getMembers = table.selectFields("BauweltID");
    private static final Statement update = table.insertAll();
    private static final Statement delete = table.delete(Table.PRIMARY);

    public static void addMember(UUID ownerID, UUID memberID) {
        new BauweltMember(SteamwarUser.get(ownerID).getId(), SteamwarUser.get(memberID).getId(), false, false).updateDB();
    }

    public static BauweltMember getBauMember(UUID ownerID, UUID memberID){
        return getBauMember(SteamwarUser.get(ownerID).getId(), SteamwarUser.get(memberID).getId());
    }

    public static BauweltMember getBauMember(int ownerID, int memberID){
        BauweltMember member = memberCache.get(memberID);
        if(member != null && member.bauweltID == ownerID)
            return member;
        return getMember.select(ownerID, memberID);
    }

    public static List<BauweltMember> getMembers(UUID bauweltID){
        return getMembers(SteamwarUser.get(bauweltID).getId());
    }

    public static List<BauweltMember> getMembers(int bauweltID){
        return getMembers.listSelect(bauweltID);
    }

    @Getter
    @Field(keys = {Table.PRIMARY})
    private final int bauweltID;
    @Getter
    @Field(keys = {Table.PRIMARY})
    private final int memberID;
    @Getter
    @Field(def = "0")
    private boolean worldEdit;
    @Getter
    @Field(def = "0")
    private boolean world;

    public BauweltMember(int bauweltID, int memberID, boolean worldEdit, boolean world) {
        this.bauweltID = bauweltID;
        this.memberID = memberID;
        this.worldEdit = worldEdit;
        this.world = world;
        memberCache.put(memberID, this);
    }

    public void setWorldEdit(boolean worldEdit) {
        this.worldEdit = worldEdit;
        updateDB();
    }

    public void setWorld(boolean world) {
        this.world = world;
        updateDB();
    }

    private void updateDB(){
        update.update(bauweltID, memberID, worldEdit, world);
    }

    public void remove(){
        delete.update(bauweltID, memberID);
        memberCache.remove(memberID);
    }

    public boolean isBuild() {
        return worldEdit;
    }

    public boolean isSupervisor() {
        return world;
    }

    public void setBuild(boolean build) {
        this.worldEdit = build;
        updateDB();
    }

    public void setSupervisor(boolean supervisor) {
        this.world = supervisor;
        updateDB();
    }
}
