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 MapLocationField extends Field {
    private String icon;
    private String glyph;

    public MapLocationField(Map<String, Object> row) {
        super(row);
        this.defaultXtype = "maplocationfield";
    }

    public MapLocationField(Map<String, Object> row, IPublicUtil util) {
        super(row, util);
        this.defaultXtype = "maplocationfield";
    }

    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.width = (25);
            img.setTop(1);
            img.height = (text.getHeight());
            if (text.getWidth() != null) {
                img.setLeft(text.getWidth() + 1);
            }
            img.setHandler("OpenLocationMap");

            Hidden longitude = new Hidden(null);
            longitude.setName(this.getName() + "_longitude");

            Hidden latitude = new Hidden(null);
            latitude.setName(this.getName() + "_latitude");

            Hidden itude = new Hidden(null);
            itude.setName(this.getName() + "_itude");

            Hidden province = new Hidden(null);
            province.setName(this.getName() + "_province");

            Hidden city = new Hidden(null);
            city.setName(this.getName() + "_city");

            Hidden district = new Hidden(null);
            district.setName(this.getName() + "_district");

            Hidden street = new Hidden(null);
            street.setName(this.getName() + "_street");

            _items.add(text);
            _items.add(img);
            _items.add(longitude);
            _items.add(latitude);
            _items.add(itude);
            _items.add(province);
            _items.add(city);
            _items.add(district);
            _items.add(street);
        }
        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(), ""));
            ((Hidden) getItems().get(2)).setDefaultval(DataTableUtil.getRowVal(updrow, this.getName() + "_longitude", ""));
            ((Hidden) getItems().get(3)).setDefaultval(DataTableUtil.getRowVal(updrow, this.getName() + "_latitude", ""));
            ((Hidden) getItems().get(4)).setDefaultval(DataTableUtil.getRowVal(updrow, this.getName() + "_itude", ""));
        }
    }
}