import { shortcut } from '@core/core';
import { Vertical, TextEditor, Htape, Left, Label, TextValueCombo, EdirotIconCheckCombo } from 'ui';
import { FormItemXtype } from '../../components/form_item/form_item';
import { ConnectionJNDI, Connection, ContextHashtable } from 'src/modules/crud/crud.typings';
import { CONNECT_CHARSET, CONNECTION_LAYOUT, JNDI_FACTORYS } from '@constants/constant';
import { CollapseXtype, EVENT_CHANGE } from 'src/modules/components/collapse/collapse';
import { connectionType } from '@constants/env';
import { TextCheckerXtype } from '../../../../components/text_checker/text_checker';
export const FormJndiXtype = 'dec.dcm.maintain.form.jndi';
@shortcut(FormJndiXtype)
export class FormJndi extends BI.Widget {
    props = {
        formData: {} as Connection,
    }
    advancedSet: any;
    testStatus: any;
    form = {
        connectionName: null,
        jndiName: null,
        initial: null,
        providerUrl: null,
        securityPrincipal: null,
        securityCredentials: null,
        newCharsetName: null,
        factoryObject: null,
        factoryState: null,
        factoryUrlPkgs: null,
        dnsUrl: null,
        authoritative: null,
        batchsize: null,
        referral: null,
        securityProtocol: null,
        authentication: null,
        language: null,
        applet: null,
    }

