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

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bokecc.room.ui.R;
import com.bokecc.room.ui.model.NamedTime;
import com.bokecc.sskt.base.CCAtlasClient;
import com.bokecc.sskt.base.bean.CCQuestionAnswerBean;
import com.bokecc.sskt.base.bean.CCQuestionBean;

import java.util.ArrayList;


/**
 * 作者 ${CC视频}.<br/>
 */

public class QuestionListAdapter extends BaseRecycleAdapter<QuestionListAdapter.QuestionListViewHolder, CCQuestionBean> {
    public static final int NAME_MAX_COUNT = 6;

    private QuestionListClickListener listClickListener;

    public QuestionListAdapter(Context context,QuestionListClickListener listClickListener) {
        super(context);
        this.listClickListener = listClickListener;
    }

    @Override
    public void onBindViewHolder(QuestionListViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        if (CCAtlasClient.getInstance().getRole() == CCAtlasClient.PRESENTER
                || CCAtlasClient.getInstance().getRole() == CCAtlasClient.ASSISTANT){
            holder.divider.setVisibility(View.VISIBLE);
            holder.questionFunction.setVisibility(View.VISIBLE);
        } else if (CCAtlasClient.getInstance().getRole() == CCAtlasClient.TALKER
                || CCAtlasClient.getInstance().getRole() == CCAtlasClient.AUDITOR){
            holder.divider.setVisibility(View.GONE);
            holder.questionFunction.setVisibility(View.GONE);
        }

        final CCQuestionBean questionBean = mDatas.get(position);
        String name = questionBean.getQuestionUser().getName();
        if (name.length() > NAME_MAX_COUNT){
            name = name.substring(0,NAME_MAX_COUNT) + "...";
        }
        if ((CCAtlasClient.getInstance().getRole() == CCAtlasClient.TALKER|| CCAtlasClient.getInstance().getRole() == CCAtlasClient.AUDITOR)
                && questionBean.getQuestionUser().getId().equals(CCAtlasClient.getInstance().getUserIdInPusher())){
            holder.question_user_name.setText(name + "(我)");
        } else {
            holder.question_user_name.setText(name);
        }

        holder.question_time.setText(questionBean.getTime());

        if (questionBean.getPublish() == 0){
            holder.tv_answer_public.setTextColor(Color.parseColor("#FFFF9502"));
            holder.tv_answer_public.setText("公开");
            holder.tv_answer_public.setClickable(true);
        } else {
            holder.tv_answer_public.setClickable(false);
            holder.tv_answer_public.setTextColor(Color.parseColor("#A6FFFFFF"));
            holder.tv_answer_public.setText("已公开");
        }

        holder.tv_question.setText(questionBean.getContent());
        holder.tv_answer_question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listClickListener.clickAnswer(position,questionBean);
            }
        });

        holder.tv_answer_public.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (questionBean.getPublish() == 0) {
                    listClickListener.clickPublic(position, questionBean);
                }
            }
        });


        ArrayList<CCQuestionAnswerBean> answerBeans = questionBean.getAnswerBeans();
        ArrayList<CCQuestionAnswerBean> showAnswerBeans = new ArrayList<>();
        if (CCAtlasClient.getInstance().getRole() == CCAtlasClient.TALKER || CCAtlasClient.getInstance().getRole() == CCAtlasClient.AUDITOR) {
            for (int i = 0; i < answerBeans.size(); i++) {
                if (answerBeans.get(i).getIsPrivate() == 0 || questionBean.getQuestionUser().getId().equals(CCAtlasClient.getInstance().getUserIdInPusher())) {
                    showAnswerBeans.add(answerBeans.get(i));
                }
            }
        } else {
            showAnswerBeans.addAll(answerBeans);
        }

        if (showAnswerBeans.size() == 0){
            holder.answer_layout.setVisibility(View.GONE);
        } else {
            holder.answer_layout.setVisibility(View.VISIBLE);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        holder.rv_answer_list.setLayoutManager(layoutManager);

        QuestionAnswerListAdapter questionAnswerListAdapter = new QuestionAnswerListAdapter(mContext);
        questionAnswerListAdapter.bindDatas(showAnswerBeans);
        holder.rv_answer_list.setAdapter(questionAnswerListAdapter);

    }

    @Override
    public int getItemView(int viewType) {
        return R.layout.cc_item_question_list;
    }

    @Override
    public QuestionListViewHolder getViewHolder(View itemView, int viewType) {
        return new QuestionListViewHolder(itemView);
    }

    static class QuestionListViewHolder extends BaseRecycleAdapter.BaseViewHolder {
        TextView question_user_name;
        TextView question_time;
        TextView tv_question;
        TextView tv_answer_question;
        TextView tv_answer_public;
        RecyclerView rv_answer_list;

        View divider;
        View questionFunction;
        View answer_layout;

        QuestionListViewHolder(View itemView) {
            super(itemView);
            question_user_name = itemView.findViewById(R.id.question_user_name);
            question_time = itemView.findViewById(R.id.question_time);
            tv_question = itemView.findViewById(R.id.tv_question);
            tv_answer_question = itemView.findViewById(R.id.tv_answer_question);
            tv_answer_public = itemView.findViewById(R.id.tv_answer_public);
            rv_answer_list = itemView.findViewById(R.id.rv_answer_list);

            divider = itemView.findViewById(R.id.question_divider);
            questionFunction = itemView.findViewById(R.id.question_function);
            answer_layout = itemView.findViewById(R.id.answer_layout);
        }
    }

    public interface QuestionListClickListener{
        void clickAnswer(int position,CCQuestionBean questionBean);
        void clickPublic(int position,CCQuestionBean questionBean);
    }

}
