package org.example.Entity.Control.Fields;


import org.example.Entity.Control.Base.Component;

import org.example.Utils.NativeExtensionUtils;

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

public class LabelCheckBox extends Field {
    private Field mainfield;
    public boolean hideLabel = true;


    public LabelCheckBox(Map<String, Object> row, Field field) {
        super(row);
        this.defaultXtype = "CheckFieldContainer";
        this.mainfield = field;
    }

    @Override
    public Object getDefaultval() {
        return super.getDefaultval();
    }

    @Override
    public void setDefaultval(Object value) {
        super.setDefaultval(value);
        String valueStr = value != null ? value.toString() : "";
        boolean empty = valueStr.isEmpty();
        ((Checkbox) getItems().get(0)).setValue(!empty);
        Field subField = (Field) getItems().get(1);
        subField.setDefaultval(valueStr);
        subField.disabled = (empty);
    }

    @Override
    public void setTabIndex(Integer value) {
        ((Field) getItems().get(1)).setTabIndex(value);
    }

    private List<Component> _items;

    public List<Component> getItems() {
        if (_items == null) {
            _items = new ArrayList<>();
            this.mainfield.setTop(0);
            this.mainfield.setLeft(18);
            if (this.getWidth() != null) {
                this.mainfield.setWidth(this.getWidth() - 18);
            }
            Checkbox cbox = new Checkbox(null);
            cbox.setName(this.getName() + "_ckbox");
            cbox.setXtype("labelCheckBox");
            String mainFieldDefaultVal = this.mainfield.getDefaultval() != null ? this.mainfield.getDefaultval().toString() : "";
            String mainFieldValue = this.mainfield.getValue() != null ? this.mainfield.getValue().toString() : "";
            boolean hasValue = !mainFieldDefaultVal.isEmpty() || !mainFieldValue.isEmpty();
            cbox.setValue(hasValue);
            cbox.setTop(3);
            this.mainfield.disabled = (!NativeExtensionUtils.toBoolean(cbox.getValue(), false));
            _items.add(cbox);
            _items.add(this.mainfield);
        }
        return _items;
    }
}