/**
 * qsfera Android client application
 *
 * @author Abel García de Prada
 * @author Juan Carlos Garrote Gascón
 *
 * Copyright (C) 2023 ownCloud GmbH.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package eu.qsfera.android.domain.server.model

import eu.qsfera.android.domain.authentication.oauth.model.OIDCServerConfiguration

sealed class ServerInfo(
    val qsferaVersion: String,
    var baseUrl: String,
) {
    val isSecureConnection get() = baseUrl.startsWith(HTTPS_PREFIX, ignoreCase = true)

    override fun equals(other: Any?): Boolean {
        if (other !is ServerInfo) return false
        if (qsferaVersion != other.qsferaVersion) return false
        if (baseUrl != other.baseUrl) return false
        return true
    }

    override fun hashCode(): Int =
        javaClass.hashCode()

    class OIDCServer(
        qsferaVersion: String,
        baseUrl: String,
        val oidcServerConfiguration: OIDCServerConfiguration,
        val webFingerClientId: String? = null,
        val webFingerScopes: List<String>? = null,
    ) : ServerInfo(qsferaVersion = qsferaVersion, baseUrl = baseUrl) {
        override fun equals(other: Any?): Boolean {
            if (other !is OIDCServer) return false
            if (oidcServerConfiguration != other.oidcServerConfiguration) return false
            return super.equals(other)
        }

        override fun hashCode(): Int =
            javaClass.hashCode()
    }

    class OAuth2Server(
        qsferaVersion: String,
        baseUrl: String,
    ) : ServerInfo(qsferaVersion = qsferaVersion, baseUrl = baseUrl)

    class BasicServer(
        qsferaVersion: String,
        baseUrl: String,
    ) : ServerInfo(qsferaVersion = qsferaVersion, baseUrl = baseUrl)

    companion object {
        const val HTTP_PREFIX = "http://"
        const val HTTPS_PREFIX = "https://"
    }
}
