import { shortcut, store } from '@core/core';
import { MaintainModel } from './maintain.model';
import { MaintainForm } from './forms/form';
import { LinkButton } from 'src/modules/components/link_button/link';
import { PAGE_INDEX } from '@constants/constant';
import { ApiFactory } from 'src/modules/crud/apiFactory';
const api = new ApiFactory().create();
import './maintain.less';
import { connectionType } from '@constants/env';
import { getJdbcDatabaseType, getTextByDatabaseType } from '../../app.service';
import { ConnectionJDBC } from 'src/modules/crud/crud.typings';
import { ButtonGroup } from '@fui/core';

@shortcut()
@store(MaintainModel)
export class Maintain extends BI.Widget {
    static xtype = 'dec.dcm.maintain';

    model: MaintainModel['model'];
    store: MaintainModel['store'];

    buttonGroup: ButtonGroup;
    socketTip: LinkButton;

    render() {
        const { isEdit, databaseType } = this.getEditConnection();
        const titleText = getTextByDatabaseType(databaseType);

        return {
            type: BI.VTapeLayout.xtype,
            hgap: 16,
            items: [
                {
                    type: BI.VerticalAdaptLayout.xtype,
                    cls: 'bi-border-bottom',
                    height: 40,
                    hgap: 5,
                    items: [
                        {
                            type: BI.IconButton.xtype,
                            $value: 'back-databases',
                            cls: 'dcm-back-font',
                            height: 15,
                            invisible: this.model.isCopy || isEdit,
                            handler: () => {
                                this.store.setPageIndex(PAGE_INDEX.DATEBASE);
                            },
                        },
                        {
                            type: BI.Label.xtype,
                            text: titleText,
                            height: 15,
                        },
                        {
                            type: LinkButton.xtype,
                            invisible: true,
                            lgap: 10,
                            text: BI.i18nText('Dec-Dcm_Socket_Unable_Connect_Tip'),
                            link: api.getHyperlink(DecCst.Hyperlink.WEBSOCKET_CONNECT),
                            ref: (_ref: LinkButton) => {
                                this.socketTip = _ref;
                            },
                        },
                    ],
                },
                {
                    type: BI.ButtonGroup.xtype,
                    ref: (_ref: ButtonGroup) => {
                        this.buttonGroup = _ref;
                    },
                    items: this.renderItems(),
                },
            ],
        };
    }

    mounted() {
        if (!api.getSocketStatus()) {
            BI.Msg.toast(BI.i18nText('Dec-Dcm_Socket_Unable_Connect'), {
                level: 'warning',
            });
            this.socketTip.setVisible(true);
        }

        this.model.connectionSelectedOne
            && api.setEditedConnectionStatus(this.model.connectionSelectedOne.connectionName);
    }

    private renderItems() {
        const { type } = this.getEditConnection();

        return [{
            type: MaintainForm.xtype,
            connectionType: type,
        }];
    }

    private getEditConnection() {
        if (this.model.datebaseTypeSelected) {
            return {
                type: this.model.datebaseTypeSelectedOne.type,
                text: this.model.datebaseTypeSelectedOne.text,
                isEdit: false,
                databaseType: this.model.datebaseTypeSelectedOne.databaseType,
            };
        }
        let databaseType = this.model.connectionSelectedOne.connectionType;
        if (databaseType === connectionType.JDBC) {
            const connectionJDBC = this.model.connectionSelectedOne.connectionData as ConnectionJDBC;
            databaseType = getJdbcDatabaseType(connectionJDBC.database, connectionJDBC.driver).databaseType;
        }

        return {
            type: this.model.connectionSelectedOne.connectionType,
            text: this.model.connectionSelectedOne.connectionName,
            isEdit: true,
            databaseType,
        };
    }
}