package com.rv.home.rv.module.ui.main.home.adapter;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ruiwenliu.wrapper.util.BaseGlideHolder;
import com.ruiwenliu.wrapper.util.TransformationUtil;
import com.ruiwenliu.wrapper.util.TransformationUtil3;
import com.ruiwenliu.wrapper.util.glide.GlideManager;
import com.ruiwenliu.wrapper.util.glide.GlideOptions;
import com.rv.component.utils.DisplayUtil;
import com.rv.home.R;
import com.rv.home.rv.module.ui.main.home.bean.HomeRecommendBean;
import com.xxrv.video.bean.circle.DiscoveryDataBean;

public class RVEnthusiastAdapter extends BaseQuickAdapter<DiscoveryDataBean, BaseGlideHolder> {

    public RVEnthusiastAdapter() {
        super(R.layout.rv_item_rv_enthusiast);
    }

    @Override
    protected void convert(BaseGlideHolder helper, DiscoveryDataBean item) {
        DiscoveryDataBean.Body body = item.getBody();
        if (body != null) {
            if (body.getImages() != null && body.getImages().size() > 0) {

//                GlideManager.getInstance(mContext).loadRoundImage2(body.getImages().get(0).getOurl(), (ImageView) helper.getView(R.id.iv_icon_pat),8);

                ImageView image = helper.getView(R.id.iv_icon_pat);
                GlideOptions options = GlideOptions.placeholderOf(com.ruiwenliu.wrapper.R.drawable.glide_icon_placeholder).
                        error(com.ruiwenliu.wrapper.R.drawable.glide_icon_error).centerCrop();
                TransformationUtil3 utils = new TransformationUtil3(image);
                Glide.with(mContext)
                        .asBitmap()
                        .load(body.getImages().get(0).getOurl())
                        .apply(options)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(DisplayUtil.dip2px(mContext,7))))
                        .into(utils);
            }

            helper.setText(R.id.tv_title, body.getText());
            if ("4".equals(body.getType())) { //消息类型 基础属性 1=文字消息、2=图文消息、3=语音消息、4=视频消息、 5=文件消
                helper.setGone(R.id.iv_isvideo, true);
            } else {
                helper.setGone(R.id.iv_isvideo, false);
            }
        }
        helper.setText(R.id.tv_user_name, item.getNickname());
        GlideManager.getInstance(mContext).loadImage(item.getPicUrl(), (ImageView) helper.getView(R.id.iv_user_icon));
        if (item.getCount() != null) {
            helper.setText(R.id.iv_like_number, "点赞数  " + getPraise(item.getCount()));
        }
        if (1 == item.getIsPraise()) { //0：未点赞 1：已点赞
            helper.setTextColor(R.id.iv_like_number, mContext.getResources().getColor(R.color.gray_FFB74B));
        } else {
            helper.setTextColor(R.id.iv_like_number, mContext.getResources().getColor(R.color.textGray));
        }
    }

    private String getPraise(DiscoveryDataBean.Count count) {
        StringBuilder builder = new StringBuilder();
        try {
            int praise = Integer.valueOf(count.getPraise()).intValue();
            if (praise < 999) {
                builder.append(count.getPraise());
            } else if (praise < 9999) {
                builder.append(praise / 1000 + "." + praise % 1000 / 100 + "k");
            } else {
                builder.append(praise / 10000 + "." + praise % 10000 / 1000 + "w");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return builder.toString();
    }
}
