package com.bokecc.livemodule.replay.qa;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.qa.adapter.QaAdapter;
import com.bokecc.livemodule.replay.DWReplayCoreHandler;
import com.bokecc.livemodule.replay.DWReplayQAListener;
import com.bokecc.sdk.mobile.live.pojo.Answer;
import com.bokecc.sdk.mobile.live.pojo.Question;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayAnswerMsg;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayQAMsg;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayQuestionMsg;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;

/**
 * 回放问答展示控件
 */
public class ReplayQAComponent extends RelativeLayout implements DWReplayQAListener {

    private Context mContext;
    private RecyclerView mQaList;
    private QaAdapter mQaAdapter;
    private List<Question> questionList = new ArrayList<>();
    private List<Answer> answerList = new ArrayList<>();

    int mQaInfoLength;

    public ReplayQAComponent(Context context) {
        super(context);
        mContext = context;
        initViews();
    }

    public ReplayQAComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initViews();
    }

    private void initViews() {
        LayoutInflater.from(mContext).inflate(R.layout.live_portrait_qa_layout, this, true);
        mQaList = findViewById(R.id.rv_qa_container);
        mQaInfoLength = 0;
        initQaLayout();
    }

    public void initQaLayout() {
        mQaList.setLayoutManager(new LinearLayoutManager(mContext));
        mQaAdapter = new QaAdapter(mContext,true);
        mQaList.setAdapter(mQaAdapter);

        // 设置监听
        DWReplayCoreHandler dwReplayCoreHandler = DWReplayCoreHandler.getInstance();
        if (dwReplayCoreHandler != null) {
            dwReplayCoreHandler.setReplayQAListener(this);
        }
    }


    public void addReplayQAInfos(List<Question> questionList) {
        mQaAdapter.setList(questionList);
    }

    @Override
    public void onQuestionAnswer(final TreeSet<ReplayQAMsg> qaMsgs) {
        for (ReplayQAMsg qaMsg : qaMsgs) {
            ReplayQuestionMsg questionMsg = qaMsg.getReplayQuestionMsg();

            if (0 == questionMsg.getIsPublish()) {
                //如果老师勾选仅提问者可见，除了自己的不添加
                if (DWReplayCoreHandler.getInstance().getViewer()!=null&&DWReplayCoreHandler.getInstance().getViewer().getId()!=null){
                    if (!DWReplayCoreHandler.getInstance().getViewer().getId().equals(questionMsg.getQuestionUserId())){
                        continue;
                    }
                }
            }

            Question question = new Question();
            int time = 0;
            long mills = 0L;
            //计算直播的绝对时间，赋值给millsTime
            try{
                time = Integer.parseInt(String.valueOf(questionMsg.getTime()))*1000;
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.HOUR_OF_DAY,0);
                calendar.set(Calendar.MINUTE,0);
                calendar.set(Calendar.SECOND,0);
                calendar.set(Calendar.MILLISECOND,0);
                calendar.add(Calendar.MILLISECOND,time);
                mills = calendar.getTime().getTime();
            }catch (Exception e){
                e.printStackTrace();
            }
            question.setContent(questionMsg.getContent())
                    .setId(questionMsg.getQuestionId())
                    .setQuestionUserId(questionMsg.getQuestionUserId())
                    .setQuestionUserName(questionMsg.getQuestionUserName())
                    .setMillsTime(mills)
                    .setIsPublish(questionMsg.getIsPublish())
                    .setExtra(questionMsg.getExtra())
                    .setUserAvatar(questionMsg.getQuestionUserAvatar());

            TreeSet<ReplayAnswerMsg> answerMsgs = qaMsg.getReplayAnswerMsgs();

            // 没有回答
            if (answerMsgs.size() < 1) {
                if (questionMsg.getIsPublish() == 0) {
                    // 未发布的问题
                    continue;
                } else if (questionMsg.getIsPublish() == 1) {
                    // 发布的问题
                    questionList.add(question);
                    continue;
                }
            }
            questionList.add(question);
            List<Answer> list = new ArrayList<>();
            // 回答过
//            QaInfo qaInfo = new QaInfo(question);
            for (ReplayAnswerMsg answerMsg : answerMsgs) {
                Answer answer = new Answer();
                //计算直播的绝对时间，赋值给millsTime
                int answerTime = 0;
                long answerMills = 0L;
                try{
                    answerTime = Integer.parseInt(String.valueOf(answerMsg.getTime()))*1000;
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.MILLISECOND,answerTime);
                    calendar.set(Calendar.HOUR_OF_DAY,0);
                    calendar.set(Calendar.MINUTE,0);
                    calendar.set(Calendar.SECOND,0);
                    calendar.set(Calendar.MILLISECOND,0);
                    answerMills = calendar.getTime().getTime();

                }catch (Exception e){
                    e.printStackTrace();
                }

                answer.setUserAvatar(answerMsg.getUserAvatar())
                        .setContent(answerMsg.getContent())
                        .setAnswerUserId(answerMsg.getUserId())
                        .setAnswerUserName(answerMsg.getUserName())
                        .setMillsTime(answerMills)
                        .setExtra(answerMsg.getExtra())
                        .setIsPrivate(answerMsg.getIsPrivate())
                        .setUserRole(answerMsg.getUserRole());
                answer.setQuestion(question);
                list.add(answer);
                answerList.add(answer);
            }
            question.setAnswerList(list);


        }


        mQaList.post(new Runnable() {
            @Override
            public void run() {
                addReplayQAInfos(questionList);
            }
        });
    }
}
