package com.bokecc.room.ui.view.widget;

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.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bokecc.room.ui.R;

public class CCQuestionAskView extends FrameLayout {
    public static final int ASK_MAX = 300;

    private OnClickTeacherAskViewListener listener;

    private Button btn_ask;
    private EditText et_ask;
    private TextView tv_text_count;

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

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

    public CCQuestionAskView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        View parentView = View.inflate(context, R.layout.cc_question_ask_view,null);
        addView(parentView);
        btn_ask = parentView.findViewById(R.id.btn_ask);
        et_ask = parentView.findViewById(R.id.et_ask);
        tv_text_count = parentView.findViewById(R.id.tv_text_count);
        et_ask.addTextChangedListener(textWatcher);
        btn_ask.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null){
                    String inputContent = et_ask.getText().toString();
                    if (!TextUtils.isEmpty(inputContent)) {
                        listener.clickSendAsk(inputContent);
                        et_ask.setText("");
                    }
                }
            }
        });
    }

    public void showAskView(OnClickTeacherAskViewListener listener){
        this.listener = listener;
        if (et_ask.isEnabled()) {
            et_ask.setHint("我要提问...");
        } else {
            et_ask.setHint("暂时无法提问");
        }
        et_ask.setText("");
        tv_text_count.setText("0/" + ASK_MAX);
        setVisibility(VISIBLE);
    }

    public interface OnClickTeacherAskViewListener{
        void clickSendAsk(String content);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            et_ask.removeTextChangedListener(this);
            if (editable.length() > ASK_MAX){
                et_ask.setText(editable.subSequence(0, ASK_MAX));
                tv_text_count.setText(""+ ASK_MAX + "/" + ASK_MAX);
            } else {
                et_ask.setText(editable.toString());
                tv_text_count.setText("" +editable.length() + "/" + ASK_MAX);
            }

            et_ask.setSelection(et_ask.getText().length());
            et_ask.addTextChangedListener(this);
        }
    };

    public void enableAskQuestion(boolean isEnable){
        btn_ask.setClickable(isEnable);
        if (!isEnable) {
            et_ask.setEnabled(false);
            et_ask.setText("");
            et_ask.setHint("暂时无法提问");
            tv_text_count.setText("0/" + ASK_MAX);
        } else {
            et_ask.setEnabled(true);
            et_ask.setHint("我要提问...");
            et_ask.setText("");
            tv_text_count.setText("0/" + ASK_MAX);
        }
    }
}