    render() {
        const { connectionName, connectionData } = this.options.formData;
        const { jndiName, newCharsetName, contextHashtable } = connectionData as ConnectionJNDI;
        const { hgap, vgap } = CONNECTION_LAYOUT;

        return {
            type: Vertical,
            hgap,
            vgap,
            items: [
                {
                    type: FormItemXtype,
                    name: BI.i18nText('Dec-Dcm_Connection_Name'),
                    nameWidth: 200,
                    forms: [{
                        type: TextCheckerXtype,
                        width: 300,
                        value: connectionName,
                        ref: (_ref: any) => {
                            this.form.connectionName = _ref;
                        },
                    }],
                },
                {
                    type: FormItemXtype,
                    name: BI.i18nText('Dec-Dcm_Connection_JNDI_Form_ConnectionName'),
                    nameWidth: 200,
                    forms: [{
                        type: TextEditor,
                        width: 300,
                        allowBlank: true,
                        value: jndiName,
                        ref: (_ref: any) => {
                            this.form.jndiName = _ref;
                        },
                    }],
                },
                {
                    type: Htape,
                    height: 180,
                    items: [
                        {
                            el: {
                                type: Left,
                                items: [
                                    {
                                        type: Label,
                                        cls: 'bi-font-bold',
                                        textAlign: 'left',
                                        text: BI.i18nText('Dec-Dcm_Connection_JNDI_Form_Connection'),
                                    },
                                ],
                            },
                            width: 200,
                        },
                        {
                            type: Vertical,
                            bgap: 10,
                            items: [
                                {
                                    type: FormItemXtype,
                                    name: 'INTIAL_CONTEXT_FACTORY',
                                    nameWidth: 200,
                                    forms: [{
                                        type: EdirotIconCheckCombo,
                                        width: 300,
                                        value: contextHashtable['java.naming.factory.initial'],
                                        ref: (_ref: any) => {
                                            this.form.initial = _ref;
                                        },
                                        items: JNDI_FACTORYS.map(item => {
                                            return {
                                                text: item.factory,
                                                value: item.factory,
                                            };
                                        }),
                                        listeners: [{
                                            eventName: BI.EditorIconCheckCombo.EVENT_CHANGE,
                                            action: () => {
                                                const value = this.form.initial.getValue();
                                                const factory = JNDI_FACTORYS.find(item => item.factory === value);
                                                factory ? this.form.providerUrl.setValue(factory.url) : null;
                                            },
                                        }],
                                    }],
                                },
                                {
                                    type: FormItemXtype,
                                    name: 'PROVIDER_URL',
                                    nameWidth: 200,
                                    forms: [{
                                        type: TextEditor,
                                        width: 300,
                                        allowBlank: true,
                                        value: contextHashtable['java.naming.provider.url'],
                                        ref: (_ref: any) => {
                                            this.form.providerUrl = _ref;
                                        },
                                    }],
                                },
                                {
                                    type: FormItemXtype,
                                    name: 'SECURITY_PRINCIPAL',
                                    nameWidth: 200,
                                    forms: [{
                                        type: TextEditor,
                                        width: 300,
                                        allowBlank: true,
                                        value: contextHashtable['java.naming.security.principal'],
                                        ref: (_ref: any) => {
                                            this.form.securityPrincipal = _ref;
                                        },
                                    }],
                                },
                                {
                                    type: FormItemXtype,
                                    name: 'SECURITY_CREDENTIALS',
                                    nameWidth: 200,
                                    forms: [{
                                        type: TextEditor,
                                        width: 300,
                                        allowBlank: true,
                                        value: contextHashtable['java.naming.security.credentials'],
                                        ref: (_ref: any) => {
                                            this.form.securityCredentials = _ref;
                                        },
                                    }],
                                },
                                {
                                    type: Label,
                                    cls: 'bi-tips',
                                    textAlign: 'left',
                                    text: BI.i18nText('Dec-Dcm_Connection_JNDI_Warning'),
                                },
                            ],
                        },
                    ],
                },
                {
                    type: FormItemXtype,
                    name: BI.i18nText('Dec-Dcm_Connection_Form_OriginalCharsetName'),
                    nameWidth: 200,
                    forms: [{
                        type: TextValueCombo,
                        width: 300,
                        value: newCharsetName ? newCharsetName : '',
                        items: CONNECT_CHARSET,
                        ref: (_ref: any) => {
                            this.form.newCharsetName = _ref;
                        },
                    }],
                },
                {
                    type: CollapseXtype,
                    width: 70,
                    name: BI.i18nText('Dec-Dcm_Connection_Form_Database_Advanced_Setting'),
                    listeners: [
                        {
                            eventName: EVENT_CHANGE,
                            action: (isCollapse: boolean) => {
                                this.advancedSet.setVisible(!isCollapse);
                            },
                        },
                    ],
                },
                {
                    type: Vertical,
                    vgap,
                    invisible: true,
                    ref: (_ref: any) => {
                        this.advancedSet = _ref;
                    },
                    items: [
                        {
                            type: FormItemXtype,
                            name: 'OBJECT_FACTORIES',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.factory.object'],
                                ref: (_ref: any) => {
                                    this.form.factoryObject = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'STATE_FACTORIES',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.factory.state'],
                                ref: (_ref: any) => {
                                    this.form.factoryState = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'URL_PKG_PREFIXES',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.factory.url.pkgs'],
                                ref: (_ref: any) => {
                                    this.form.factoryUrlPkgs = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'DNS_URL',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.dns.url'],
                                ref: (_ref: any) => {
                                    this.form.dnsUrl = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'AUTHORITATIVE',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.authoritative'],
                                ref: (_ref: any) => {
                                    this.form.authoritative = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'BATCHSIZE',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.batchsize'],
                                ref: (_ref: any) => {
                                    this.form.batchsize = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'REFERRAL',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.referral'],
                                ref: (_ref: any) => {
                                    this.form.referral = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'SECURITY_PROTOCOL',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.security.protocol'],
                                ref: (_ref: any) => {
                                    this.form.securityProtocol = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'SECURITY_AUTHENTICATION',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.security.authentication'],
                                ref: (_ref: any) => {
                                    this.form.authentication = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'LANGUAGE',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.language'],
                                ref: (_ref: any) => {
                                    this.form.language = _ref;
                                },
                            }],
                        },
                        {
                            type: FormItemXtype,
                            name: 'APPLET',
                            nameWidth: 200,
                            forms: [{
                                type: TextEditor,
                                width: 300,
                                allowBlank: true,
                                value: contextHashtable['java.naming.applet'],
                                ref: (_ref: any) => {
                                    this.form.applet = _ref;
                                },
                            }],
                        },
                    ],
                },
            ],
        };
    }

    public setError(value: string) {
        this.form.connectionName.setError(value);
    }

    public getSubmitValue():Connection {
        const contextHashtable = {
            'java.naming.factory.initial': this.form.initial.getValue(),
            'java.naming.provider.url': this.form.providerUrl.getValue(),
            'java.naming.factory.object': this.form.factoryObject.getValue(),
            'java.naming.factory.state': this.form.factoryState.getValue(),
            'java.naming.factory.url.pkgs': this.form.factoryUrlPkgs.getValue(),
            'java.naming.dns.url': this.form.dnsUrl.getValue(),
            'java.naming.authoritative': this.form.authoritative.getValue(),
            'java.naming.batchsize': this.form.batchsize.getValue(),
            'java.naming.referral': this.form.referral.getValue(),
            'java.naming.security.protocol': this.form.securityProtocol.getValue(),
            'java.naming.security.authentication': this.form.authentication.getValue(),
            'java.naming.security.principal': this.form.securityPrincipal.getValue(),
            'java.naming.security.credentials': this.form.securityCredentials.getValue(),
            'java.naming.language': this.form.language.getValue(),
            'java.naming.applet': this.form.applet.getValue(),
        };

        for (const propName in contextHashtable) {
            if (contextHashtable[propName] === null || contextHashtable[propName] === undefined || contextHashtable[propName] === '') {
                delete contextHashtable[propName];
            }
        }
        
        return {
            connectionId: this.form.connectionName.getValue(),
            connectionName: this.form.connectionName.getValue(),
            connectionType: connectionType.JNDI,
            connectionData: {
                jndiName: this.form.jndiName.getValue(),
                newCharsetName: this.form.newCharsetName.getValue()[0] || '',
                originalCharsetName: this.form.newCharsetName.getValue()[0] || '',
                creator: Dec ? Dec.personal.username : '',
                contextHashtable: contextHashtable as ContextHashtable,
            },
        };
    }
}