import { IViewBargeAxiosType, ViewBargeFactory } from "./ViewBargeFactory";

export class LockUnLockViewBargeFactory {
    public get isValid() {
        return this.message === null;
    }

    public message: string | null = null;
    public controllerPath: string = '';
    private actionLockPath: string = 'lock';
    private actionUnLockPath: string = 'unLock';
    private id: number = 0;
    private yearId: number = 0;


    constructor(
        public viewBargeFactory: ViewBargeFactory,
        public axiosData?: IViewBargeAxiosType,
        id?: number,
        yearId?: number | null,
    ) {

        if (typeof id === 'number' && id > 0) {
            this.id = id;
        } else {
            this.message = 'شناسه برگه به درستی شناسایی نشده است ';
        }
        if (this.isValid && typeof yearId === 'number' && yearId > 0) {
            this.yearId = yearId;
        } else {
            this.message = 'دوره مالی برگه به درستی شناسایی نشده است ';
        }
        if (this.isValid && this.axiosData) {
            this.controllerPath = this.axiosData.controllerLockUnLockPath || this.axiosData.controllerPath;
            this.actionLockPath = this.axiosData.actionLockPath || 'lock';
            this.actionUnLockPath = this.axiosData.actionUnLockPath || 'unLock';
        } else {
            this.message = 'اطلاعات ارتباطی به درستی ثبت نشده است ';
        }
    }


    public toggel = (fieldName: string) => {
        if (!fieldName) {
            this.viewBargeFactory.elementsOfForm.showInvalidArgumentMessageBox('Field Name did not pass correctlly!!!');
            return;
        }
        const currentValue = this.viewBargeFactory.lastResponse.getProp(fieldName);
        if (currentValue) {
            this.unLock(fieldName);
        } else {
            this.lock(fieldName);
        }

    }

    public lock = (fieldName: string) => {
        if (!fieldName) {
            this.viewBargeFactory.elementsOfForm.showInvalidArgumentMessageBox('Field Name did not pass correctlly!!!');
            return;
        }
        if (this.isValid) {
            this.lockUnLock(this.actionLockPath, () => {
                this.viewBargeFactory.lastResponse.setProp(fieldName, true);
            })
        } else {
            this.viewBargeFactory.elementsOfForm.showInvalidArgumentMessageBox(this.message!);
        }
    }

    public unLock = (fieldName: string) => {
        if (!fieldName) {
            this.viewBargeFactory.elementsOfForm.showInvalidArgumentMessageBox('Field Name did not pass correctlly!!!');
            return;
        }
        if (this.isValid) {
            this.lockUnLock(this.actionUnLockPath, () => {
                this.viewBargeFactory.lastResponse.setProp(fieldName, false);
            })
        } else {
            this.viewBargeFactory.elementsOfForm.showInvalidArgumentMessageBox(this.message!);
        }
    }

    public lockUnLock = (path: string, afterSuccessed: () => void) => {
        if (!(this.axiosData)) {
            this.viewBargeFactory.elementsOfForm.showInvalidArgumentMessageBox('تنظیمات به درستی انجام نشده است');
            return;
        }

        this.viewBargeFactory.elementsOfForm.showWaitingFormSpinner();

        this.axiosData.axios(this.controllerPath).post(`${path}/${this.id}/${this.yearId}`, {}, this.viewBargeFactory.mainStateManager.tokenInfo.headerOfAxios)
            .then(response => {
                if (response.data.isSuccess) {
                    afterSuccessed();
                    this.viewBargeFactory.elementsOfForm.closeWaitingFormSpinner();
                    this.viewBargeFactory.forceUpdate();
                } else {
                    this.viewBargeFactory.elementsOfForm.closeWaitingFormSpinner();
                    this.viewBargeFactory.elementsOfForm.showInvalidArgumentMessageBox(response.data.messageRoot);
                }
            })
            .catch(e => {
                this.viewBargeFactory.elementsOfForm.closeWaitingFormSpinner();
                this.viewBargeFactory.elementsOfForm.showErrorMessageBox(e);
            });
    }
}