import { AttributeName, FormName } from './NameTypes';

export abstract class FormProxy { 
    private _xrm!: Xrm.XrmStatic;

    /**
     * Asynchronously saves the record.
     * @returns Returns an asynchronous promise.
     */
    public save(): Promise<void> {
        return new Promise((resolve, reject) => {
            this.Xrm.Page.data.save()
            .then(resolve)
            .fail(reject);
        });
    }

    /**
     * Asynchronously refreshes data on the form, without reloading the page.
     * @param save true to save the record, after the refresh.
     * @returns Returns an asynchronous promise.
     */
    public refresh(save: boolean): Promise<void> {        
        return new Promise((resolve, reject) => {
            this.Xrm.Page.data.refresh(save)
            .then(resolve)
            .fail(reject);
        });
    }

    /**
     * Gets form type.
     * @returns The form type.
     * @remarks **Values returned are**:
     * * 0  Undefined
     * * 1  Create
     * * 2  Update
     * * 3  Read Only
     * * 4  Disabled
     * * 6  Bulk Edit
     * * Deprecated values are 5 (Quick Create), and 11 (Read Optimized)
     */
    get FormType(): XrmEnum.FormType {
        return Xrm.Page.ui.getFormType();
    }

    /**
     * Gets the label for the form.
     * @returns The FormName.
     */
    get FormName(): FormName {
        return { name: Xrm.Page.ui.formSelector.getCurrentItem().getLabel() };
    }  

    /**
     * @returns The current Xrm Object on this script
     */  
    get Xrm() {
        return this._xrm;
    }
    set Xrm(xrm: Xrm.XrmStatic) {
        this._xrm = xrm;
    }
}