package com.ycgis.macall.personalcenter.v.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.rs.macall.androidx.basemodel.base.BaseRecyclerAdapter;
import com.rs.macall.androidx.basemodel.utils.StringUtil;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.m.adapterbean.MessageBean;
import com.ycgis.macall.personalcenter.p.app.AppCache;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * created by:   Macall
 * create time:  2022/11/11 17:20
 * copyright:    @ruansee.com
 * Describe:
 */
public class MessageAdapter extends BaseRecyclerAdapter<MessageBean,BaseRecyclerAdapter.ViewHolder> {

    public MessageAdapter(Context context, List<MessageBean> dataList) {
        super(context, dataList);
    }

    @Override
    public int getItemViewType(int position) {
        return getDataItem(position).getBaseType();
    }

    @NonNull
    @NotNull
    @Override
    public BaseRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        if (viewType == 1){
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_message,parent,false);
            return new MessageHolder(view);
        }
        if (viewType == 0|| viewType == -1){
            View view  = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_error_empty_xiao,parent,false);
            return new ErrorViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull BaseRecyclerAdapter.ViewHolder viewHolder, int position) {
        MessageBean dataItem = getDataItem(position);
        if (viewHolder instanceof MessageHolder){
            MessageHolder holder = (MessageHolder) viewHolder;
            holder.tv_app_name.setText(dataItem.getAppName());
            holder.tv_msg_title.setText(dataItem.getTitle());
            holder.tv_msg_content.setText(dataItem.getContent());
            if (dataItem.isRead()){//已读
                holder.iv_doit.setVisibility(View.GONE);
                holder.tv_read_type.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.icon_record));
//                holder.iv_icon.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.ic_read));
            }else {//未读
                holder.tv_read_type.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.icon_not_record));
//                int i = position % 2;
//                if (i == 0){
//                    holder.iv_icon.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.ic_unread1));
//                }else {
//                    holder.iv_icon.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.ic_unread2));
//                }
                holder.iv_doit.setVisibility(View.VISIBLE);
            }
            if (StringUtil.hasContent(dataItem.getAppIcon())){
                holder.setImg(holder.iv_icon,dataItem.getAppIcon());
            }
            holder.tv_time.setText(dataItem.getTime());
        }
        if (viewHolder instanceof BaseRecyclerAdapter.ErrorViewHolder){
            ErrorViewHolder holder = (ErrorViewHolder)viewHolder;
            if (dataItem.getBaseType() == 0){
                holder.setEmpty(dataItem.getBaseMessage(),R.drawable.icon_error2);
            }else {
                holder.setError(dataItem.getBaseMessage(),R.drawable.icon_empty);
            }
        }
    }

    private class MessageHolder extends ViewHolder{
        private TextView tv_app_name,tv_msg_title,tv_time,tv_msg_content;
        private ImageView iv_icon,iv_doit,tv_read_type;

        public MessageHolder(View itemView) {
            super(itemView);
            tv_app_name = itemView.findViewById(R.id.tv_app_name);
            tv_read_type = itemView.findViewById(R.id.tv_read_type);
            tv_msg_content = itemView.findViewById(R.id.tv_msg_content);
            tv_msg_title = itemView.findViewById(R.id.tv_msg_title);
            tv_time = itemView.findViewById(R.id.tv_time);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            iv_doit = itemView.findViewById(R.id.iv_doit);
            if (listClickListener!=null){
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listClickListener.onItemClickListener(v,getAdapterPosition());
                    }
                });
            }
        }

        private void setImg(ImageView img, String url) {
            String imgUrl = AppCache.BASE_IMAGE_URL + url;
            //图片加载自己实现
            Glide.with(img)
                    .load(imgUrl)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
                    .into(img);
        }
    }
}
