package com.sheep.gamegroup.view.adapter;

import android.support.annotation.LayoutRes;
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.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserComment;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.gamegroup.util.ApiUtil;
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 rx.functions.Action1;

/**
 * Created by realicing on 2018/11/1.
 * realicing@sina.com
 * 游戏资讯评论 adapter
 */
public class AdpArticleComment extends BaseQuickAdapter<UserComment, BaseViewHolder> {
    public AdpArticleComment(List<UserComment> list) {
        super(R.layout.item_article_comment, list);
    }
    public AdpArticleComment(@LayoutRes int layoutId, List<UserComment> list) {
        super(layoutId, list);
    }
    private boolean hasCommentDetail = true;

    public AdpArticleComment setHasCommentDetail(boolean hasCommentDetail) {
        this.hasCommentDetail = hasCommentDetail;
        return this;
    }

    @Override
    protected void convert(BaseViewHolder helper, final UserComment item) {
        int position = helper.getAdapterPosition() - getHeaderLayoutCount();
        ImageView article_comment_user_avatar = helper.itemView.findViewById(R.id.article_comment_user_avatar);
        TextView article_comment_user_name = helper.itemView.findViewById(R.id.article_comment_user_name);
        TextView article_comment_num1 = helper.itemView.findViewById(R.id.article_comment_num1);
        final TextView article_comment_num2 = helper.itemView.findViewById(R.id.article_comment_num2);
        final TextView article_comment_num3 = helper.itemView.findViewById(R.id.article_comment_num3);
        View article_comment_line_top = helper.itemView.findViewById(R.id.article_comment_line_top);
        ViewUtil.setVisibility(article_comment_line_top, position != 0);
        if(item == null){
            return;
        }
        ViewUtil.setAvatar(article_comment_user_avatar, item.getAvatar());
        ViewUtil.setText(article_comment_num1, item.getComment());
        ViewUtil.setText(article_comment_num2, item.getLike());
        ViewUtil.setText(article_comment_num3, item.getExpression());
        ViewUtil.setText(article_comment_user_name, item.getNickname());
        //String.format(Locale.CHINA, "%d %d %d %d", position, helper.getAdapterPosition(), helper.getLayoutPosition(), helper.getOldPosition()));
        TextViewUtil.initIntroView(helper.itemView, item.getShowAll());

        if(hasCommentDetail) {
            article_comment_num1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //跳转评论详情界面
                    Jump2View.getInstance().goActAppCommentDetail(ActivityManager.getInstance().currentActivity(), item.getId());
                }
            });
        }

        article_comment_num2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                article_comment_num2.setEnabled(false);
                ApiUtil.postGameUserUserGameCommentLike(item.getId(), new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        G.showToast("操作成功");
                        item.plusLike();
                        ViewUtil.setText(article_comment_num2, item.getLike());
                        article_comment_num2.setEnabled(true);
                    }
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast("操作失败");
                        article_comment_num2.setEnabled(true);
                    }
                });
            }
        });
        if(article_comment_num3 != null) {
            article_comment_num3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    article_comment_num3.setEnabled(false);
                    ApiUtil.postGameUserUserGameCommentExpressionLike(item.getId(), new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {

                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            G.showToast("操作成功");
                            item.plusExpression();
                            ViewUtil.setText(article_comment_num3, item.getExpression());
                            article_comment_num3.setEnabled(true);
                        }

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            G.showToast("操作失败");
                            article_comment_num3.setEnabled(true);
                        }
                    });
                }
            });
        }
        article_comment_user_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转个人游戏主页界面
                Jump2View.getInstance().goActUserAppHome(ActivityManager.getInstance().currentActivity(), item.getUser_id());
            }
        });
    }
}
