package com.bokecc.livemodule.live.qa;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.DWLiveCoreHandler;
import com.bokecc.livemodule.live.DWLiveQAListener;
import com.bokecc.livemodule.live.chat.KeyboardHeightObserver;
import com.bokecc.livemodule.live.qa.adapter.LiveQaAdapter;
import com.bokecc.livemodule.live.qa.adapter.QaAdapter;
import com.bokecc.livemodule.live.qa.module.QaInfo;
import com.bokecc.livemodule.live.qa.qainputlayout.QaInputLayout;
import com.bokecc.livemodule.view.BaseRelativeLayout;
import com.bokecc.livemodule.view.gift.interfaces.OnClickBottomStatusListener;
import com.bokecc.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.pojo.Answer;
import com.bokecc.sdk.mobile.live.pojo.Question;

import org.json.JSONException;

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

/**
 * 直播间问答组件
 */
public class LiveQAComponent extends BaseRelativeLayout implements DWLiveQAListener, KeyboardHeightObserver {

    // 列表
    private RecyclerView mQaList;
    private QaAdapter mQaAdapter;
    private InputMethodManager mImm;
    int mQaInfoLength;
    private List<Question> questionList = new ArrayList<>();
    private List<Answer> answerList = new ArrayList<>();

    public LiveQAComponent(Context context) {
        super(context);
        initQaLayout();
    }

    public LiveQAComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initQaLayout();
    }

    public void initViews() {
        LayoutInflater.from(mContext).inflate(R.layout.live_portrait_qa_layout, this, true);
        mQaList = findViewById(R.id.rv_qa_container);


        mQaInfoLength = 0;

    }

    @SuppressLint("ClickableViewAccessibility")
    public void initQaLayout() {
        mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        mQaList.setLayoutManager(new LinearLayoutManager(mContext));
        mQaAdapter = new QaAdapter(mContext,false);
        mQaList.setAdapter(mQaAdapter);
        mQaList.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mImm.hideSoftInputFromWindow(mQaList.getWindowToken(), 0);
                return false;
            }


        });

        DWLiveCoreHandler dwLiveCoreHandler = DWLiveCoreHandler.getInstance();
        if (dwLiveCoreHandler != null) {
            dwLiveCoreHandler.setDwLiveQAListener(this);
        }
    }




    public void addQuestion(Question question) {
        questionList.add(question);
        refreshList();
    }

    public void showQuestion(String questionId) {
        for (int i = 0; i < questionList.size(); i++) {
            Question question = questionList.get(i);
            if (question != null && question.getId().equals(questionId)) {
                question.setIsPublish(1);
            }
        }
        refreshList();
    }

    public void addAnswer(Answer answer) {
        answerList.add(answer);
        refreshList();

    }

    //------------------------ 处理直播问答回调信息 ------------------------------------


    @Override
    public void onHistoryQuestionAnswer(final List<Question> questions, final List<Answer> answers) {

        mQaList.post(new Runnable() {
            @Override
            public void run() {
                //拼装QA数据

                questionList.clear();
                questionList.addAll(questions);
                answerList.clear();
                answerList.addAll(answers);
                refreshList();


            }
        });
    }

    private void refreshList() {
        for (Question question : questionList) {
            List<Answer> answerList = new ArrayList<>();
            for (Answer answer : this.answerList) {
                if (answer.getQuestionId().equals(question.getId())) {
                    question.setIsPublish(1);
                    answer.setQuestion(question);
                    answerList.add(answer);
                    question.setAnswerList(answerList);
                }
            }
        }
        mQaAdapter.setList(questionList);


        mQaAdapter.notifyDataSetChanged();
        if (mQaAdapter.getItemCount() > 1) {
            mQaList.scrollToPosition(mQaAdapter.getItemCount() - 1);
        }
    }

    @Override
    public void onQuestion(final Question question) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                addQuestion(question);
            }
        });
    }

    @Override
    public void onPublishQuestion(final String questionId) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showQuestion(questionId);
            }
        });
    }

    @Override
    public void onAnswer(final Answer answer) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                addAnswer(answer);
            }
        });
    }

    @Override
    public void onKeyboardHeightChanged(int height, int orientation) {
        if (height>5){
//            setTranslationY(-height);
        }else {
//            setTranslationY(0);
        }
    }

    private OnClickBottomStatusListener onClickBottomStatusListener;

    public void setOnClickBottomStatusListener(OnClickBottomStatusListener onClickBottomStatusListener) {
        this.onClickBottomStatusListener = onClickBottomStatusListener;
    }


    public void setOnlyShowSelf(boolean isSelf) {
        mQaAdapter.setOnlyShowSelf(isSelf);
    }
}

