package com.gh.gamecenter.qa.editor;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.gh.gamecenter.common.base.BaseRecyclerViewHolder;
import com.gh.gamecenter.common.callback.OnListClickListener;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.DirectUtils;
import com.gh.gamecenter.common.utils.ImageUtils;
import com.gh.gamecenter.core.utils.MtaHelper;
import com.gh.common.util.NewsUtils;
import com.gh.gamecenter.core.utils.NumberUtils;
import com.gh.gamecenter.common.view.DrawableView;
import com.gh.gamecenter.R;
import com.gh.gamecenter.collection.AnswerFragment;
import com.gh.gamecenter.databinding.AskAnswerItemBinding;
import com.gh.gamecenter.entity.UserEntity;
import com.gh.gamecenter.qa.entity.AnswerEntity;
import com.gh.gamecenter.qa.entity.CommunityVideoEntity;

import java.util.List;

/**
 * Created by khy on 19/12/17.
 */

@Deprecated
public class AnswerViewHolder extends BaseRecyclerViewHolder<AnswerEntity> {

    public AskAnswerItemBinding binding;

    public AnswerViewHolder(View itemView, OnListClickListener listClickListener) {
        super(itemView, listClickListener);
        itemView.setOnClickListener(this);
        binding.askAnswerItemTitle.setOnClickListener(this);
    }

    public AnswerViewHolder(AskAnswerItemBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }


    public void initAnswerViewHolder(Context context, AnswerEntity entity, String entrance, String path) {
        setClickData(entity);
        binding.askAnswerItemContent.setText(entity.getBrief());
        binding.askAnswerItemCommentCount.setText(String.format("%s 评论", NumberUtils.transSimpleCount(entity.getCommentCount())));
        binding.askAnswerItemVotecount.setText(context.getString(R.string.ask_vote_count, NumberUtils.transSimpleCount(entity.getVote())));
        UserEntity user = entity.getUser();
        binding.askAnswerItemUsername.setText(user.getName());
        ImageUtils.displayIcon(binding.askAnswerItemUsericon, user.getIcon());
        if (user.getAuth() != null) {
            ImageUtils.display(binding.askAnswerItemUserBadge, user.getAuth().getIcon());
        } else {
            ImageUtils.display(binding.askAnswerItemUserBadge, "");
        }
        if (user.getBadge() != null) {
            binding.sdvUserBadge.setVisibility(View.VISIBLE);
            binding.tvBadgeName.setVisibility(View.VISIBLE);
            ImageUtils.display(binding.sdvUserBadge, user.getBadge().getIcon());
            binding.tvBadgeName.setText(user.getBadge().getName());
        } else {
            binding.sdvUserBadge.setVisibility(View.GONE);
            binding.tvBadgeName.setVisibility(View.GONE);
        }
        binding.sdvUserBadge.setOnClickListener(v -> DialogUtils.showViewBadgeDialog(context, user.getBadge(),
                () -> {
                    MtaHelper.onEvent("进入徽章墙_用户记录", "问题详情", user.getName() + "（" + user.getId() + "）");
                    MtaHelper.onEvent("徽章中心", "进入徽章中心", "问题详情");
                    DirectUtils.directToBadgeWall(context, user.getId(), user.getName(), user.getIcon());
                }));
        binding.tvBadgeName.setOnClickListener(v -> binding.sdvUserBadge.performClick());
        setAnswerImage(entity.getImages(), entity.getPassVideos());
        if (entity.getTime() != null && entity.getTime() != 0) {
            binding.askAnswerItemEndDesc.setText(NewsUtils.getFormattedTime(entity.getTime()));
        }
        skipPersonalHome(entity, entrance, path);
    }

