package kr.co.hanwha.hifive.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;

/**
 * Created by sanghoonkwon on 2018. 1. 22..
 */

public class HFCommentEditView extends LinearLayout {
    private EditText mEditText;
    private TextView mTextViewAddComment;

    private OnCommentEditListener onCommentEditListener;

    private ArrayList<String> mListTags;
    private String mLastText = "";
    private int mHeight = 0;

    public void setOnCommentEditListener(OnCommentEditListener onCommentEditListener) {
        this.onCommentEditListener = onCommentEditListener;
    }

    public HFCommentEditView(Context context) {
        super(context);
        initialize(context);
    }

    public HFCommentEditView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    public HFCommentEditView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    private void initialize(Context context) {
        if (isInEditMode()) return;

        mListTags = new ArrayList<>();

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_comment_edit, this, true);

        mEditText = (EditText) findViewById(R.id.editText);

        mTextViewAddComment = (TextView) findViewById(R.id.addComment_textView);

        mEditText.addTextChangedListener(textWatcher);
        mEditText.setOnFocusChangeListener(onFocusChangeListener);

        mTextViewAddComment.setOnClickListener(onClickListener);
    }

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

    public void clearText() {
        mEditText.setText("");
        mEditText.clearFocus();
    }

    public void setText(@Nullable String text) {
        if (text != null) {
            mEditText.setText(text);
        }
    }

    public boolean setFocus() {
        return mEditText.requestFocus();
    }

    /**
     * 데이터 확인
     *
     * @return TRUE : EMPTY
     */
    public boolean isEmpty() {
        return (TextUtils.isEmpty(getText()));
    }

    /**
     * 태그 목록
     *
     * @return ArrayList
     */
    public ArrayList<String> getListTags() {
        return mListTags;
    }


    /**************************************************
     * OnClickListener
     **************************************************/
    OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.addComment_textView:
                    if (onCommentEditListener != null) {
                        String contents = mEditText.getText().toString();
                        onCommentEditListener.onAddCommentClicked(HFCommentEditView.this, contents);
                    }
                    break;
            }
        }
    };

    /**************************************************
     * OnFocusChangeListener
     **************************************************/
    OnFocusChangeListener onFocusChangeListener = new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (onCommentEditListener != null)
                onCommentEditListener.onFocusChange(HFCommentEditView.this, b);
        }
    };


    /**************************************************
     * EditText - TextWatcher
     **************************************************/
    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            mLastText = s.toString();
        }

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

        @Override
        public void afterTextChanged(Editable s) {
            if (onCommentEditListener != null)
                onCommentEditListener.onValueChanged(HFCommentEditView.this);

            if (s.toString().length() > 0) {
                mTextViewAddComment.setVisibility(View.VISIBLE);
            } else {
                mTextViewAddComment.setVisibility(View.GONE);
            }

            if (mHeight != HFCommentEditView.this.getHeight()) {
                mHeight = HFCommentEditView.this.getHeight();
                if (onCommentEditListener != null)
                    onCommentEditListener.onHeightChanged(mHeight);
            }
        }
    };

    /**************************************************
     * OnCommentEditListener
     **************************************************/
    public interface OnCommentEditListener {
        void onHeightChanged(@NonNull int height);

        void onFocusChange(@NonNull HFCommentEditView v, boolean b);

        void onValueChanged(@NonNull HFCommentEditView v);

        void onAddCommentClicked(@NonNull HFCommentEditView v, @Nullable String contents);
    }
}
