/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
*  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.com)
*  @license    Pimcore Open Core License (POCL)
*/

pimcore.registerNS("pimcore.object.tags.link");
/**
 * @private
 */
pimcore.object.tags.link = Class.create(pimcore.object.tags.abstract, {

    type: "link",
    dirty: false,

    initialize: function (data, fieldConfig) {

        this.data = "";
        this.defaultData = {
            type: "internal",
            path: "",
            parameters: "",
            anchor: "",
            accesskey: "",
            rel: "",
            tabindex: "",
            target: ""
        };

        if (data) {
            this.data = data;
        }
        else {
            this.data = this.defaultData;
        }
        this.fieldConfig = fieldConfig;

    },

    getGridColumnConfig: function (field) {
        var renderer = function (key, value, metaData, record) {
            this.applyPermissionStyle(key, value, metaData, record);

            if (record.data.inheritedFields && record.data.inheritedFields[key] && record.data.inheritedFields[key].inherited == true) {
                metaData.tdCls += " grid_value_inherited";
            }
            if (value) {
                return value.text;
            }
            return t("empty");

        }.bind(this, field.key);

        return {
            text: t(field.label), sortable: true, dataIndex: field.key, renderer: renderer,
            getEditor: this.getWindowCellEditor.bind(this, field)
        };
    },

    getLayoutEdit: function () {

        this.editButton = new Ext.Button({
            iconCls: "pimcore_icon_link pimcore_icon_overlay_edit",
            style: "margin-left: 5px",
            handler: this.openEditor.bind(this)
        });

        this.openButton = new Ext.Button({
            iconCls: "pimcore_icon_open",
            style: "margin-left: 5px",
            handler: function() {
                if (this.data && this.data.path) {
                    if (this.data.linktype == "internal") {
                        pimcore.helpers.openElement(this.data.path, this.data.internalType);
                    } else {
                        window.open(this.data.path, "_blank");
                    }
                }
            }.bind(this)
        });

        var text = "[" + t("not_set") + "]";
        if (this.data.text) {
            text = this.data.text;
        } else if (this.data.path) {
            text = this.data.path;
        }

        if (text.length > 100) {
            text = text.slice(0, 99)
            text += '…'
        }

        this.displayField = new Ext.form.DisplayField({
            value: text
        });

        var componentCfg = {
            fieldLabel: t(this.fieldConfig.title),
            layout: 'hbox',
            border: false,
            combineErrors: false,
            items: [this.displayField, this.openButton, this.editButton],
            componentCls: this.getWrapperClassNames(),
        };

        if (this.fieldConfig.labelWidth) {
            componentCfg.labelWidth = this.fieldConfig.labelWidth;
        }

        if (this.fieldConfig.labelAlign) {
            componentCfg.labelAlign = this.fieldConfig.labelAlign;
        }

        this.component = Ext.create('Ext.form.FieldContainer', componentCfg);

        return this.component;
    },


    getLayoutShow: function () {

        this.component = this.getLayoutEdit();
        this.editButton.hide();

        return this.component;
    },

    getValue: function () {
        return this.data;
    },

    getName: function () {
        return this.fieldConfig.name;
    },

    openEditor: function () {
        let config = [];

        if (!empty(this.fieldConfig.allowedTypes)){
            config['allowedTypes'] = this.fieldConfig.allowedTypes;
        }
        if (!empty(this.fieldConfig.allowedTargets)){
            config['allowedTargets'] = this.fieldConfig.allowedTargets;
        }
        if (!empty(this.fieldConfig.disabledFields)){
            config['disabledFields'] = this.fieldConfig.disabledFields;
        }

        this.window = pimcore.helpers.editmode.openLinkEditPanel(this.data, {
            empty: this.empty.bind(this),
            cancel: this.cancel.bind(this),
            save: this.save.bind(this)
        }, config);
    },

    openSearchEditor: function () {
        pimcore.helpers.itemselector(false, this.addDataFromSelector.bind(this), {
                type: ["asset", "document"]
            },
            {
                context: Ext.apply({scope: "objectEditor"}, this.getContext())
            });
    },

    save: function () {
        var values = this.window.getComponent("form").getForm().getFieldValues();
        if (Ext.encode(values) != Ext.encode(this.data)) {
            this.dirty = true;
        }
        this.data = values;

        var text = "[" + t("not_set") + "]";
        if (this.data.text) {
            text = this.data.text;
        } else if (this.data.path) {
            text = this.data.path;
        }

        if (text.length > 100) {
            text = text.slice(0, 99)
            text += '…'
        }

        this.displayField.setValue(text);

        // close window
        this.window.close();
    },

    empty: function () {

        // close window
        this.window.close();

        this.data = this.defaultData;
        this.dirty = true;

        // set text
        this.displayField.setValue("[" + t("not_set") + "]");
    },

    cancel: function () {
        this.window.close();
    },

    isDirty: function () {
        if (!this.isRendered()) {
            return false;
        }

        return this.dirty;
    },

    getCellEditValue: function () {
        return this.getValue();
    }
});
