﻿
// ReSharper disable InconsistentNaming

import * as jQuery from 'jquery';

interface IDiscussionClient {
    addMessage(message: Foo | null | undefined): Promise<void>;
}

class DiscussionClient implements IDiscussionClient {
    baseUrl: string;
    beforeSend: any = undefined;
    protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;

    constructor(baseUrl?: string) {
        this.baseUrl = baseUrl ?? "";
    }

    addMessage(message: Foo | null | undefined) {
        return new Promise<void>((resolve, reject) => {
            this.addMessageWithCallbacks(message, (result) => resolve(result), (exception, _reason) => reject(exception));
        });
    }

    private addMessageWithCallbacks(message: Foo | null | undefined, onSuccess?: (result: void) => void, onFail?: (exception: string, reason: string) => void) {
        let url_ = this.baseUrl + "/api/Discussion";
        url_ = url_.replace(/[?&]$/, "");

        const content_ = JSON.stringify(message);

        jQuery.ajax({
            url: url_,
            beforeSend: this.beforeSend,
            type: "post",
            data: content_,
            dataType: "text",
            headers: {
                "Content-Type": "application/json",
            }
        }).done((_data, _textStatus, xhr) => {
            this.processAddMessageWithCallbacks(url_, xhr, onSuccess, onFail);
        }).fail((xhr) => {
            this.processAddMessageWithCallbacks(url_, xhr, onSuccess, onFail);
        });
    }

    private processAddMessageWithCallbacks(_url: string, xhr: any, onSuccess?: any, onFail?: any): void {
        try {
            let result = this.processAddMessage(xhr);
            if (onSuccess !== undefined)
                onSuccess(result);
        } catch (e) {
            if (onFail !== undefined)
                onFail(e, "http_service_exception");
        }
    }

    protected processAddMessage(xhr: any): void | null {
        const status = xhr.status;

        let _headers: any = {};
        if (status === 204) {
            const _responseText = xhr.responseText;
            return;

        } else if (status !== 200 && status !== 204) {
            const _responseText = xhr.responseText;
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
        }
        return;
    }
}

class Foo implements IFoo {
    bar?: string | undefined;

    constructor(data?: IFoo) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (this as any)[property] = (data as any)[property];
            }
        }
    }

    init(_data?: any) {
        if (_data) {
            this.bar = _data["Bar"];
        }
    }

    static fromJS(data: any): Foo {
        data = typeof data === 'object' ? data : {};
        let result = new Foo();
        result.init(data);
        return result;
    }

    toJSON(data?: any) {
        data = typeof data === 'object' ? data : {};
        data["Bar"] = this.bar;
        return data;
    }
}

interface IFoo {
    bar?: string | undefined;
}

class ApiException extends Error {
    override message: string;
    status: number;
    response: string;
    headers: { [key: string]: any; };
    result: any;

    constructor(message: string, status: number, response: string, headers: { [key: string]: any; }, result: any) {
        super();

        this.message = message;
        this.status = status;
        this.response = response;
        this.headers = headers;
        this.result = result;
    }

    protected isApiException = true;

    static isApiException(obj: any): obj is ApiException {
        return obj.isApiException === true;
    }
}

function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
    if (result !== null && result !== undefined)
        throw result;
    else
        throw new ApiException(message, status, response, headers, null);
}