package com.sheep.gamegroup.util.viewHelper;

import android.support.v7.widget.AppCompatRatingBar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.gamegroup.dateview.DateUtil;
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;

/**
 * Created by realicing on 2018/11/6.
 * realicing@sina.com
 * 用户评论帮助类
 */
public class UserCommentHelper {
    protected View rootView;

    public UserCommentHelper(int rootViewId) {
        this.rootView = LayoutInflater.from(SheepApp.getInstance()).inflate(rootViewId, null);
        initView();
    }

    public UserCommentHelper(View rootView) {
        this.rootView = rootView;
        initView();
    }

    public View getRootView() {
        return rootView;
    }

    protected TextView user_comment_detail_game_name;
    protected ImageView user_comment_detail_game_icon;
    protected ImageView user_comment_detail_user_avatar;
    protected TextView user_comment_detail_user_name;
    protected TextView user_comment_detail_user_publish_time;
    protected AppCompatRatingBar user_comment_detail_user_publish_score;
    protected TextView user_comment_detail_num1;
    protected TextView user_comment_detail_num2;
    protected TextView user_comment_detail_num3;

    protected void initView() {
        user_comment_detail_game_name = rootView.findViewById(R.id.user_comment_detail_game_name);
        user_comment_detail_game_icon = rootView.findViewById(R.id.user_comment_detail_game_icon);
        user_comment_detail_user_avatar = rootView.findViewById(R.id.user_comment_detail_user_avatar);
        user_comment_detail_user_name = rootView.findViewById(R.id.user_comment_detail_user_name);
        user_comment_detail_user_publish_time = rootView.findViewById(R.id.user_comment_detail_user_publish_time);
        user_comment_detail_user_publish_score = rootView.findViewById(R.id.user_comment_detail_user_publish_score);
        user_comment_detail_num1 = rootView.findViewById(R.id.user_comment_detail_num1);
        user_comment_detail_num2 = rootView.findViewById(R.id.user_comment_detail_num2);
        user_comment_detail_num3 = rootView.findViewById(R.id.user_comment_detail_num3);
    }

    //加载数据
    public void loadHeaderViewData(final UserComment item) {
        user_comment_detail_user_publish_score.setMax(10);
        user_comment_detail_user_publish_score.setProgress(item.getScore());
        ViewUtil.setText(user_comment_detail_num1, item.getComment());
        ViewUtil.setText(user_comment_detail_num2, item.getLike());
        ViewUtil.setText(user_comment_detail_num3, item.getExpression());
        ViewUtil.setText(user_comment_detail_game_name, item.getApp_name());
        ViewUtil.setText(user_comment_detail_user_name, item.getNickname());
        user_comment_detail_user_publish_time.setText(DateUtil.getTimeText(item.getCreate_time()));
        ViewUtil.setGameImage(user_comment_detail_game_icon, item.getApp_icon(), SheepApp.getInstance().getResources().getDimensionPixelSize(R.dimen.content_padding_5));
        ViewUtil.setAvatar(user_comment_detail_user_avatar, item.getAvatar());
        TextViewUtil.initIntroView(rootView, item.getShowAll().setShowAllText("全文"));
        user_comment_detail_user_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Jump2View.getInstance().goActUserAppHome(ActivityManager.getInstance().currentActivity(), item.getUser_id());
            }
        });
        user_comment_detail_num1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Jump2View.getInstance().goActAppCommentDetail(ActivityManager.getInstance().currentActivity(), item.getId());
            }
        });
        user_comment_detail_num2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                user_comment_detail_num2.setEnabled(false);
                ApiUtil.postGameUserUserGameCommentLike(item.getId(), new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {

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

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        G.showToast("操作成功");
                        if(user_comment_detail_num3 != null) {
                            item.plusExpression();
                            ViewUtil.setText(user_comment_detail_num3, item.getExpression());
                            user_comment_detail_num3.setEnabled(true);
                        }
                    }
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast("操作失败");
                        if(user_comment_detail_num3 != null)
                            user_comment_detail_num3.setEnabled(true);
                    }
                });
            }
        });
    }

    public ImageView getUser_comment_detail_game_icon() {
        return user_comment_detail_game_icon;
    }

    public AppCompatRatingBar getUser_comment_detail_user_publish_score() {
        return user_comment_detail_user_publish_score;
    }
}
