/*
 * Copyright (c) 2021 PSMForums. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.joshuacerdenia.android.nicefeed.util

import java.util.*

object RssUrlTransformer {

    private const val BASE_NY_TIMES = "www.nytimes.com"
    private const val ATOM_XML = "atom.xml"

    fun getUrl(link: String): String {
        val url = link.removePrefix("feed/").removeSuffix("/")
            // This is possibly not ideal, but it results in too many failed requests otherwise:
            .replace("http://", "https://")
            .toLowerCase(Locale.ROOT)

        return when {
            url.contains(BASE_NY_TIMES) -> url.replace(BASE_NY_TIMES, "rss.nytimes.com")
            url.contains("feedproxy.google.com") -> "" // We don't want this
            url.contains("youtube.com") -> url
            url.contains("medium.com") -> url
            url.endsWith("blogspot.com") -> "$url/feeds/posts/default?alt=rss"
            url.endsWith("/feeds/posts/default") -> "$url?alt=rss"
            url.endsWith("wordpress.com") -> "$url/feed"
            url.endsWith("tumblr.com") -> "$url/rss"
            url.contains(ATOM_XML) -> url.replace(ATOM_XML, "rss.xml")
            url.endsWith(".xml") -> url
            url.endsWith("/feed") -> url
            url.endsWith("/rss") -> url
            else -> url
        }
    }
}