import http from "@ohos:net.http";
import { DefaultRequestBody } from "@normalized:N&&&@yic/http/src/main/ets/requestbody/DefaultRequestBody&1.0.1";
import DownloadRequestBody from "@normalized:N&&&@yic/http/src/main/ets/requestbody/DownloadRequestBody&1.0.1";
import type { RequestBody } from '../requestbody/RequestBody';
import UploadRequestBody from "@normalized:N&&&@yic/http/src/main/ets/requestbody/UploadRequestBody&1.0.1";
import { HttpConfig } from "@normalized:N&&&@yic/http/src/main/ets/core/HttpConfig&1.0.1";
import type { Process } from './Model';
/**
 * @ProjectName : HarmonyUtilSdk
 * @FileName : Request
 * @Author : yic
 * @Time : 2024/6/21 9:33
 * @Description : Request调用类
 */
export class Request {
    static Post<T>(url: string, handler?: (requestBody: DefaultRequestBody) => void): Promise<T> {
        return this.defaultRequest<T>(url, http.RequestMethod.POST, handler);
    }
    static Get<T>(url: string, handler?: (requestBody: DefaultRequestBody) => void): Promise<T> {
        return this.defaultRequest<T>(url, http.RequestMethod.GET, handler);
    }
    static Head(url: string, handler?: (requestBody: DefaultRequestBody) => void): Promise<Object> {
        return this.defaultRequest<string>(url, http.RequestMethod.HEAD, handler);
    }
    static Options(url: string, handler?: (requestBody: DefaultRequestBody) => void): Promise<Object> {
        return this.defaultRequest<string>(url, http.RequestMethod.OPTIONS, handler);
    }
    static async Download(url: string, filePath: string, handler?: (requestBody: DownloadRequestBody) => void): Promise<Process> {
        let requestBody = new DownloadRequestBody(url, filePath);
        handler && handler(requestBody);
        const response = await this.requestReal<Process>(requestBody);
        return response;
    }
    static async Upload(url: string, handler?: (requestBody: UploadRequestBody) => void): Promise<Process> {
        let requestBody = new UploadRequestBody(url);
        handler && handler(requestBody);
        const response = await this.requestReal<Process>(requestBody);
        return response;
    }
    static Cancel(tag: string) {
        HttpConfig.getInstance().getTaskDispatch().cancel(tag);
    }
    static defaultRequest<T>(url: string, method: http.RequestMethod, handler?: (requestBody: RequestBody) => void): Promise<T> {
        let requestBody = new DefaultRequestBody(url, method);
        handler && handler(requestBody);
        return this.requestReal(requestBody);
    }
    private static requestReal<T>(requestBody: RequestBody): Promise<T> {
        return new Promise<T>((resolve, reject) => {
            requestBody.interceptorIndex = -1;
            HttpConfig.getInstance()
                .getTaskDispatch()
                .dispatch(requestBody)
                .then((response) => {
                try {
                    //进行结果转换
                    let bean: T = HttpConfig.getInstance().getConverter().convert(response);
                    resolve(bean);
                }
                catch (err) {
                    reject(err);
                }
            })
                .catch((err) => {
                reject(err);
            });
        });
    }
}
