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;
import com.bokecc.sskt.base.bean.CCQuestionBean;

public class CCQuestionAnswerView extends FrameLayout {
    public static final int QUESTION_MAX_HINT = 15;
    public static final int ANSWER_MAX = 300;

    private OnClickTeacherAnswerViewListener listener;
    private CCQuestionBean ccQuestionBean;

    private Button btn_answer;
    private EditText et_answer;
    private TextView tv_text_count;
    private ToggleButton toggle_button;

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

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

    public CCQuestionAnswerView(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_answer_view,null);
        addView(parentView);
        btn_answer = parentView.findViewById(R.id.btn_answer);
        et_answer = parentView.findViewById(R.id.et_answer);
        tv_text_count = parentView.findViewById(R.id.tv_text_count);
        et_answer.addTextChangedListener(textWatcher);
        toggle_button = parentView.findViewById(R.id.toggle_button);
        btn_answer.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null){
                    String inputContent = et_answer.getText().toString();
                    boolean isPivate = toggle_button.isChecked();
                    if (!TextUtils.isEmpty(inputContent)) {
                        listener.clickSendAnswer(inputContent,isPivate);
                        setVisibility(GONE);
                    }
                }
            }
        });
        parentView.findViewById(R.id.view_dismiss).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setVisibility(GONE);
            }
        });
    }

    public void initData(CCQuestionBean ccQuestionBean, OnClickTeacherAnswerViewListener listener){
        this.listener = listener;
        this.ccQuestionBean = ccQuestionBean;
        String content = ccQuestionBean.getContent();
        String showContent = content.length() > QUESTION_MAX_HINT ? content.substring(0,QUESTION_MAX_HINT) + "...": content;
        et_answer.setHint("回复：" + showContent);
        et_answer.setText("");
        tv_text_count.setText("0/" + ANSWER_MAX);
        setVisibility(VISIBLE);
    }

    public interface OnClickTeacherAnswerViewListener{
        void clickSendAnswer(String content,boolean isPrivate);
    }

    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_answer.removeTextChangedListener(this);
            if (editable.length() > ANSWER_MAX){
                et_answer.setText(editable.subSequence(0,ANSWER_MAX));
                tv_text_count.setText(""+ANSWER_MAX + "/" + ANSWER_MAX);
            } else {
                et_answer.setText(editable.toString());
                tv_text_count.setText("" +editable.length() + "/" + ANSWER_MAX);
            }

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