package io.openim.android.ouimoments.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.xiaofeng.flowlayoutmanager.Alignment;
import com.xiaofeng.flowlayoutmanager.FlowLayoutManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.openim.android.ouicore.adapter.RecyclerViewAdapter;
import io.openim.android.ouicore.adapter.ViewHol;
import io.openim.android.ouicore.base.BaseApp;
import io.openim.android.ouicore.base.vm.injection.Easy;
import io.openim.android.ouicore.utils.Common;
import io.openim.android.ouicore.utils.Constants;
import io.openim.android.ouicore.utils.OnDedrepClickListener;
import io.openim.android.ouicore.utils.Routes;
import io.openim.android.ouicore.utils.TimeUtil;
import io.openim.android.ouicore.vm.PreviewMediaVM;
import io.openim.android.ouicore.widget.AvatarImage;
import io.openim.android.ouicore.widget.CommonDialog;
import io.openim.android.ouimoments.R;
import io.openim.android.ouimoments.adapter.viewholder.CircleViewHolder;
import io.openim.android.ouimoments.adapter.viewholder.ImageViewHolder;
import io.openim.android.ouimoments.adapter.viewholder.TargetMomentsViewHolder;
import io.openim.android.ouimoments.adapter.viewholder.URLViewHolder;
import io.openim.android.ouimoments.adapter.viewholder.VideoViewHolder;
import io.openim.android.ouimoments.bean.ActionItem;
import io.openim.android.ouimoments.bean.CircleItem;
import io.openim.android.ouimoments.bean.CommentConfig;
import io.openim.android.ouimoments.bean.CommentItem;
import io.openim.android.ouimoments.bean.FavortItem;
import io.openim.android.ouimoments.bean.PhotoInfo;
import io.openim.android.ouimoments.mvp.presenter.CirclePresenter;
import io.openim.android.ouimoments.mvp.presenter.MsgDetailVM;
import io.openim.android.ouimoments.ui.ImagePagerActivity;
import io.openim.android.ouimoments.ui.ImagePagerPersonActivity;
import io.openim.android.ouimoments.ui.MomentsDetailActivity;
import io.openim.android.ouimoments.ui.MsgDetailActivity;
import io.openim.android.ouimoments.ui.PartSeeActivity;
import io.openim.android.ouimoments.ui.ToUserMomentsActivity;
import io.openim.android.ouimoments.utils.DatasUtil;
import io.openim.android.ouimoments.utils.UrlUtils;
import io.openim.android.ouimoments.widgets.CircleVideoView;
import io.openim.android.ouimoments.widgets.CommentListView;
import io.openim.android.ouimoments.widgets.ExpandTextView;
import io.openim.android.ouimoments.widgets.MultiImageView;
import io.openim.android.ouimoments.widgets.PraiseListView;
import io.openim.android.ouimoments.widgets.SnsPopupWindow;
import io.openim.android.ouimoments.widgets.dialog.CommentDialog;

public class CircleAdapter extends BaseRecycleViewAdapter {

    public final static int TYPE_HEAD = 0;

    private static final int STATE_IDLE = 0;
    private static final int STATE_ACTIVED = 1;
    private static final int STATE_DEACTIVED = 2;
    private int videoState = STATE_IDLE;
    public int HEADVIEW_SIZE = 1;

    int curPlayIndex = -1;

    private CirclePresenter presenter;
    private Context context;
    public String lastYear, lastMd;

    public void setCirclePresenter(CirclePresenter presenter) {
        this.presenter = presenter;
    }

