package com.bokecc.livemodule.live.qa.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bokecc.dwlivedemo.R;
import com.bokecc.dwlivedemo.activity.ImageDetailsActivity;
import com.bokecc.livemodule.utils.DensityUtil;
import com.bokecc.livemodule.view.WarpLinearLayout;
import com.bokecc.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.pojo.Answer;
import com.bokecc.sdk.mobile.live.pojo.Question;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 问答答案适配器
 */
public class QaInnerAdapter extends RecyclerView.Adapter<QaInnerAdapter.QaViewHolder> {


    private List<Answer> list = new ArrayList<>();
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Answer> sourceList = new ArrayList<>();

    //0折叠，1 展开
    private int state = 0;


    private SpannableStringBuilder ss = new SpannableStringBuilder();



    public QaInnerAdapter(Context context) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
    }


    @NonNull
    @Override
    public QaInnerAdapter.QaViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView;
        itemView = mInflater.inflate(R.layout.item_qa_answer, parent, false);
        return new QaInnerAdapter.QaViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(QaInnerAdapter.QaViewHolder holder, int position) {
        Answer answer = list.get(position);

        holder.qaSingleLayout.setVisibility(View.VISIBLE);
        String msg = answer.getAnswerUserName() + ": " + answer.getContent();
        ss.clear();
        ss.append(msg);
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#12ad1a")),
                0, answer.getAnswerUserName().length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#1E1F21")),
                answer.getAnswerUserName().length() + 1, msg.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.answerText.setText(ss);
        holder.llQaImgCon.removeAllViews();

        if (answer.getExtra() != null && answer.getExtra().getImg() != null) {
            showQaImg(holder, answer.getExtra().getImg());
        }
        holder.tvQaExpand.setVisibility(View.GONE);

        //判断当前状态，如果是折叠状态，list超过3条，需要在最后一条显示 展开提示语
        if (state==0){
            if (sourceList.size()>3&&position==list.size()-1){
                holder.tvQaExpand.setVisibility(View.VISIBLE);
                holder.tvQaExpand.setText("展开其他"+(sourceList.size()-3)+"条回复");
                Drawable drawable = mContext.getResources().getDrawable(R.drawable.icon_qa_arrow_down);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                holder.tvQaExpand.setCompoundDrawables(null,null,drawable,null);
            }
        }else {
            if (sourceList.size()>3&&position==list.size()-1){
                holder.tvQaExpand.setVisibility(View.VISIBLE);
                holder.tvQaExpand.setText("收起回复");
                Drawable drawable = mContext.getResources().getDrawable(R.drawable.icon_qa_arrow_up);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                holder.tvQaExpand.setCompoundDrawables(null,null,drawable,null);
            }
        }
        holder.tvQaExpand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state==0){
                    state = 1;
                    setState(state);
                }else {
                    state = 0;
                    setState(state);
                }
            }
        });

    }

    private void setState(int state){
        if (state==1){
            //展开状态
            list.clear();
            list.addAll(sourceList);
            notifyDataSetChanged();
        }else {
            list.clear();
            //收缩状态
            if (sourceList.size()<=3){
                list.addAll(sourceList);
                notifyDataSetChanged();
            }else {
                for (int i = 0; i <3; i++) {
                    list.add(sourceList.get(i));
                }
                notifyDataSetChanged();
            }
        }
    }

    public void setList(List<Answer> list){
        sourceList.clear();
        sourceList.addAll(list);
        //如果list的size大于3，默认处于收缩状态
        state = 0;
        setState(state);
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    private void showQaImg(QaInnerAdapter.QaViewHolder holder, List<Question.ExtraDTO.ImgDTO> imgUrlList) {
        Collections.sort(imgUrlList, new Comparator<Question.ExtraDTO.ImgDTO>() {
            @Override
            public int compare(Question.ExtraDTO.ImgDTO o1, Question.ExtraDTO.ImgDTO o2) {
                return o1.getOrder() - o2.getOrder();
            }
        });

        final List<String> urlList = new ArrayList<>();
        for (int i = 0; i < imgUrlList.size(); i++) {
            Question.ExtraDTO.ImgDTO imgDTO = imgUrlList.get(i);
            if (imgDTO != null) {
                urlList.add(imgDTO.getUrl());
                ImageView imageView = new ImageView(mContext);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                holder.llQaImgCon.addView(imageView);
                ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                if (imgUrlList.size() == 1) {
                    layoutParams.width = DensityUtil.dp2px(mContext, 180f);
                    layoutParams.height = DensityUtil.dp2px(mContext, 135f);
                } else {
                    layoutParams.width = DensityUtil.dp2px(mContext, 75f);
                    layoutParams.height = DensityUtil.dp2px(mContext, 75f);
                }

                Glide.with(mContext).load(imgDTO.getUrl()).into(imageView);
                final int index = i;
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mContext instanceof Activity) {
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < urlList.size(); i++) {
                                String url = urlList.get(i);

                                if (url != null && !TextUtils.isEmpty(url)) {
                                    sb.append(url);
                                    if (i < urlList.size() - 1) {
                                        sb.append("###");
                                    }
                                }
                            }

                            Intent intent = new Intent(mContext, ImageDetailsActivity.class);
                            intent.putExtra("imageUrl", sb.toString());
                            intent.putExtra("hasDelete", false);
                            intent.putExtra("index", index);
                            ((Activity) mContext).startActivity(intent);
                        }

                    }
                });
            }
        }

    }


    public static class QaViewHolder extends RecyclerView.ViewHolder {

        TextView tvQaExpand;
        WarpLinearLayout llQaImgCon;
        TextView answerText;
        LinearLayout qaSingleLayout;

        public QaViewHolder(@NonNull View itemView) {
            super(itemView);
            answerText = itemView.findViewById(R.id.qa_answer);
            qaSingleLayout = itemView.findViewById(R.id.ll_qa_single_layout);
            llQaImgCon = itemView.findViewById(R.id.ll_qa_img_con);
            tvQaExpand = itemView.findViewById(R.id.tv_qa_expand);
        }
    }
}
