package com.yidianling.im.session.viewholder;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yidianling.common.tools.ToastUtil;
import com.yidianling.im.R;
import com.yidianling.im.session.extension.CustomAttachLingxiWhichQuestion;
import com.yidianling.im.session.extension.CustomAttachmentOrderStatus;
import com.yidianling.im.session.viewholder.adapter.AdapterLingxiMultipleChoice;
import com.yidianling.im.session.viewholder.adapter.AdapterLingxiWhichQuestion;
import com.yidianling.im.session.viewholder.bean.LingxiWhichQuestionBean;
import com.yidianling.im.session.viewholder.bean.QuestionItemBean;
import com.yidianling.nimbase.common.ui.recyclerview.adapter.BaseMultiItemFetchLoadAdapter;
import com.yidianling.uikit.business.session.viewholder.MsgViewHolderBase;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class MsgViewHolderLingxiWhichQuestion extends MsgViewHolderBase implements AdapterLingxiWhichQuestion.OnQuestionlItemClickListener, AdapterLingxiMultipleChoice.OnMultiplelItemClickListener {
    private RecyclerView recycleviewWhichQuestion;
    private RecyclerView recycleviewMultipleChoice;
    private AdapterLingxiWhichQuestion adapterLingxiWhichQuestion;
    private AdapterLingxiMultipleChoice adapterLingxiMultipleChoice;

    ArrayList<QuestionItemBean> questionList = new ArrayList<QuestionItemBean>();
    Integer maxItemSize = 5;
    Boolean isSingleChoice=true;
    CustomAttachLingxiWhichQuestion customAttachLingxiWhichQuestion;
    public MsgViewHolderLingxiWhichQuestion(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int getContentResId() {
         customAttachLingxiWhichQuestion = ((CustomAttachLingxiWhichQuestion) message.getAttachment());
        switch (Integer.parseInt(customAttachLingxiWhichQuestion.selectType)){
            case 1://您想要咨询下列哪类的问题？（单选）
//                return R.layout.im_nim_lingxi_which_question;
                return R.layout.im_nim_lingxi_multiple_choice;
            case 2://请问是发生了什么事情导致的呢？（多选）
                return R.layout.im_nim_lingxi_multiple_choice;
            case 3://您是先生还是女生
                return R.layout.im_nim_lingxi_men_female;
            default:
                return R.layout.im_nim_lingxi_which_question;
        }
    }

    @Override
    protected void inflateContentView() {
        recycleviewWhichQuestion = findViewById(R.id.which_question_rv);
        recycleviewMultipleChoice = findViewById(R.id.multiple_question_rv);

    }

    @Override
    protected void bindContentView() {
        customAttachLingxiWhichQuestion = ((CustomAttachLingxiWhichQuestion) message.getAttachment());
        //您想要咨询下列哪类的问题？（单选）

//        adapterLingxiWhichQuestion = new AdapterLingxiWhichQuestion();
        for (int i=0;i<8;i++){
            questionList.add(new QuestionItemBean("1","婚姻情感试",false));
        }
        adapterLingxiWhichQuestion.setTagData(questionList);
        adapterLingxiWhichQuestion.setItemListener(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
        //当list中字段大于6个显示单行，否则显示双行
        gridLayoutManager.setSpanCount(maxItemSize > 6 ? 1 : 2);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        recycleviewWhichQuestion.setLayoutManager(gridLayoutManager);
        recycleviewWhichQuestion.setAdapter(adapterLingxiWhichQuestion);

        //请问是发生了什么事情导致的呢？（多选）
        adapterLingxiMultipleChoice = new AdapterLingxiMultipleChoice();
        adapterLingxiMultipleChoice.setTagData(questionList);
        adapterLingxiMultipleChoice.setItemListener(this);
        GridLayoutManager multipleChoiceManager = new GridLayoutManager(context, 2);
        //当list中字段大于6个显示单行，否则显示双行
        multipleChoiceManager.setSpanCount(maxItemSize > 6 ? 1 : 2);
        multipleChoiceManager.setOrientation(GridLayoutManager.VERTICAL);
        recycleviewMultipleChoice.setLayoutManager(multipleChoiceManager);
        recycleviewMultipleChoice.setAdapter(adapterLingxiMultipleChoice);

    }

    @Override
    public void onItemClick(@NotNull QuestionItemBean itemBean, int position) {
        for (int i=0;i<questionList.size();i++){
            questionList.get(i).setSelected(i==position);
        }
        adapterLingxiWhichQuestion.setTagData(questionList);
    }

    @Override
    public void onMultipleItemClick(@NotNull QuestionItemBean string, int position) {
        //多选发送消息
        for (int i=0;i<questionList.size();i++){
            if (questionList.get(i).isSelected()){
                if (i==position){
                    questionList.get(i).setSelected(false);
                }
            }else{
                if (i==position){
                    questionList.get(i).setSelected(true);
                }
            }
        }
        adapterLingxiMultipleChoice.setTagData(questionList);
    }
}
