import http from "@ohos:net.http";
import ArrayList from "@ohos:util.ArrayList";
import type { Chain } from '../core/Chain';
import { HttpConfig } from "@normalized:N&&&@yic/http/src/main/ets/core/HttpConfig&1.0.1";
/**
 * @ProjectName : HarmonyUtilSdk
 * @FileName : RequestBody
 * @Author : yic
 * @Time : 2024/6/20 15:00
 * @Description : 定义请求体的封装
 */
export abstract class RequestBody {
    protected header = {};
    protected param = {};
    protected method: http.RequestMethod = http.RequestMethod.GET;
    protected url: string;
    interceptorIndex: number = -1;
    protected cancelFlag = false;
    protected tag?: string;
    private expressed = false; //true:接口调试时直接执行,不会因为指定队列满了进入等待队列,也不会因为与正在执行的相同接口而被抛弃;false:相反
    constructor(url: string, method: http.RequestMethod) {
        this.url = url;
        this.method = method;
    }
    setHeader(key: string, value: any): RequestBody {
        this.header[key] = value;
        return this;
    }
    getHeader(): Object {
        return this.header;
    }
    setMethod(method: http.RequestMethod) {
        this.method = method;
    }
    getMethod(): http.RequestMethod {
        return this.method;
    }
    setParam(key: string, value: number | string): RequestBody {
        this.param[key] = `${value}`;
        return this;
    }
    /**
     * 取消请求
     */
    cancel() {
        console.info('isCanceled');
        this.cancelFlag = true;
    }
    isCancel(): boolean {
        return this.cancelFlag;
    }
    setExpressed(isExpressed: boolean) {
        this.expressed = isExpressed;
    }
    isExpressed(): boolean {
        return this.expressed;
    }
    getParam(): Object {
        return this.param;
    }
    setUrl(url: string) {
        this.url = url;
    }
    setTag(tag: string) {
        this.tag = tag;
    }
    getTag(): string {
        return this.tag;
    }
    getUrl(): string {
        let url = '';
        if (this.url.startsWith('http')) {
            url = this.url;
        }
        else {
            url = HttpConfig.getInstance().getHost() + this.url;
        }
        return url;
    }
    /**
     * 先判断责任链模式中是否还有下一个拦截器处理,没有就调用具体请求
     * @param chain
     * @returns
     */
    execute(chain: Chain): Promise<http.HttpResponse> {
        this.interceptorIndex++;
        const interceptor = HttpConfig.getInstance().getInterceptor(this.interceptorIndex);
        if (!interceptor) {
            return this.call();
        }
        else {
            return interceptor.intercept(chain);
        }
    }
    //获取get请求参数拼接
    getUrlWithParam(): string {
        let key = this.getUrl();
        let p = '';
        let arr: ArrayList<string> = new ArrayList();
        Object.keys(this.getParam()).forEach((key) => {
            arr.add(key);
        });
        this.sort(arr).forEach((key) => {
            if (p.length > 0) {
                p += "&";
            }
            p += `${key}=${this.getParam()[key]}`;
        });
        if (p.length > 0) {
            key += `?${p}}`;
        }
        return key;
    }
    private sort(arr: ArrayList<string>): ArrayList<string> {
        if (arr.length <= 1) {
            return arr;
        }
        const result = new ArrayList<string>();
        arr.forEach((item) => {
            if (result.length === 0) {
                result.add(item);
            }
            else {
                for (let i = 0; i < result.length; i++) {
                    if (item.localeCompare(result[i]) < 0) {
                        result.insert(item, i);
                    }
                    else if (i === result.length - 1) {
                        result.add(item);
                        break;
                    }
                }
            }
        });
        return result;
    }
    private testSort(arr: ArrayList<string>): ArrayList<string> {
        let flag = false;
        for (let i = 0; i < arr.length - 1; i++) {
            for (let index = 0; index < arr.length - 1 - i; index++) {
                if (arr[index] > arr[index + 1]) {
                    flag = true; //有元素交换
                    let temp = arr[index];
                    arr[index] = arr[index + 1];
                    arr[index + 1] = temp;
                }
            }
            if (!flag) { //提前结束排序,优化
                break;
            }
            else {
                flag = false; //继续排序
            }
        }
        return arr;
    }
    protected abstract call(): Promise<http.HttpResponse>;
}
