import { AxiosInstance } from "axios";
import { TextBoxFactory } from "../ComponentFactory/TextBoxFactory";
import { MainStateManager } from "../MainStateManager";
import { ElementsOfFormFactory } from "../Page/ElementsOfFormFactory";
import { Argument } from "../Page/ElementsOfFormFactory/Argument";
import { IMainStateFactory } from "../Types";

interface ISaveResponse {
    data: any;
    isSuccess: boolean;
    massage: string;
}
export class SimpleRouteFormFactory implements IMainStateFactory {
    public argument?: Argument;
    public getTextBoxFactory = (fieldName: string): TextBoxFactory => {
        const selfFactoryName = 'selfFactory' + fieldName;
        return this.any[selfFactoryName];
    }

    public forceUpdate = () => { };
    public cach: {} = {};


    public set accept(value: any) {
        this._accept = value;
    }
    public get accept() {
        return this._accept;
    }
    public _accept = (afterSuccess?: (responseData: any) => void) => {
        const elementErrorCount = this.elementsOfForm.validate();
        if (elementErrorCount) {
            this.elementsOfForm.showThereAreSomeErrorYouCanNot(elementErrorCount);
            return;
        }

        this.saveData(afterSuccess);
    }

    public saveData = (afterSuccess?: (responseData: any) => void) => {
        if (!(this.axiosData && this.apiSync)) {
            return;
        }
        this.elementsOfForm.showWaitingFormSpinner();
        this.apiSync.post<ISaveResponse>(this.axiosData.acceptPath, this.elementsOfForm.searchParameter, this.mainStateManager.tokenInfo.headerOfAxios)
            .then(async response => {
                if (response.data.isSuccess) {
                    if (response.data.getProp('isSuccess')) {
                        const id = response.data.data.getProp('id');
                        await this.elementsOfForm.saveAttachedFiles(id);
                        this.elementsOfForm.closeWaitingFormSpinner();
                        if (afterSuccess) {
                            this.elementsOfForm.showSuccessFullMessageBox(undefined, undefined, undefined, () => afterSuccess(response.data.getProp('data')));
                        } else {
                            this.elementsOfForm.showSuccessFullMessageBox(undefined, undefined, undefined);
                        }
                        this.forceUpdate();
                    } else {
                        this.elementsOfForm.closeWaitingFormSpinner();
                        this.elementsOfForm.showInvalidArgumentMessageBox(response.data.getProp('messageRoot'));
                    }
                } else {
                    this.elementsOfForm.closeWaitingFormSpinner();
                    this.elementsOfForm.showInvalidArgumentMessageBox(response.data.getProp('messageRoot'));
                }
            })
            .catch(e => {
                this.elementsOfForm.closeWaitingFormSpinner();
                this.elementsOfForm.showErrorMessageBox(e);
            });
    }

    public cancel: () => void = () => { };
    public delete: () => void = () => { };
    public new: () => void = () => { };
    public get any(): any {
        return this;
    }
    public elementsOfForm: ElementsOfFormFactory;
    public apiSync?: AxiosInstance;
    constructor(
        public mainStateManager: MainStateManager,
        public axiosData?: {
            axios: (path: string) => AxiosInstance,
            controllerPath: string,
            acceptPath: string,
        }
    ) {
        this.elementsOfForm = new ElementsOfFormFactory(this);
        if (this.axiosData) {
            this.apiSync = this.axiosData.axios(this.axiosData.controllerPath);
        }
    }

    public close = () => {
        if (this.elementsOfForm.subPageItem) {
            this.elementsOfForm.subPageItem.close();
        }
    }
}