const fetch = require("node-fetch")
const moment = require("moment")

function getsrcinfo(userinput, callback) {
    var input = userinput
    var rank;
    var modfor = "None"
    var hasCountry = true
    var rankemote = "";
    input = input.toLowerCase()
    var finalrank
    var flag = ""
    var rankarray = []
    fetch("https://www.speedrun.com/api/v1/users?lookup=" + input).then(res => res.json())
        .then(res => {


            try {

                var userid = res.data[0].id
                var weblink = res.data[0].weblink
                var acccreation = moment.utc(res.data[0].signup).format('dddd, MMMM do YYYY')
                var username = res.data[0].names.international
                var usercountry = "No Country Specified"
                var imgurl = 'https://www.speedrun.com/themes/user/' + username + '/image.png'
                var imgoptions = {
                    uri: imgurl,
                    resolveWithFullResponse: true,
                    simple: false
                };
                if (res.data[0].location != null) {

                    usercountry = res.data[0].location.country.names.international
                    if (Object.keys(res.data[0].location).includes("region")) {
                        usercountry = res.data[0].location.region.names.international
                    }
                    var usercountrycode = res.data[0].location.country.code
                    if(usercountrycode.includes("/")){
                      if(usercountrycode == "ca/qc"){
                        flag = "<:flag_qc:756644945930092544> "
                      } else 
                      {usercountrycode = usercountrycode.split("/")[0]}
                    }
                    else{
                    flag = ":flag_" + usercountrycode + ": "
                    }
                }
                var usertwitch = res.data[0].twitch
                var useryt = res.data[0].youtube
                var usertwitter = res.data[0].twitter
                var links = []
                if (useryt != null) {
                    useryt = "<:yt:755816076574720081> " + res.data[0].youtube.uri
                    links[links.length] = useryt
                }
                if (usertwitch != null) {
                    usertwitch = "<:twitch:755815718527696967> " + res.data[0].twitch.uri
                    links[links.length] = usertwitch
                }
                if (usertwitter != null) {
                    usertwitter = "<:twitter:755816592176316528> " + res.data[0].twitter.uri
                    links[links.length] = usertwitter
                }
                if (links.length == 0) {
                    links[links.length] = "No Links"
                }
                fetch("https://www.speedrun.com/api/v1/runs?user=" + userid + "&max=1000")

                    .then(runres => runres.json())
                    .then(runres => {
                        var runs = runres.data.length
                        if (runs == 200) {
                            runs = "200+"
                        }

                        fetch("https://www.speedrun.com/api/v1/users/" + userid + "/personal-bests?max=200")

                            .then(pbres => pbres.json())
                            .then(pbres => {


                                var pbs = pbres.data.length
                                if (pbs == 200) {
                                    pbs = "200+"
                                }


                                fetch(res.data[0].links[2].uri)

                                    .then(a => a.json())
                                    .then(res => {


                                        if (res.data.length != 0) {
                                            modfor = res.data.map(x => x.names).map(x => x.international).join("\n")

                                            var gamemods = res.data.map(x => x.moderators)
                                            var abbrs = res.data.map(x => x.abbreviation)


                                            for (i = 0; i < abbrs.length; i++) {

                                                if (Object.keys(gamemods[i]).includes(userid)) {
                                                    rank = gamemods[i][userid]


                                                    fetch("https://www.speedrun.com/" + abbrs[i])
                                                        .then(res => res.text()).then(res =>{

                                                            var a = res.split("Moderated by:")[1]
                                                            a = a.split("</small>")[0]
                                                            a = a.split(/<a/g)
                                                            for (i = 0; i < a.length; i++) {
                                                                if (a[i].toLowerCase().includes(input)) {

                                                                    if (a[i].includes("Verifier")) {
                                                                        rank = ("verifier")


                                                                    } else if (a[i].includes("supermod")) {
                                                                        rank = "super-moderator"
                                                                    } else if (a[i].includes("mod")) {
                                                                        rank = "moderator"
                                                                    }
                                                                }

                                                            }
                                                            rankarray[rankarray.length] = rank
                                                            if (rankarray.length == gamemods.length) {
                                                                if (rankarray.includes("super-moderator")) {
                                                                    finalrank = "super-moderator"
                                                                    rankemote = "<:supermod:755829397616984096> "
                                                                } else if (rankarray.includes("moderator")) {
                                                                    finalrank = "moderator"
                                                                    rankemote = " <:mod:755829397969174528> "
                                                                } else {
                                                                    finalrank = "verifier"
                                                                    rankemote = "<:verifier:755816781423181944> "
                                                                }

                                                                callback(null, username, rankemote, flag, imgurl, weblink, usercountry, acccreation, modfor, pbs, runs, links)




                                                            }



                                                        })
                                                }


                                            }


                                        } else {

                                            callback(null, username, rankemote, flag, imgurl, weblink, usercountry, acccreation, modfor, pbs, runs, links)

                                        }

                                    })
                            })
                    })
            } catch (err) {
                callback("Invalid Username")
            }
        })
}
module.exports = {
    getsrcinfo
}