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 android.widget.Spinner;

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

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;
import kz.arta.synergy.workflow.mobile.android.forms.mobile.adapters.AdapterFormsSpinner;

/**
 * Created by neox on 30.06.15.
 * Компонент "Выпадающий список"
 */
public class FormListBox extends LinearLayout implements IMobileComponents, IListBox {

    /**
     * Выпадающий список
     */
    private Spinner spinner;

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

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

    /**
     * Тип компонента
     */
    private String type;

    /**
     * Значение компонента
     */
    private String value;

    /**
     * Данные компонента
     */
    private MobileFormView.Data[] data;

    public FormListBox(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 spinnerParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        spinnerParams.setMargins(0, FormConstants.MARGINS, 0, FormConstants.MARGINS);
        // сам спиннер
        spinner = new Spinner(getContext(), Spinner.MODE_DIALOG);
        spinner.setLayoutParams(spinnerParams);
        spinner.setBackgroundResource(android.R.color.transparent);
        addView(spinner);
    }

    @Override
    public void setData(MobileFormView.Data[] data) {
        this.data = data;
        List<String> labels = new ArrayList<>();
        for (MobileFormView.Data item : data) {
            labels.add(item.label);
        }
        AdapterFormsSpinner adapter = new AdapterFormsSpinner(getContext(), labels);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        // выставление значение по умолчанию
        for (int i = 0; i < data.length; i++) {
            if (data[i].value.equals(value)) {
                spinner.setSelection(i);
            }
        }
    }

    @Override
    public String getKey() {
        if (data == null || data.length == 0) {
            return "";
        }
        return data[(int) (spinner.getSelectedItemId())].value;
    }

    @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) {
        spinner.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() {
        if (spinner == null || spinner.getSelectedItem() == null) {
            return "";
        }
        return spinner.getSelectedItem().toString();
    }

    @Override
    public void setValue(String value) {
        this.value = value;
    }

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

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