package com.sheep.gamegroup.view.adapter;

import android.support.v7.widget.AppCompatRatingBar;
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;

/**
 * Created by realicing on 2018/11/1.
 * realicing@sina.com
 * 游戏评论adapter
 */
public class AdpGcGameAppComment extends BaseQuickAdapter<UserComment, BaseViewHolder> {
    public AdpGcGameAppComment(List<UserComment> list) {
        super(R.layout.item_gc_game_app_comment, list);
    }

    @Override
    protected void convert(BaseViewHolder helper, final UserComment 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);
        AppCompatRatingBar game_app_comment_score = helper.itemView.findViewById(R.id.game_app_comment_score);
        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);
        final TextView game_app_comment_num3 = helper.itemView.findViewById(R.id.game_app_comment_num3);
        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;
        }
        ViewUtil.setAvatar(game_app_comment_user_avatar, item.getAvatar());
        ViewUtil.setText(game_app_comment_num1, item.getComment());
        ViewUtil.setText(game_app_comment_num2, item.getLike());
        ViewUtil.setText(game_app_comment_num3, item.getExpression());
        game_app_comment_score.setProgress(item.getScore());
        game_app_comment_score.setMax(10);
        ViewUtil.setText(game_app_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());

        game_app_comment_num1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转游戏评论详情界面
                Jump2View.getInstance().goActAppCommentDetail(ActivityManager.getInstance().currentActivity(), item.getId());
            }
        });
        game_app_comment_user_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转个人游戏主页界面
                Jump2View.getInstance().goActUserAppHome(ActivityManager.getInstance().currentActivity(), item.getUser_id());
            }
        });

        game_app_comment_num2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                game_app_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(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_num3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                game_app_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(game_app_comment_num3, item.getExpression());
                        game_app_comment_num3.setEnabled(true);
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast("操作失败");
                        game_app_comment_num3.setEnabled(true);
                    }
                });
            }
        });
    }
}
