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

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;
import kz.arta.synergy.workflow.mobile.android.common.utils.Utils;

/**
 * Created by neox on 25.06.15.
 * Компонент для поиска
 */
public class SearchEditText extends RelativeLayout {

    /**
     * Поле для ввода текста
     */
    private EditText editText;

    /**
     * Иконка лупы
     */
    private ImageView icon;

    /**
     * Лиснер
     */
    private SearchTextListener listener;

    public SearchEditText(Context context) {
        this(context, null);
    }

    public SearchEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * Инициализация компонентов
     */
    private void init() {

        LayoutParams editTextParams = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        LayoutParams iconParams = new LayoutParams((int) (getResources().getDimension(R.dimen.user_chooser_search_image)),
                (int) (getResources().getDimension(R.dimen.user_chooser_search_image)));

        iconParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        iconParams.addRule(RelativeLayout.CENTER_VERTICAL);
        iconParams.setMargins(0, 0, (int) (getResources().getDimension(R.dimen.user_chooser_icon_margin_right)), 0);

        editText = new EditText(getContext());
        editText.setTypeface(Fonts.getMedium(getContext()));
        editText.setTextSize(Utils.getPxFromDp(R.dimen.edit_text_size_text, getContext()));
        editText.setHint(getResources().getString(R.string.search_items));
        editText.setBackgroundResource(R.drawable.edit_text_background);
        editText.setLayoutParams(editTextParams);
        editText.setMaxLines(1);
        editText.setSingleLine(true);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    icon.setImageResource(R.drawable.search_icon);
                } else {
                    icon.setImageResource(R.drawable.progress_disawoved_icon);
                }
                if (listener != null) {
                    listener.onTextChanged(s.toString());
                }
            }
        });

        icon = new ImageView(getContext());
        icon.setImageResource(R.drawable.search_icon);
        icon.setLayoutParams(iconParams);
        icon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                clear();
            }
        });

        addView(editText);
        addView(icon);
    }

    /**
     * Текст плейсхолдера
     *
     * @param text текст
     */
    public void setHint(String text) {
        if (editText != null) {
            editText.setHint(text);
        }
    }

    /**
     * Очищение
     */
    public void clear() {
        editText.setText("");
        icon.setImageResource(R.drawable.search_icon);
    }


    public String getText() {
        return editText.getText().toString();
    }

    public void setListener(SearchTextListener listener) {
        this.listener = listener;
    }

    public interface SearchTextListener {
        void onTextChanged(String s);
    }
}
