// Copyright (C) 2024 Robert J.A. Wagner; Am Kiefernwald 49b; D-64297 Darmstadt - All Rights Reserved 
import { supply_DefaultContact, hoNotUSER, hoNotCLERK } from "./lib/contact.mjs";


export default class toApp {
    formContact() {
        let formContact = XRW.mkForm({ FID: 'formContact', ynCreateFolder: 'Y', folder: this.folder, ynCreateRender: 'Y', renderType: 'Form', pageX: 25, anchor: 'grForm' });
        formContact.setFEG("User", supply_DefaultContact());
        formContact.setSection("S1",
            { FEG_Prefix: "User", FEG_Title: "Kontakt", ynVolatile: 'Y' },
        );
        return formContact;
    }
    formAPI_Mail() {
        let formAPI_Mail = XRW.mkForm({ FID: 'formAPI_Mail', ynCreateFolder: 'N', folder: this.folder, ynCreateRender: 'Y', renderType: 'Form', pageX: 25, minPixelPerPoint: 20, anchor: 'grForm' });
        formAPI_Mail.setFEG('API_Mail',
            { FE_Prefix: 'GenericArray', FGM_Key: 'mailTo', FE_Title: 'Array' },
            { FE_Prefix: 'GenericString', FGM_Key: 'mailSubject', FE_Title: 'String' },
            { FE_Prefix: 'HTML', FGM_Key: 'mailBody', FE_Title: 'HTML-Text' },
        );
        formAPI_Mail.setSection("S1", { FEG_Prefix: "API_Mail", FEG_Title: "undefined", ynVolatile: 'Y' });
        return formAPI_Mail;
    }
    formBestaetigung_EGG() {
        let formBestaetigung_EGG = XRW.mkForm({ FID: 'formBestaetigung_EGG', ynCreateFolder: 'N', folder: this.folder, ynCreateRender: 'Y', renderType: 'Form', pageX: 25, minPixelPerPoint: 20, anchor: 'grForm' });
        formBestaetigung_EGG.setFEG('Bestaetigung_EGG',
            { FE_Prefix: 'HTML', FE_Title: 'HTML-Text', FGM_Format: 'dX1,W23,Vitem,N' },
            { FE_Prefix: 'SubTable', FGM_Format: 'dY1,dX1,W23,Vitem,N' },
            { FE_Prefix: 'HTML', FE_Title: 'HTML-Text', FGM_Format: 'dY2,dX1,W23,Vitem,N' },
        );
        formBestaetigung_EGG.setSection("S1", { FEG_Prefix: "Bestaetigung_EGG", FEG_Title: "undefined", ynVolatile: 'Y' });
        return formBestaetigung_EGG;
    }
    formEGG_Gerate() {
        let formEGG_Gerate = XRW.mkForm({ FID: 'formEGG_Gerate', ynCreateFolder: 'N', folder: this.folder, ynCreateRender: 'Y', renderType: 'Form', pageX: 25, minPixelPerPoint: 20, anchor: 'grForm' });
        formEGG_Gerate.setFEG('EGG_Gerate',
            { FE_Prefix: 'HTML', FE_Title: 'HTML-Text', FGM_Format: 'dX1,W23,Vitem,N' },
            { FE_Prefix: 'GenericButton', FGM_Key: 'btAdd', FE_Title: 'Hinzufügen', FGM_Format: 'dX1,W1,Vitem', icon: 'add', SEonClick: `$newForm("SubForm");XRW.message("Form created");`, FGM_TPos: 4, iconColor: 'red', ynfHidden: "$.FD_ixForm==0 || item.option.ynInSummaryMode=='Y'", ynReadOnly: 'Y', ynSkipSave: 'Y', iconSize_px: 25, ynClearCaption: 'Y', ynSkipOnSummary: 'Y', ynTableStyle: 'Y' },
        );
        formEGG_Gerate.setFEG('SubForm',
            { FE_Prefix: 'Render', FGM_Format: "dX10,f12,Sbold,W80,Vitem?uri=Typ,W80,Vitem?uri=Nummer,W80,Vitem?uri=Test,N", SEonBuild: `if (ctx.arg._FIX!=1) ctx.item.ynSkip='Y'` },
            { FE_Prefix: 'GenericInteger', FGM_Key: 'Anzahl', FE_Title: 'Anzahl', FGM_Format: 'dX1,W2,Vitem', default: 1, FGM_TPos: 1, maxvalue: 10, minvalue: 1, ynReadOnly: 'N', ynfOptional: 'N', defaultValue: 1, ynTableStyle: 'Y' },
            { FE_Prefix: 'GenericDate', FGM_Key: 'NextAb', FE_Title: 'Abholung', FGM_Format: 'dX1,W4,Vitem', SEautoValue: "$value('EGG_Informationen')", default: "$value('EGG_Informationen')", FGM_TPos: 4, ynfHidden: "item.option.ynInSummaryMode!='Y'", ynReadOnly: 'Y', ynTableStyle: 'Y' },
            { FE_Prefix: 'GenericFixSelect', FGM_Key: 'GeraeteTyp', FE_Title: 'Gerätetyp', FGM_Format: 'dX1,W12,Vitem', select: ['Aktenvernichter', 'Backofen', 'Drucker', 'Dunstabzugshaube', 'Gefrierschrank', 'Gefriertruhe', 'Heißmangel', 'Herd/Ceranfeld', 'Kühlschrank', 'Mikrowelle', 'Monitor', 'PC', 'Rasenmäher', 'Gartenelektrogeräte', 'Sonnenbank', 'Spülmaschine', 'Staubsauger', 'Trockner', 'TV', 'Waschmaschine', 'Klimageräte', 'Elektrische Sport- und Freizeitgeräte'], FGM_TPos: 8, placeholder: 'Bitte Auswählen', ynTableStyle: 'Y' },
            { FE_Prefix: 'GenericButton', FGM_Key: 'btDelete', FE_Title: 'Löschen', FGM_Format: 'dX1,W1,Vitem', icon: 'delete', SEonClick: '$deleteForm()', FGM_TPos: 4, iconColor: 'red', ynfHidden: "$.FD_ixForm==0 || item.option.ynInSummaryMode=='Y'", ynReadOnly: 'Y', ynSkipSave: 'Y', iconSize_px: 25, ynClearCaption: 'Y', ynSkipOnSummary: 'Y', ynTableStyle: 'Y' },
            // { FE_Prefix: 'HTML', FE_Title: 'HTML-Text', FGM_Format: 'W1,Vitem,N', html: '&nbsp;', FGM_TPos: 4, ynSkipSave: 'Y', ynfOptional: 'Y' },
        );
        formEGG_Gerate.setSection("S1", { FEG_Prefix: "EGG_Gerate", FEG_Title: "undefined", ynVolatile: 'Y' });
        formEGG_Gerate.setSection("S1", { FEG_Prefix: "SubForm", FEG_Title: "undefined", ynVolatile: 'Y' });
        return formEGG_Gerate;
    }
    async sendForm() {
        this.folder.getFolderParam();
        this.nState = 0;
        await this.nextForm();
    }
    async nextForm() {
        if (this.nState >= this.pState.length) return XRW.panic(`Form-index(${this.nState}) overflow`);
        XRW.mForm = new Map();
        let state = this.pState[this.nState];
        if (!state in this) return XRW.panic(`Form("${state}) not found`);
        // if (state.startsWith("sub")){}
        this.form = this[state]();
        this.nState++;
        if (this.nState < this.pState.length) {
            this.form.setFEG("Continue",
                { FE_Prefix: 'GenericButton', FGM_Format: "dY1,f10,dX1,W6,Vitem", FE_Title: 'Mit der Berarbeitung fortfahren', caption: "Weiter", ynClearCaption: 'N', icon: 'arrowRight', iconColor: 'blue', iconSize_px: 16, SEonClick: `#A;await oApp.nextForm();` },
            );
            this.form.setSection("S1",
                { FEG_Prefix: "Continue", FEG_Title: "Weiter", ynVolatile: 'Y' },
            );
        } else {
            this.form.setFEG("Send",
                { FE_Prefix: 'GenericButton', FGM_Format: "dY1,f12,cFill?color=magenta,dX1,W8,Vitem", FE_Title: 'Antrag absenden', caption: "Senden", ynClearCaption: 'N', icon: 'send', iconColor: 'blue', iconSize_px: 42, SEonClick: `#A;await oApp.sendForm();` },
            );
            this.form.setSection("S1",
                { FEG_Prefix: "Send", FEG_Title: "Antrag einreichen", ynVolatile: 'Y' },
            );

        }
        await XRW.windowResizeCallBack();
    }
    async Start() {
        this.pState = ['formContact', 'formEGG_Gerate', 'formAPI_Mail', 'formBestaetigung_EGG'];
        this.nState = 0;
        XRW.USR_EMail ??= "oma@oma.de";
        if (true) {
            Object.assign(XRW, XRW.key.url2json("?OU_Name=EGOVC&LOG_Role=USER&USR_EMail=robert.wagner@egovc.de&USR_Pass=Welcome1"));
            let res = await XRW.api.login();
            if (!res) return XRW.panic(`Login failed`);
            console.log(XRW.SID);
        }
        this.folder = XRW.mkFolder();
        await this.nextForm();
        if (false) {
            form.callBack_onChange((control, item) => {
                form.folder.debug();
            });
            console.log(form);
        }
    }
}
globalThis.oApp = new toApp();
