/** @format */

import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
import { message } from "antd";
import { getStorageFromKey } from "@/utils/localStorage/config.ts";

class Request {
	private instance: AxiosInstance | undefined;

	constructor(config: AxiosRequestConfig) {
		this.instance = axios.create(config);
		// 全局请求拦截
		this.instance.interceptors.request.use(
			(config) => {
				const token: string | null = getStorageFromKey("token");
				if (token) {
					config.headers.Authorization = `${import.meta.env.VITE_APP_TOKEN_KEY} ${token}`;
				}

				// if (config.method == "post") {
				// 	config.data = EncryptData(JSON.stringify(config.data));
				// }
				return config;
			},
			(error) => {
				return Promise.reject(error);
			},
		);

		// 全局响应拦截
		this.instance.interceptors.response.use(
			(response) => {
				// 后端返回字符串表示需要解密操作
				// if (typeof response.data == "string") {
				// 	response.data = DecryptData(response.data);
				// 	if (response.status !== 200) {
				// 		message.error(response.statusText).then();
				// 		return Promise.reject(response.data);
				// 	}
				// }
				return response.data;
			},
			(error) => {
				const { response } = error;
				if (response) {
					this.handleCode(response.status);
				}
				if (!window.navigator.onLine) {
					message.error("网络连接失败");
					// return router.push({
					//     path: '/404',
					// })
					return Promise.reject(error);
				}
			},
		);
	}

	handleCode(code: number): void {
		switch (code) {
			case 400:
				message
					.open({
						content: "请求错误(400)",
						type: "error",
					})
					.then();
				break;
			case 401:
				message
					.open({
						content: "未授权，请重新登录(401)",
						type: "error",
					})
					.then();
				break;
			case 403:
				message
					.open({
						content: "拒绝访问(403)",
						type: "error",
					})
					.then();
				break;
			case 404:
				message
					.open({
						content: "请求出错(404)",
						type: "error",
					})
					.then();
				break;
			case 408:
				message
					.open({
						content: "请求超时(408)",
						type: "error",
					})
					.then();
				break;
			case 500:
				message
					.open({
						content: "服务器错误(500)",
						type: "error",
					})
					.then();
				break;
			case 501:
				message
					.open({
						content: "服务未实现(501)",
						type: "error",
					})
					.then();
				break;
			case 502:
				message
					.open({
						content: "网络错误(502)",
						type: "error",
					})
					.then();
				break;
			case 503:
				message
					.open({
						content: "服务不可用(503)",
						type: "error",
					})
					.then();
				break;
			case 504:
				message
					.open({
						content: "网络超时(504)",
						type: "error",
					})
					.then();
				break;
			case 505:
				message
					.open({
						content: "HTTP版本不受支持(505)",
						type: "error",
					})
					.then();
				break;
			default:
				message
					.open({
						content: `连接出错(${code})!`,
						type: "error",
					})
					.then();
				break;
		}
	}

	request<T>(config: AxiosRequestConfig<T>): Promise<T> {
		return new Promise<T>((resolve, reject) => {
			this.instance
				?.request<any, T>(config)
				.then((res) => {
					resolve(res);
				})
				.catch((err) => {
					reject(err);
				});
		});
	}
}

export default Request;