    public void initCollectionAnswerViewHolder(Context context, AnswerEntity entity, String type, String entrance) {
        setClickData(entity);
        if (entity.getActive()) {
            binding.askAnswerItemContent.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
            binding.askAnswerItemContent.setTextColor(ContextCompat.getColor(binding.askAnswerItemTitle.getContext(), R.color.title));
        } else {
            binding.askAnswerItemContent.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            binding.askAnswerItemContent.setTextColor(ContextCompat.getColor(binding.askAnswerItemTitle.getContext(), R.color.hint));
        }
        binding.askAnswerItemContent.setText(entity.getBrief());
        binding.askAnswerItemTitle.setVisibility(View.VISIBLE);
        binding.askAnswerItemTitle.setText(entity.getQuestions().getTitle());
        String commentText = String.format("%s评论 · %s点赞 · %s", NumberUtils.transSimpleCount(entity.getCommentCount()), NumberUtils.transSimpleCount(entity.getVote()), NewsUtils.getFormattedTime(entity.getTime()));
        binding.askAnswerItemVotecount.setText(commentText);
        binding.askAnswerItemCommunityName.setVisibility(View.VISIBLE);
        binding.askAnswerItemCommunityName.setText(entity.getCommunityName());
        binding.askAnswerItemCommentCount.setVisibility(View.GONE);
        UserEntity user = entity.getUser();
        binding.askAnswerItemUsername.setText(user.getName());

        if (user.getBadge() != null) {
            binding.sdvUserBadge.setVisibility(View.VISIBLE);
            binding.tvBadgeName.setVisibility(View.VISIBLE);
            ImageUtils.display(binding.sdvUserBadge, user.getBadge().getIcon());
            binding.tvBadgeName.setText(user.getBadge().getName());
        } else {
            binding.sdvUserBadge.setVisibility(View.GONE);
            binding.tvBadgeName.setVisibility(View.GONE);
        }
        String key;
        if (AnswerFragment.COLLECTION.equals(type)) {
            key = "我的收藏-回答列表";
        } else if (AnswerFragment.HISTORY.equals(type)) {
            key = "浏览记录-回答列表";
        } else {
            key = "插入回答-收藏回答列表";
        }
        binding.sdvUserBadge.setOnClickListener(v -> DialogUtils.showViewBadgeDialog(context, user.getBadge(),
                () -> {
                    MtaHelper.onEvent("进入徽章墙_用户记录", key, user.getName() + "（" + user.getId() + "）");
                    MtaHelper.onEvent("徽章中心", "进入徽章中心", key);
                    DirectUtils.directToBadgeWall(context, user.getId(), user.getName(), user.getIcon());
                }));
        binding.tvBadgeName.setOnClickListener(v -> binding.sdvUserBadge.performClick());

        ImageUtils.displayIcon(binding.askAnswerItemUsericon, user.getIcon());
        if (user.getAuth() != null) {
            ImageUtils.display(binding.askAnswerItemUserBadge, user.getAuth().getIcon());
        } else {
            ImageUtils.display(binding.askAnswerItemUserBadge, "");
        }
        setAnswerImage(entity.getImages(), entity.getPassVideos());
        skipPersonalHome(entity, entrance, "我的收藏-回答");
    }

    public void initMyAnswerViewHolder(AnswerEntity entity) {
        setClickData(entity);
        binding.askAnswerItemUsername.setVisibility(View.GONE);
        binding.askAnswerItemUsericonContainer.setVisibility(View.GONE);
        binding.askAnswerItemTitle.setVisibility(View.VISIBLE);
        binding.askAnswerItemTitle.setText(entity.getQuestions().getTitle());
        binding.askAnswerItemContent.setText(entity.getBrief());
        binding.askAnswerItemCommunityName.setVisibility(View.VISIBLE);
        binding.askAnswerItemCommunityName.setText(entity.getCommunityName());
        binding.askAnswerItemCommentCount.setText(String.format("%s 评论", NumberUtils.transSimpleCount(entity.getCommentCount())));
        binding.askAnswerItemVotecount.setText(itemView.getContext().getString(R.string.ask_vote_count, NumberUtils.transSimpleCount(entity.getVote())));
        setAnswerImage(entity.getImages(), entity.getPassVideos());
    }

    private void setAnswerImage(List<String> images, List<CommunityVideoEntity> videos) {
        if (videos.size() > 0) {
            CommunityVideoEntity videoEntity = videos.get(0);
            ImageUtils.display(binding.askAnswerItemImg, videoEntity.getPoster());
            binding.askAnswerItemVideoDuration.setBackground(DrawableView.getOvalDrawable(R.color.black_alpha_50, 999F));
            binding.askAnswerItemVideoDuration.setText(videoEntity.getDuration());
            binding.askAnswerItemVideoDuration.setVisibility(View.VISIBLE);
            binding.askAnswerItemVideoPlay.setVisibility(View.VISIBLE);
            binding.askAnswerItemImg.setVisibility(View.VISIBLE);
        } else if (images.size() > 0) {
            for (int i = 0; i < images.size(); i++) {
                if (!images.get(i).contains(".gif")) {
                    binding.askAnswerItemImg.setVisibility(View.VISIBLE);
                    ImageUtils.display(binding.askAnswerItemImg, images.get(i));
                    break;
                }
                if (i == images.size() - 1) {
                    binding.askAnswerItemImg.setVisibility(View.GONE);
                }
            }
            binding.askAnswerItemVideoPlay.setVisibility(View.GONE);
            binding.askAnswerItemVideoDuration.setVisibility(View.GONE);
        } else {
            binding.askAnswerItemImg.setVisibility(View.GONE);
            binding.askAnswerItemVideoPlay.setVisibility(View.GONE);
            binding.askAnswerItemVideoDuration.setVisibility(View.GONE);
        }
    }

    private void skipPersonalHome(AnswerEntity entity, String entrance, String path) {
        View.OnClickListener onClickListener = v -> DirectUtils.directToHomeActivity(itemView.getContext(), entity.getUser().getId(), entrance, path);
        binding.askAnswerItemUsericon.setOnClickListener(onClickListener);
        binding.askAnswerItemUsername.setOnClickListener(onClickListener);
    }
}
