/*
 *   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.data

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.cbor.Cbor
import kotlinx.serialization.decodeFromByteArray
import kotlinx.serialization.encodeToByteArray

@Serializable
data class GroupsData(val groups: MutableList<GroupData>)

@Serializable
data class GroupData(val name: String, val fights: MutableList<Int>)

@OptIn(ExperimentalSerializationApi::class)
class Groups {
    companion object {
        private var groups: GroupsData = if (kGroupsFile.exists()) {
            Cbor.decodeFromByteArray(kGroupsFile.readBytes())
        } else {
            if (!kGroupsFile.parentFile.exists()) {
                kGroupsFile.parentFile.mkdirs()
            }
            kGroupsFile.createNewFile()
            kGroupsFile.writeBytes(Cbor.encodeToByteArray(GroupsData(mutableListOf())))

            GroupsData(mutableListOf())
        }

        fun getGroup(name: String): GroupData? {
            return groups.groups.find { it.name == name }
        }

        fun getGroup(fight: Int): GroupData? {
            return groups.groups.find { it.fights.contains(fight) }
        }

        fun getOrCreateGroup(name: String): GroupData {
            val group = getGroup(name)
            if (group != null) {
                return group
            }
            val newGroup = GroupData(name, mutableListOf())
            groups.groups.add(newGroup)
            return newGroup
        }

        fun resetGroup(fight: Int, save: Boolean = false) {
            val oldGroup = getGroup(fight)
            oldGroup?.fights?.remove(fight)
            if(oldGroup?.fights?.isEmpty() == true) {
                groups.groups.remove(oldGroup)
            }
            if(save) {
                kGroupsFile.writeBytes(Cbor.encodeToByteArray(groups))
            }
        }

        fun setGroup(fight: Int, group: String) {
            resetGroup(fight)
            val newGroup = getOrCreateGroup(group)
            newGroup.fights.add(fight)
            kGroupsFile.writeBytes(Cbor.encodeToByteArray(groups))
        }

        fun getAllGroups(): List<String> {
            return groups.groups.map { it.name }
        }
    }
}