package com.sheep.gamegroup.view.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lqr.emoji.EmotionLayout;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.ShowAll;
import com.sheep.gamegroup.model.entity.UserComment;
import com.sheep.gamegroup.model.entity.UserCommentReply;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.gamegroup.util.ApiUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.TextViewUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.List;
import java.util.Locale;

import rx.functions.Action1;

/**
 * Created by realicing on 2018/11/2.
 * realicing@sina.com
 * 用户评论详情中的评论 adapter
 */
public class AdpUserCommentReply extends BaseQuickAdapter<UserCommentReply, BaseViewHolder> {
    public AdpUserCommentReply(List<UserCommentReply> list) {
        super(R.layout.item_user_comment_reply, list);
    }
    private UserComment userComment;
    private boolean isMe;//是否是我的评论详情，我的评论详情中，列表里面显示回复图标，并可点击进行回复


    public void setUserComment(UserComment userComment) {
        this.userComment = userComment;
        if(userComment != null){
            isMe = TextUtils.equals(String.valueOf(userComment.getUser_id()), DataUtil.getInstance().getUserId());
        }
    }
    //点击回复图标的回调
    private Action1<UserCommentReply> action1;

    public void setAction1(Action1<UserCommentReply> action1) {
        this.action1 = action1;
    }

    @Override
    protected void convert(BaseViewHolder helper, final UserCommentReply item) {
        int position = helper.getAdapterPosition() - getHeaderLayoutCount();
        ImageView game_app_comment_user_avatar = helper.itemView.findViewById(R.id.game_app_comment_user_avatar);
        TextView game_app_comment_user_name = helper.itemView.findViewById(R.id.game_app_comment_user_name);
        final TextView game_app_comment_num1 = helper.itemView.findViewById(R.id.game_app_comment_num1);
        final TextView game_app_comment_num2 = helper.itemView.findViewById(R.id.game_app_comment_num2);
        View game_app_comment_line_bottom = helper.itemView.findViewById(R.id.game_app_comment_line_bottom);
        TextView user_comment_reply_content = helper.itemView.findViewById(R.id.user_comment_reply_content);
        View game_app_comment_line_top = helper.itemView.findViewById(R.id.game_app_comment_line_top);
        ViewUtil.setVisibility(game_app_comment_line_top, position != 0);
        if(item == null)
            return;
        //自己回复自己的评论
        boolean replyUserIsCommentUser = userComment == null || userComment.getUser_id() == item.getUser_id();
        if(replyUserIsCommentUser){
            ViewUtil.setText(game_app_comment_user_name, item.getNickname());
        } else {
            ViewUtil.setText(game_app_comment_user_name, String.format(Locale.CHINA, "%s 回复 %s", item.getNickname(), userComment.getNickname()));
        }
        ViewUtil.setAvatar(game_app_comment_user_avatar, item.getAvatar());
        ViewUtil.setText(game_app_comment_num2, item.getLike());
        ViewUtil.setVisibility(game_app_comment_num1, isMe && !replyUserIsCommentUser && TextUtils.isEmpty(item.getReply()));//自己在自己的评论详情中，别人的评论下才显示回复图标,但是如果已经回复了，就不显示
        ViewUtil.setVisibility(game_app_comment_line_bottom, !TextUtils.isEmpty(item.getReply()));
        ViewUtil.setVisibility(user_comment_reply_content, !TextUtils.isEmpty(item.getReply()));
        if(item.getReplyShowAll().isHasExp())
            ViewUtil.setH5ImgText(user_comment_reply_content, item.getReplyShowAll().getContent(), G.WIDTH - G.getRealPix(50));
        else
            ViewUtil.setText(user_comment_reply_content, item.getReply());
        TextViewUtil.initIntroView(helper.itemView, item.getShowAll());
        game_app_comment_num1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(action1 != null)
                    action1.call(item);
            }
        });
        game_app_comment_num2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                game_app_comment_num2.setEnabled(false);
                ApiUtil.postGameUserUserCommentLike(item.getId(), new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        G.showToast("操作成功");
                        item.setLike(item.getLike() + 1);
                        ViewUtil.setText(game_app_comment_num2, item.getLike());
                        game_app_comment_num2.setEnabled(true);
                    }
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast("操作失败");
                        game_app_comment_num2.setEnabled(true);
                    }
                });
            }
        });
        game_app_comment_user_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Jump2View.getInstance().goActUserAppHome(ActivityManager.getInstance().currentActivity(), item.getUser_id());
            }
        });
    }
}
