package com.webclip.base;

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

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.webclip.base.databinding.ItemNotifyListBinding;

import java.security.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 通知列表适配器
 */
public class MyNotifyListAdapter extends RecyclerView.Adapter<MyNotifyListAdapter.ViewHolder> {

    private List<MessageInfo> listdata;
    private onItemClickPostionListener itemClickPostionListener;
    private Context context;
    private MessageInfo messageItem;


    public MyNotifyListAdapter(Context context, List<MessageInfo> listdata, MessageInfo messageItem) {
        this.context = context;
        this.listdata = listdata;
        this.messageItem = messageItem;
    }

    public void setListdata(List<MessageInfo> listdata) {
        this.listdata = listdata;
        notifyDataSetChanged();
    }


    public void setOnItemClick(onItemClickPostionListener onItemClick) {
        this.itemClickPostionListener = onItemClick;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNotifyListBinding commentBinding = ItemNotifyListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(commentBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        MessageInfo messageInfo = listdata.get(position);
        if (messageItem != null && messageInfo.getRecordId() == messageItem.getPushId()) {
            messageInfo.setShowAll(true);
            messageItem = null;
        }

        //        private final TextView tvType;
//        private final TextView tvTime;
//        private final TextView tvContent;
//        private final TextView tvTitle;
//        private final TextView tvJumpLink;
//        private final ImageView ivType;
//        private final ImageView ivNotifyPull;
//        private final ImageView ivMsg;
//        private ConstraintLayout root;
//        private LinearLayout layoutMore;
//        private LinearLayout rootLy;
//        private TextView lookIv;

        switch (messageInfo.getType()){
            case 2:
                holder.itemNotifyListBinding.bigBg.setBackgroundColor(context.getColor(R.color.notify_imagecolor));
                holder.itemNotifyListBinding.tvMsgType.setText(holder.itemNotifyListBinding.bigBg.getContext().getString(R.string.image_title));
                holder.itemNotifyListBinding.ivIcon.setImageResource(R.mipmap.ic_notifylogo_img);
                break;
            case 3:
                holder.itemNotifyListBinding.bigBg.setBackgroundColor(context.getColor(R.color.notify_jumplinkcolor));
                holder.itemNotifyListBinding.tvMsgType.setText(holder.itemNotifyListBinding.bigBg.getContext().getString(R.string.link_title));
                holder.itemNotifyListBinding.ivIcon.setImageResource(R.mipmap.ic_notifylogo_link);
                break;
            default:
                holder.itemNotifyListBinding.bigBg.setBackgroundColor(context.getColor(R.color.notify_textcolor));
                holder.itemNotifyListBinding.tvMsgType.setText(holder.itemNotifyListBinding.bigBg.getContext().getString(R.string.text_title));
                holder.itemNotifyListBinding.ivIcon.setImageResource(R.mipmap.ic_notifylogo);
                break;
        }

        holder.itemNotifyListBinding.tvMsgTitle.setText(messageInfo.getTitle());
        holder.itemNotifyListBinding.layoutMore.setVisibility(View.GONE);
        holder.itemNotifyListBinding.ivNotifycontent.setText(messageInfo.getContent());
        holder.itemNotifyListBinding.ivNotifyimage.setVisibility(View.GONE);
        holder.itemNotifyListBinding.tvMsgTime.setText(messageInfo.getCreateTime());
        holder.itemNotifyListBinding.ivNotifyjumpclick.setVisibility(View.GONE);

        holder.itemNotifyListBinding.lookIv.setText(longTime(messageInfo.getCreateTime()));
        if (!TextUtils.isEmpty(messageInfo.getImage())) {
            LogUtils.i("地址是啥："+messageInfo.getImage());
            holder.itemNotifyListBinding.ivNotifyimage.setVisibility(View.VISIBLE);
            Glide.with(context).load(messageInfo.getImage()).into(holder.itemNotifyListBinding.ivNotifyimage);
        }
        if (!TextUtils.isEmpty(messageInfo.getJumpUrl())) {
            holder.itemNotifyListBinding.ivNotifyjumpclick.setVisibility(View.VISIBLE);
            holder.itemNotifyListBinding.ivNotifyjumpclick.setText(messageInfo.getJumpUrl());
            holder.itemNotifyListBinding.ivNotifyjumpclick.setOnClickListener(view -> {
                if (itemClickPostionListener != null) {
                    itemClickPostionListener.item(position);
                }
            });
        }
        if (messageInfo.isShowAll()) {
            holder.itemNotifyListBinding.icNotifyPull.setBackgroundResource(R.mipmap.ic_notify_shangla);
            holder.itemNotifyListBinding.layoutMore.setVisibility(View.VISIBLE);
        } else {
            holder.itemNotifyListBinding.icNotifyPull.setBackgroundResource(R.mipmap.ic_notify_xiala);
            holder.itemNotifyListBinding.layoutMore.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(view -> {
            messageInfo.setShowAll(!messageInfo.isShowAll());
            notifyItemChanged(position);
        });

    }

    public String longTime(String dateString){
        // 定义所需的日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Long timeStr = dateFormat.parse(dateString).getTime();

            String name = (((System.currentTimeMillis()-timeStr)/1000/60)+1000)+"";

            return name;
        } catch (ParseException e) {
           return "";
        }

    }


    @Override
    public int getItemCount() {
        return listdata != null ? listdata.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ItemNotifyListBinding itemNotifyListBinding;
        public ViewHolder(@NonNull ItemNotifyListBinding itemView) {
            super(itemView.getRoot());
            itemNotifyListBinding = itemView;
        }

    }

    public interface onItemClickPostionListener {
        void item(int position);
    }

}
