package kz.arta.synergy.workflow.mobile.android.forms.mobile.components;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import kz.arta.synergy.workflow.mobile.android.common.components.AEditText;
import kz.arta.synergy.workflow.mobile.android.common.components.ATitle;
import kz.arta.synergy.workflow.mobile.android.common.utils.Wrapper;
import kz.arta.synergy.workflow.mobile.android.forms.mobile.FormConstants;

/**
 * Created by neox on 30.06.15.
 * Компонент "Однострочное поле"
 */
public class FormTextBox extends LinearLayout implements IMobileComponents {

    /**
     * Заголовок
     */
    private ATitle title;

    /**
     * Сам компонент
     */
    private AEditText text;

    /**
     * Идентификатор компонента
     */
    private String id;

    /**
     * Тип
     */
    private String type;

    public FormTextBox(Context context) {
        super(context);
        init();
    }

    private void init() {
        setOrientation(LinearLayout.VERTICAL);
        setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        // заголовок
        LayoutParams titleParams = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        titleParams.setMargins(FormConstants.MARGINS, FormConstants.MARGINS, FormConstants.MARGINS, FormConstants.MARGINS);
        title = new ATitle(getContext());
        title.setLayoutParams(titleParams);
        addView(title);
        // сам компонент
        LayoutParams textParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        textParams.setMargins(0, FormConstants.MARGINS, 0, FormConstants.MARGINS);
        text = new AEditText(getContext());
        text.setLayoutParams(textParams);
        text.setMaxLines(1);
        addView(text);
    }

    @Override
    public void setLabel(String label) {
        if (label != null && !label.isEmpty()) {
            title.setText(label);
        } else {
            title.setVisibility(View.GONE);
        }
    }

    @Override
    public void enabled(boolean enabled) {
        text.setEnabled(enabled);
    }

    @Override
    public void setRequired(String required) {

    }

    @Override
    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String getType() {
        return type;
    }

    @Override
    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String getValue() {
        return text.getText().toString();
    }

    @Override
    public void setValue(String value) {
        text.setText(value);
    }

    @Override
    public String getID() {
        return id;
    }

    @Override
    public String getJson() {
        return "{" +
                Wrapper.getItem("type", getType()) + "," +
                Wrapper.getItem("id", getID()) + "," +
                Wrapper.getItem("value", getValue())
                + "}";
    }
}
