/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2024 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.*;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@AllArgsConstructor
public class Mod {

    static {
        SqlTypeMapper.ordinalEnumMapper(Platform.class);
        SqlTypeMapper.ordinalEnumMapper(ModType.class);
    }

    private static final Table<Mod> table = new Table<>(Mod.class, "Mods");
    private static final SelectStatement<Mod> get = table.select(Table.PRIMARY);
    private static final SelectStatement<Mod> findFirst = new SelectStatement<>(table, "SELECT * FROM Mods WHERE ModType = 0 LIMIT 1");
    private static final SelectStatement<Mod> getPageOfType = new SelectStatement<>(table, "SELECT * FROM Mods WHERE ModType = ? ORDER BY ModName DESC LIMIT ?, ?");
    private static final Statement insert = table.insert(Table.PRIMARY);
    private static final Statement set = table.update(Table.PRIMARY, "ModType");

    public static Mod get(String name, Platform platform) {
        return get.select(platform, name);
    }

    public static Mod getOrCreate(String name, Platform platform) {
        Mod mod = get(name, platform);
        if(mod != null)
            return mod;

        insert.update(platform, name);
        return new Mod(platform, name, ModType.UNKLASSIFIED);
    }

    public static List<Mod> getAllModsFiltered(int page, int elementsPerPage, ModType filter) {
        return Mod.getPageOfType.listSelect(filter, page * elementsPerPage, elementsPerPage);
    }

    public static Mod findFirstMod() {
        return findFirst.select();
    }

    @Getter
    @Field(keys = {Table.PRIMARY})
    private final Platform platform;
    @Getter
    @Field(keys = {Table.PRIMARY})
    private final String modName;
    @Getter
    @Field(def = "0")
    private ModType modType;

    public void setModType(ModType modType) {
        set.update(modType, platform, modName);
        this.modType = modType;
    }

    public enum Platform {
        FORGE,
        LABYMOD,
        FABRIC
    }

    public enum ModType {
        UNKLASSIFIED("7"),
        GREEN("a"),
        YELLOW("e"),
        RED("c"),
        YOUTUBER_ONLY("6");

        ModType(String colorcode) {
            this.colorcode = colorcode;
        }
        private final String colorcode;

        public String getColorCode() {
            return colorcode;
        }
    }
}
