/*
  StreamGoose internal badge provider — Go reference implementation
  
  Created by Luxferre in 2024, released into public domain
*/

package main

import (
  "net/http"
)

/* platform-specific badge provider implementations */

func badge_provider_impls() map[string]interface{} {
  return map[string]interface{} {
    "tw": func(badgename string) string { /* Twitch implementation */
      if _, ok := TW_BADGE_CACHE[badgename]; ok { /* found */
        return TW_BADGE_CACHE[badgename]
      } else { /* not found */
        return ""
      }
    },
    /* "kk": func(badgename string) {}, */
  }
}

/* serve the GET /badge/{platform}/{badgename} request */
/* returns a 301-type redirect to the resolved URL or 404 if unresolved */
func bp_handle_request(w http.ResponseWriter, r *http.Request) {
  platform := r.PathValue("platform")
  badgename := r.PathValue("badgename")
  impls := badge_provider_impls()
  if _, ok := impls[platform]; ok { /* process the badge and return 301 */
    badgeurl := impls[platform].(func(string) string)(badgename)
    if len(badgeurl) > 0 { /* return 301 redirect */
      http.Redirect(w, r, badgeurl, http.StatusMovedPermanently)
    } else { /* return 404 */
      http.Error(w, "404 - Badge not found", http.StatusNotFound)
      w.Write([]byte("404 - Badge not found"))
    }
  } else { /* return 404 */
    http.Error(w, "404 - Badge provider not found", http.StatusNotFound)
  }
}