    public CircleAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        }

        int itemType = 0;
        CircleItem item = (CircleItem) datas.get(position - HEADVIEW_SIZE);
        if (CircleItem.TYPE_URL.equals(item.getType())) {
            itemType = CircleViewHolder.TYPE_URL;
        } else if (CircleItem.TYPE_IMG.equals(item.getType())) {
            itemType = CircleViewHolder.TYPE_IMAGE;
        } else if (CircleItem.TYPE_VIDEO.equals(item.getType())) {
            itemType = CircleViewHolder.TYPE_VIDEO;
        }
        return itemType;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (viewType == TYPE_HEAD) {
            View headView = LayoutInflater.from(parent.getContext()).inflate(R.layout.head_circle, parent, false);
            viewHolder = new HeaderViewHolder(headView);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_circle_item, parent, false);

            if (presenter.isSpecifiedUser()) {
                //创建指定用户的朋友圈视图
                return new TargetMomentsViewHolder(view);
            }

            if (viewType == CircleViewHolder.TYPE_URL) {
                viewHolder = new URLViewHolder(view);
            } else if (viewType == CircleViewHolder.TYPE_IMAGE) {
                viewHolder = new ImageViewHolder(view);
            } else if (viewType == CircleViewHolder.TYPE_VIDEO) {
                viewHolder = new VideoViewHolder(view);
            }
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (getItemViewType(position) == TYPE_HEAD) {
            try {
                HeaderViewHolder holder = (HeaderViewHolder) viewHolder;
                if (presenter.isSpecifiedUser()) {
                    holder.headIv.load(presenter.user.getHeadUrl(),presenter.user.getName());
                    if (presenter.user.getId().equals(BaseApp.inst().loginCertificate.userID))
                        holder.nameBottomTv.setText(BaseApp.inst().loginCertificate.nickname);
                    else holder.nameBottomTv.setText(presenter.user.getName());
                    holder.nameTv.setVisibility(View.GONE);
                } else {
                    holder.nameTv.setVisibility(View.VISIBLE);
                    holder.headIv.load(BaseApp.inst().loginCertificate.faceURL,BaseApp.inst().loginCertificate.nickname);
                    holder.nameTv.setText(BaseApp.inst().loginCertificate.nickname);
                    holder.headIv.setOnClickListener(v -> {
                        context.startActivity(new Intent(context, ToUserMomentsActivity.class).putExtra(Constants.K_RESULT, DatasUtil.curUser));
                    });

                    holder.newMsgTips.setVisibility(TextUtils.isEmpty(presenter.unReadCount) ? View.GONE : View.VISIBLE);
                    holder.newMsgTips.setText(String.format(context.getString(io.openim.android.ouicore.R.string.new_msg_tips), presenter.unReadCount));
                    holder.newMsgTips.setOnClickListener(v -> {
                        presenter.unReadCount = null;
                        holder.newMsgTips.setVisibility(View.GONE);
                        //TODO JUMP
                        new MsgDetailVM().clearMsg(MsgDetailVM.clear_unread_num);
                        context.startActivity(new Intent(context, MsgDetailActivity.class));
                    });
                }

            } catch (Exception ignored) {
            }
        } else {
            final int circlePosition = position - HEADVIEW_SIZE;
            final CircleItem circleItem = (CircleItem) datas.get(circlePosition);
            if (presenter.isSpecifiedUser()) {
                //绑定指定用户的朋友圈视图的数据
                bindTargetMomentsView((TargetMomentsViewHolder) viewHolder, circlePosition, circleItem);
                return;
            }

            final CircleViewHolder holder = (CircleViewHolder) viewHolder;
            final String circleId = circleItem.getId();
            String name = circleItem.getUser().getName();
            String headImg = circleItem.getUser().getHeadUrl();
            final String content = circleItem.getContent();
            String createTime = circleItem.getCreateTime();
            final List<FavortItem> favortDatas = circleItem.getFavorters();
            final List<CommentItem> commentsDatas = circleItem.getComments();
            boolean hasFavort = circleItem.hasFavort();
            boolean hasComment = circleItem.hasComment();

            LinearLayout.LayoutParams headIVParams = (LinearLayout.LayoutParams) holder.headIv.getLayoutParams();
            headIVParams.width = presenter.isSpecifiedMoment() ? ConvertUtils.dp2px(44) : ConvertUtils.dp2px(38);
            headIVParams.height = presenter.isSpecifiedMoment() ? ConvertUtils.dp2px(44) : ConvertUtils.dp2px(38);
            holder.headIv.setLayoutParams(headIVParams);
            holder.headIv.load(headImg,name);

            if (!presenter.isSpecifiedUser()) holder.headIv.setOnClickListener(v -> {
                context.startActivity(new Intent(context, ToUserMomentsActivity.class).putExtra(Constants.K_RESULT, circleItem.getUser()));
            });
            holder.nameTv.setText(name);
            holder.timeTv.setTextColor(presenter.isSpecifiedMoment() ? Color.parseColor("#282828") : Color.parseColor("#737373"));
            long time = TimeUtils.string2Millis(createTime, "yyyy/MM/dd HH:mm");
            holder.timeTv.setText(TimeUtils.getFriendlyTimeSpanByNow(time));

            if (!TextUtils.isEmpty(content)) {
                holder.contentTv.setExpand(circleItem.isExpand());
                holder.contentTv.setExpandStatusListener(new ExpandTextView.ExpandStatusListener() {
                    @Override
                    public void statusChange(boolean isExpand) {
                        circleItem.setExpand(isExpand);
                    }
                });

                holder.contentTv.setText(UrlUtils.formatUrlString(content));
            }
            holder.contentTv.setVisibility(TextUtils.isEmpty(content) ? View.GONE : View.VISIBLE);
            holder.authorityLy.setVisibility(View.GONE);
            if (DatasUtil.curUser.getId().equals(circleItem.getUser().getId())) {
                holder.deleteBtn.setVisibility(View.VISIBLE);
                if (circleItem.getPermission() == 1) {
                    holder.authorityLy.setVisibility(View.VISIBLE);
                    holder.authorityLy.setOnClickListener(null);
                    holder.authorityIv.setImageResource(R.mipmap.ic_m_lock);
                    holder.authorityTv.setText(io.openim.android.ouicore.R.string.str_private_tips);
                }
                if (circleItem.getPermission() == 2 || circleItem.getPermission() == 3) {
                    holder.authorityLy.setVisibility(View.VISIBLE);
                    holder.authorityIv.setImageResource(R.mipmap.ic_m_friends);
                    holder.authorityTv.setText(io.openim.android.ouicore.R.string.part_see_tips2);
                    holder.authorityLy.setOnClickListener(v -> {
                        context.startActivity(new Intent(context, PartSeeActivity.class).putExtra(Constants.K_NAME, circleItem.getPermission() == 2 ? this.context.getString(io.openim.android.ouicore.R.string.part_see_tips2) : this.context.getString(io.openim.android.ouicore.R.string.who_invisible)).putExtra(Constants.K_RESULT, (Serializable) circleItem.getPermissionUsers()));
                    });
                }
            } else {
                holder.authorityLy.setVisibility(View.GONE);
                holder.deleteBtn.setVisibility(View.GONE);
            }
            holder.aboutWhoTv.setVisibility(TextUtils.isEmpty(circleItem.getAtUsers()) ? View.GONE : View.VISIBLE);
            holder.aboutWhoTv.setText(String.format(this.context.getString(io.openim.android.ouicore.R.string.about_who), circleItem.getAtUsers()));

            holder.deleteBtn.setOnClickListener(v -> {
                CommonDialog commonDialog = new CommonDialog(context).atShow();
                commonDialog.getMainView().tips.setText(io.openim.android.ouicore.R.string.delete_moments_tips);
                commonDialog.getMainView().cancel.setOnClickListener(v1 -> commonDialog.dismiss());
                commonDialog.getMainView().confirm.setOnClickListener(v1 -> {
                    //删除
                    if (presenter != null) {
                        presenter.deleteCircle(circleId);
                        commonDialog.dismiss();
                    }
                });
            });
            if (hasFavort || hasComment) {
                if (hasFavort) {//处理点赞列表
                    holder.llPraise.setVisibility(View.VISIBLE);
                    if (!presenter.isSpecifiedMoment()) {
                        holder.praiseListView.setOnItemClickListener(new PraiseListView.OnItemClickListener() {
                            @Override
                            public void onClick(int position) {
                                String userName = favortDatas.get(position).getUser().getName();
                                String userId = favortDatas.get(position).getUser().getId();

                                ARouter.getInstance().build(Routes.Main.PERSON_DETAIL).withString(Constants.K_ID, userId).navigation();
                            }
                        });
                        holder.praiseListView.setVisibility(View.VISIBLE);
                        holder.praiseListView.setDatas(favortDatas);
                        holder.praiseRlv.setVisibility(View.GONE);
                        holder.ivPraise.setVisibility(View.GONE);
                    } else {
                        FlowLayoutManager flowLayoutManager = new FlowLayoutManager();
                        flowLayoutManager.setAutoMeasureEnabled(true);
                        flowLayoutManager.setAlignment(Alignment.LEFT);
                        holder.praiseRlv.setLayoutManager(flowLayoutManager);
                        PraiseRlvAdapter praiseRlvAdapter = new PraiseRlvAdapter();
                        praiseRlvAdapter.setDatas(favortDatas);
                        holder.praiseRlv.setAdapter(praiseRlvAdapter);
                        holder.praiseListView.setVisibility(View.GONE);
                        holder.praiseRlv.setVisibility(View.VISIBLE);
                        holder.ivPraise.setVisibility(View.VISIBLE);
                    }

                } else {
                    holder.praiseListView.setVisibility(View.GONE);
                    holder.praiseRlv.setVisibility(View.GONE);
                    holder.llPraise.setVisibility(View.GONE);
                    holder.ivPraise.setVisibility(View.GONE);
                }

                if (hasComment) {//处理评论列表
                    holder.llComment.setVisibility(View.VISIBLE);
                    holder.commentList.setOnItemClickListener(new CommentListView.OnItemClickListener() {
                        @Override
                        public void onItemClick(int commentPosition) {
                            CommentItem commentItem = commentsDatas.get(commentPosition);
                            if (DatasUtil.curUser.getId().equals(commentItem.getUser().getId())) {//复制或者删除自己的评论
//                                CommentDialog dialog = new CommentDialog(context, presenter, commentItem, circleItem.getId(), circlePosition);
//                                dialog.show();
                                if (presenter != null) {
                                    CommentConfig config = new CommentConfig();
                                    config.momentID = circleItem.getId();
                                    config.circlePosition = circlePosition;
                                    config.commentPosition = commentPosition;
                                    config.commentType = CommentConfig.Type.REPLY;
                                    config.replyUser = commentItem.getUser();
                                    presenter.showEditTextBody(config);
                                }
                            } else {//回复别人的评论
                                if (presenter != null) {
                                    CommentConfig config = new CommentConfig();
                                    config.momentID = circleItem.getId();
                                    config.circlePosition = circlePosition;
                                    config.commentPosition = commentPosition;
                                    config.commentType = CommentConfig.Type.REPLY;
                                    config.replyUser = commentItem.getUser();
                                    presenter.showEditTextBody(config);
                                }
                            }
                        }
                    });
                    holder.commentList.setOnItemLongClickListener(new CommentListView.OnItemLongClickListener() {
                        @Override
                        public void onItemLongClick(int commentPosition) {
                            //长按进行复制或者删除
                            CommentItem commentItem = commentsDatas.get(commentPosition);
                            CommentDialog dialog = new CommentDialog(context, presenter, commentItem, circleItem.getId(), circlePosition);
                            dialog.show();
                        }
                    });
                    holder.commentList.setDatas(commentsDatas, presenter.isSpecifiedMoment());
                    if (presenter.isSpecifiedMoment()) {
                        holder.ivComment.setVisibility(View.VISIBLE);
                        holder.llComment.setBackgroundResource(R.drawable.sty_radius_4_f7f7f7);
                    } else {
                        holder.ivComment.setVisibility(View.GONE);
                        holder.llComment.setBackgroundColor(Color.TRANSPARENT);
                    }
                    holder.commentList.setVisibility(View.VISIBLE);


                } else {
                    holder.commentList.setVisibility(View.GONE);
                    holder.llComment.setVisibility(View.GONE);
                    holder.ivComment.setVisibility(View.GONE);
                    holder.llComment.setBackgroundColor(Color.TRANSPARENT);
                }
                if (!presenter.isSpecifiedMoment()) {
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.digCommentBody.getLayoutParams();
                    layoutParams.setMargins(ConvertUtils.dp2px(63), 0, ConvertUtils.dp2px(12), 0);
                    holder.digCommentBody.setLayoutParams(layoutParams);
                } else {
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.setMargins(ConvertUtils.dp2px(12), 0, ConvertUtils.dp2px(12), 0);
                    holder.digCommentBody.setLayoutParams(layoutParams);
                    holder.digCommentBody.setBackgroundColor(Color.TRANSPARENT);
                    holder.llPraise.setBackgroundResource(R.drawable.sty_radius_4_f7f7f7);
                    holder.digLine.setBackgroundColor(Color.WHITE);
                    holder.ivPraise.setVisibility(View.VISIBLE);
                }
                holder.digCommentBody.setVisibility(View.VISIBLE);

            } else {
                holder.digCommentBody.setVisibility(View.GONE);
            }

            holder.digLine.setVisibility(hasFavort && hasComment ? View.VISIBLE : View.GONE);

            final SnsPopupWindow snsPopupWindow = holder.snsPopupWindow;
            //判断是否已点赞
            String curUserFavortId = circleItem.getCurUserFavortId(BaseApp.inst().loginCertificate.userID);
            if (!TextUtils.isEmpty(curUserFavortId)) {
                snsPopupWindow.getmActionItems().get(0).mTitle = context.getString(io.openim.android.ouicore.R.string.cancel);
            } else {
                snsPopupWindow.getmActionItems().get(0).mTitle = context.getString(io.openim.android.ouicore.R.string.star);
            }
            snsPopupWindow.update();
            snsPopupWindow.setmItemClickListener(new PopupItemClickListener(circlePosition, circleItem, circleItem.getId()));
            holder.snsBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //弹出popupwindow
                    snsPopupWindow.showPopupWindow(view);
                }
            });

            holder.urlTipTv.setVisibility(View.GONE);
            switch (holder.viewType) {
                case CircleViewHolder.TYPE_URL:// 处理链接动态的链接内容和和图片
                    if (holder instanceof URLViewHolder) {
                        String linkImg = circleItem.getLinkImg();
                        String linkTitle = circleItem.getLinkTitle();
                        Glide.with(context).load(linkImg).into(((URLViewHolder) holder).urlImageIv);
                        ((URLViewHolder) holder).urlContentTv.setText(linkTitle);
                        ((URLViewHolder) holder).urlBody.setVisibility(View.VISIBLE);
                        ((URLViewHolder) holder).urlTipTv.setVisibility(View.VISIBLE);
                    }

                    break;
                case CircleViewHolder.TYPE_IMAGE:// 处理图片
                    if (holder instanceof ImageViewHolder) {
                        final List<PhotoInfo> photos = circleItem.getPhotos();
                        if (photos != null && photos.size() > 0) {
                            ((ImageViewHolder) holder).multiImageView.setVisibility(View.VISIBLE);
                            ((ImageViewHolder) holder).multiImageView.setList(photos);
                            ((ImageViewHolder) holder).multiImageView.setOnItemClickListener(new MultiImageView.OnItemClickListener() {
                                @Override
                                public void onItemClick(View view, int position) {
                                    //imagesize是作为loading时的图片size
                                    ImagePagerActivity.ImageSize imageSize = new ImagePagerActivity.ImageSize(view.getMeasuredWidth(), view.getMeasuredHeight());

                                    List<String> photoUrls = new ArrayList<String>();
                                    for (PhotoInfo photoInfo : photos) {
                                        photoUrls.add(photoInfo.url);
                                    }
                                    ImagePagerActivity.startImagePagerActivity(context, photoUrls, position, imageSize);


                                }
                            });
                        } else {
                            ((ImageViewHolder) holder).multiImageView.setVisibility(View.GONE);
                        }
                    }

                    break;
                case CircleViewHolder.TYPE_VIDEO:
                    if (holder instanceof VideoViewHolder) {
                        ((VideoViewHolder) holder).videoView.setVideoUrl(circleItem.getVideoUrl());
                        ((VideoViewHolder) holder).videoView.setVideoImgUrl(circleItem.getVideoImgUrl());//视频封面图片
                        ((VideoViewHolder) holder).videoView.setPostion(position);
                        ((VideoViewHolder) holder).videoView.setOnPlayClickListener(new CircleVideoView.OnPlayClickListener() {
                            @Override
                            public void onPlayClick(int pos) {
                                curPlayIndex = pos;
                            }
                        });
                    }

                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 绑定指定用户的朋友圈视图的数据
     */
    private void bindTargetMomentsView(TargetMomentsViewHolder holder, int circlePosition, CircleItem circleItem) {
        holder.view.getRoot().setOnClickListener(v -> {
            context.startActivity(new Intent(context, MomentsDetailActivity.class).putExtra(Constants.K_ID, circleItem.getId()));
        });

        holder.view.single.setOnClickListener(new OnDedrepClickListener() {
            @Override
            public void click(View v) {
                if (Objects.equals(circleItem.getType(), CircleItem.TYPE_IMG)) {
                    ImagePagerPersonActivity.startImagePagerActivity(context, circleItem, circlePosition);
                } else {
                    String videoUrl = circleItem.getVideoUrl();
                    String imgUrl = circleItem.getVideoImgUrl();
                    if (TextUtils.isEmpty(videoUrl)) {
                        ToastUtils.showShort("video url is empty...");
                        return;
                    }
                    PreviewMediaVM mediaVM = Easy.installVM(PreviewMediaVM.class);
                    PreviewMediaVM.MediaData data = new PreviewMediaVM.MediaData(videoUrl);
                    data.thumbnail = imgUrl;
                    data.mediaUrl = videoUrl;
                    data.isVideo = true;
                    mediaVM.previewSingle(data);
                    ARouter.getInstance().build(Routes.Conversation.PREVIEW).navigation();
                }
            }
        });
        int index = circlePosition - 1;
        if (index < 0) index = 0;
        boolean isSameDate = index != 0 && ((CircleItem) datas.get(index)).getCreateTime().equals(circleItem.getCreateTime());
        int i10 = Common.dp2px(10);
        int i30 = Common.dp2px(30);
        int i15 = Common.dp2px(15);
        if (index == 0) {
            holder.view.getRoot().setPadding(i10, i30, i10, 0);
        } else {
            holder.view.getRoot().setPadding(i10, isSameDate ? i15 : i30, i10, circlePosition == datas.size() - 1 ? i30 : 0);
        }
        holder.view.yearLy.setVisibility(View.GONE);
        String year = TimeUtil.getTime(circleItem.getCreateTimeL(), "yyyy");
        String localDate = TimeUtil.getTime(System.currentTimeMillis(), "yyyy");
        if (!localDate.equals(year) && !year.equals(lastYear)) {
            holder.view.yearLy.setVisibility(View.VISIBLE);
            holder.view.year.setText(year);
            lastYear = year;
        }
        long date = circleItem.getCreateTimeL();
        boolean isToday = TimeUtils.isToday(date);
        boolean isYesterday = TimeUtils.getTimeSpan(TimeUtils.getNowMills(), date, TimeConstants.DAY) == 1;
        String day = TimeUtil.getTime(date, "dd");
        String month = TimeUtil.getTime(date, "MM月");
        String md = day + month;
        holder.view.time.setVisibility(View.INVISIBLE);
        if (isToday) {
            day = context.getString(io.openim.android.ouicore.R.string.today);
            month = "";
        }
        if (isYesterday) {
            day = context.getString(io.openim.android.ouicore.R.string.yesterday);
            month = "";
        }
        SpanUtils.with(holder.view.time)
                .append(day).setBold()
                .append(month).setFontSize(13, true).create();
//        if (!md.equals(lastMd)) {
        holder.view.time.setVisibility(View.VISIBLE);
        lastMd = md;
//        }

        holder.view.content.setText(circleItem.getContent());

        if (Objects.equals(circleItem.getType(), CircleItem.TYPE_IMG)) {
            holder.view.isVideo.setVisibility(View.GONE);
            int photoNum = circleItem.getPhotos().size();

            if (photoNum == 0) {
                holder.view.media.setVisibility(View.GONE);
                holder.view.single.setVisibility(View.GONE);
                holder.view.photoNum.setVisibility(View.GONE);
                holder.view.content.setBackgroundColor(context.getResources().getColor(io.openim.android.ouicore.R.color.theme_bg2));

            } else if (photoNum == 1) {
                holder.view.single.setVisibility(View.VISIBLE);
                holder.view.media.setVisibility(View.GONE);
                holder.view.photoNum.setVisibility(View.GONE);

                Glide.with(context).load(circleItem.getPhotos().get(0).url).placeholder(io.openim.android.ouicore.R.mipmap.ic_chat_photo).error(io.openim.android.ouicore.R.mipmap.ic_chat_photo).centerCrop().into(holder.view.icon);
            } else {
                holder.view.media.setVisibility(View.VISIBLE);
                holder.view.single.setVisibility(View.GONE);
                holder.view.photoNum.setVisibility(View.VISIBLE);
                holder.view.photoNum.setText(String.format(context.getString(io.openim.android.ouicore.R.string.tal_num_photo), photoNum));
                holder.view.media.setLayoutManager(new GridLayoutManager(context, 2));
                RecyclerViewAdapter<PhotoInfo, ViewHol.ImageViewHolder> adapter;
                holder.view.media.setAdapter(adapter = new RecyclerViewAdapter<PhotoInfo, ViewHol.ImageViewHolder>(ViewHol.ImageViewHolder.class) {

                    @Override
                    public void onBindView(@NonNull ViewHol.ImageViewHolder holder, PhotoInfo url, int position) {
                        holder.view.getRoot().getLayoutParams().height = (photoNum > 2) ? Common.dp2px(38) : -1;
                        holder.view.getRoot().setOnClickListener(new OnDedrepClickListener() {
                            @Override
                            public void click(View v) {
                                ImagePagerPersonActivity.startImagePagerActivity(context, circleItem, circlePosition);
                            }
                        });
                        Glide.with(context).load(url.url).placeholder(io.openim.android.ouicore.R.mipmap.ic_chat_photo).error(io.openim.android.ouicore.R.mipmap.ic_chat_photo).centerCrop().into(holder.view.getRoot());
                    }
                });
                adapter.setItems(circleItem.getPhotos().subList(0, Math.min(photoNum, 4)));
            }
        } else if (Objects.equals(circleItem.getType(), CircleItem.TYPE_VIDEO)) {
            holder.view.single.setVisibility(View.VISIBLE);
            holder.view.isVideo.setVisibility(View.VISIBLE);
            holder.view.media.setVisibility(View.GONE);
            holder.view.photoNum.setVisibility(View.GONE);

            Glide.with(context).load(circleItem.getVideoImgUrl()).placeholder(io.openim.android.ouicore.R.mipmap.ic_chat_photo).error(io.openim.android.ouicore.R.mipmap.ic_chat_photo).centerCrop().into(holder.view.icon);
        } else {
            holder.view.media.setVisibility(View.GONE);
            holder.view.single.setVisibility(View.GONE);
        }
    }


    @Override
    public int getItemCount() {
        return datas.size() + 1;//有head需要加1
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder {
        public final AvatarImage headIv;
        public final TextView nameTv, nameBottomTv, newMsgTips;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            headIv = itemView.findViewById(R.id.headIv);
            nameTv = itemView.findViewById(R.id.name);
            nameBottomTv = itemView.findViewById(R.id.nameBottom);
            newMsgTips = itemView.findViewById(R.id.newMsgTips);
        }
    }

    private class PopupItemClickListener implements SnsPopupWindow.OnItemClickListener {
        private String momentID;
        //动态在列表中的位置
        private int mCirclePosition;
        private long mLasttime = 0;
        private CircleItem mCircleItem;

        public PopupItemClickListener(int circlePosition, CircleItem circleItem, String favorId) {
            this.momentID = favorId;
            this.mCirclePosition = circlePosition;
            this.mCircleItem = circleItem;
        }

        @Override
        public void onItemClick(ActionItem actionitem, int position) {
            switch (position) {
                case 0://点赞、取消点赞
                    if (System.currentTimeMillis() - mLasttime < 700)//防止快速点击操作
                        return;
                    mLasttime = System.currentTimeMillis();
                    if (presenter != null) {
                        if (BaseApp.inst().getString(io.openim.android.ouicore.R.string.star).equals(actionitem.mTitle.toString())) {
                            presenter.addFavort(mCirclePosition, momentID);
                        } else {//取消点赞
                            presenter.deleteFavort(mCirclePosition, momentID);
                        }
                    }
                    break;
                case 1://发布评论
                    if (presenter != null) {
                        CommentConfig config = new CommentConfig();
                        config.momentID = momentID;
                        config.circlePosition = mCirclePosition;
                        config.commentType = CommentConfig.Type.PUBLIC;
                        presenter.showEditTextBody(config);
                    }
                    break;
                default:
                    break;
            }
        }
    }


    private class PraiseRlvAdapter extends BaseRecycleViewAdapter {


        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyHolder(View.inflate(parent.getContext(), R.layout.adapter_circle_rlv_praise_item, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            AvatarImage imageView = ((MyHolder) holder).img;
            FavortItem favortDatas = (FavortItem) getDatas().get(position);
            imageView.load(favortDatas.getUser().getHeadUrl(),favortDatas.getUser().getName());
            imageView.setOnClickListener(new OnDedrepClickListener() {
                @Override
                public void click(View v) {
                    ARouter.getInstance().build(Routes.Main.PERSON_DETAIL).withString(Constants.K_ID, favortDatas.getUser().getId()).navigation();
                }
            });
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }


        class MyHolder extends RecyclerView.ViewHolder {

            private AvatarImage img;

            public MyHolder(View itemView) {
                super(itemView);
                img = (AvatarImage) itemView.findViewById(R.id.headIv);
            }
        }
    }
}
