import axios from "axios";

const vite_url = import.meta.env.VITE_URL;

export const apiAxios = axios.create({
    baseURL: `${vite_url}/api`
});
apiAxios.defaults.headers.post['Accept'] = 'application/json; charset=UTF-8';

export function isInClub(userinfo, clubId) {
    return userinfo?.groups.filter((g => g.startsWith("/club/"))).map(g => g.split("/")[2]).map(g => Number(g.split("-")[0])).includes(clubId)
}

export function isClubAdmin(userinfo) {
    return userinfo?.roles?.includes("club_president") || userinfo?.roles?.includes("club_respo_intra")
        || userinfo?.roles?.includes("club_secretaire") || userinfo?.roles?.includes("club_tresorier");
}


export const errFormater = (data, msg) => {
    if (typeof data.response.data === 'string' || data.response.data instanceof String)
        return `${msg} (${data.response.statusText}: ${data.response.data}) 😕`
    return `${msg} (${data.response.statusText}: ${JSON.stringify(data.response.data)}) 😕`
}

export const CatList = [
    "SUPER_MINI",
    "MINI_POUSSIN",
    "POUSSIN",
    "BENJAMIN",
    "MINIME",
    "CADET",
    "JUNIOR",
    "SENIOR1",
    "SENIOR2",
    "VETERAN1",
    "VETERAN2"
];

export function getCategoryFormBirthDate(birth_date, currentDate = new Date()) {
    const currentSaison = getSaison(currentDate)
    const birthYear = birth_date.getFullYear()

    const diff = currentSaison - birthYear;
    if (diff < 6) {
        return "SUPER_MINI";
    } else if (diff < 8) {
        return "MINI_POUSSIN";
    } else if (diff < 10) {
        return "POUSSIN";
    } else if (diff < 12) {
        return "BENJAMIN";
    } else if (diff < 14) {
        return "MINIME";
    } else if (diff < 16) {
        return "CADET";
    } else if (diff < 18) {
        return "JUNIOR";
    } else if (diff < 25) {
        return "SENIOR1";
    } else if (diff < 35) {
        return "SENIOR2";
    } else if (diff < 45) {
        return "VETERAN1";
    } else {
        return "VETERAN2";
    }
}

export function getSaison(currentDate = new Date()) {
    if (currentDate.getMonth() >= 8) { //septembre et plus
        return currentDate.getFullYear()
    } else {
        return currentDate.getFullYear() - 1
    }
}

export function getCatName(cat) {
    switch (cat) {
        case "SUPER_MINI":
            return "Super Mini";
        case "MINI_POUSSIN":
            return "Mini Poussin";
        case "POUSSIN":
            return "Poussin";
        case "BENJAMIN":
            return "Benjamin";
        case "MINIME":
            return "Minime";
        case "CADET":
            return "Cadet";
        case "JUNIOR":
            return "Junior";
        case "SENIOR1":
            return "Senior 1";
        case "SENIOR2":
            return "Senior 2";
        case "VETERAN1":
            return "Vétéran 1";
        case "VETERAN2":
            return "Vétéran 2";
        case null:
            return "Catégorie inconnue";
        default:
            return cat;
    }
}

export function win(scores) {
    let sum = 0
    for (const score of scores) {
        if (score.s1 === -1000 || score.s2 === -1000) continue
        if (score.s1 > score.s2) sum++
        else if (score.s1 < score.s2) sum--
    }
    return sum
}

export function scorePrint(s1) {
    switch (s1) {
        case -997:
            return "disc."
        case -998:
            return "abs."
        case -999:
            return "for."
        case -1000:
            return ""
        default:
            return String(s1)
    }
}

export function timePrint(time, negSign = false) {
    if (time === null || time === undefined)
        return ""
    const neg = time < 0
    if (neg){
        if (!negSign)
            return "00:00"
        time = -time
    }

    const ms = time % 1000
    time = (time - ms) / 1000
    const sec = time % 60
    time = (time - sec) / 60
    const min = time % 60
    const hr = (time - min) / 60

    return (neg ? "-" : "") +
        (hr > 0 ? String(hr).padStart(2, '0') + ":" : "") +
        String(min).padStart(2, '0') + ":" +
        String(sec).padStart(2, '0')
}
