package org.example.Entity.Control.Fields;


import org.example.Entity.Control.Base.Component;
import org.example.Entity.Control.Com.Button;
import org.example.Utils.DataTableUtil;
import org.example.Utils.IPublicUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TextAreaEditor extends Field {
    public String icon;
    public String glyph;

    public TextAreaEditor(Map<String, Object> row) {
        super(row);
        this.setXtype("fields.textareaeditor");
    }


    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getGlyph() {
        return glyph;
    }

    public void setGlyph(String glyph) {
        this.glyph = glyph;
    }

    @Override
    public void setTabIndex(Integer value) {
        if (getItems() != null && !getItems().isEmpty()) {
            ((TextField) getItems().get(0)).setTabIndex(value);
        }
    }

    private List<Component> _items;

    public List<Component> getItems() {
        if (_items == null) {
            _items = new ArrayList<>();
            TextField text = new TextField(this._row);
            text.setTop(0);
            text.setLeft(0);
            if (this.getWidth() != null) {
                text.setWidth(this.getWidth() - 30);
            }

            Button img = new Button();
            img.setXtype("button.IconTextButton");
            img.width = (25);
            img.setTop(1);
            img.height = (text.getHeight());
            if (text.getWidth() != null) {
                img.setLeft(text.getWidth() + 1);
            }
            img.setHandler("OpenEditor");

            _items.add(text);
            _items.add(img);
        }
        return _items;
    }

    @Override
    public void setDefaultVal(Map<String, Object> updrow) {
        if (getItems() != null && !getItems().isEmpty()) {
            ((TextField) getItems().get(0)).setDefaultval(DataTableUtil.getRowVal(updrow, this.getName(), ""));
        }
    }
}