import axios, {AxiosHeaders} from "axios";
import Router from "../router";


export default {
    GetDataByPath: GetDataByPath,
    PostDataByPath: PostDataByPath,
    PutDataByPath: PutDataByPath,
    DeleteDataByPath: DeleteDataByPath,
}

function getToken(): string {
    let token = localStorage.getItem("token");
    if (!token) {
        Router.push({path: "/"})
        return ""
    }
    return token ?? "";
}

const HeaderTokenName = "auth-sign"
const BaseApiPathPrefix = "/api/"
const JsonGetTypeKey = "Accept"
const JsonPostTypeKey = "Content-Type"

export class ResponseData<T> {
    data: T;
    code: number;
    message: string;

    constructor(data: T, code: number, message: string) {
        this.data = data;
        this.code = code;
        this.message = message;
    }
}

export async function GetDataByPath(path: string, params?: Object, calibration = false): Promise<ResponseData<any>> {
    let headers = new AxiosHeaders();
    headers.set(JsonGetTypeKey, 'application/json',)
    if (calibration) {
        let token = getToken()
        if (token === null || token === "") {
            throw "账号未登录"
        }
        headers.set(HeaderTokenName, getToken())
    }
    let data = {}
    await axios.get<ResponseData<any>>(BaseApiPathPrefix + path, {
        headers: headers,
        params: params
    }).then(
        res => {
            if (res.data.code === 565) {
                localStorage.removeItem("token")
            }
            data = res.data
        }
    )
    return data as ResponseData<any>
}

export async function PostDataByPath(path: string, json?: Object, calibration = false): Promise<ResponseData<any>> {
    let headers = new AxiosHeaders();
    headers.set(JsonPostTypeKey, 'application/json',)
    if (calibration) {
        let token = getToken()
        if (token === null || token === "") {
            throw "账号未登录"
        }
        headers.set(HeaderTokenName, getToken())
    }

    let data = {}
    await axios.post<ResponseData<any>>(BaseApiPathPrefix + path, json, {
        headers: headers
    }).then(
        res => {
            if (res.data.code === 565) {
                localStorage.removeItem("token")
            }
            data = res.data
        }
    )
    return data as ResponseData<any>
}

export async function PutDataByPath(path: string, json?: Object, calibration = false): Promise<ResponseData<any>> {
    let headers = new AxiosHeaders();
    headers.set(JsonPostTypeKey, 'application/json',)
    if (calibration) {
        let token = getToken()
        if (token === null || token === "") {
            throw "账号未登录"
        }
        headers.set(HeaderTokenName, getToken())
    }

    let data = {}
    await axios.put<ResponseData<any>>(BaseApiPathPrefix + path, json, {
        headers: headers
    }).then(
        res => {
            if (res.data.code === 565) {
                localStorage.removeItem("token")
            }
            data = res.data
        }
    )
    return data as ResponseData<any>
}


export async function DeleteDataByPath(path: string, params?: Object, calibration = false): Promise<ResponseData<any>> {
    let headers = new AxiosHeaders();
    headers.set(JsonGetTypeKey, 'application/json',)
    if (calibration) {
        let token = getToken()
        if (token === null || token === "") {
            throw "账号未登录"
        }
        headers.set(HeaderTokenName, getToken())
    }
    let data = {}
    await axios.delete<ResponseData<any>>(BaseApiPathPrefix + path, {
        headers: headers,
        params: params
    }).then(
        res => {
            if (res.data.code === 565) {
                localStorage.removeItem("token")
            }
            data = res.data
        }
    )
    return data as ResponseData<any>
}
