package com.example.leudaemialikeme.Adapter;

import android.content.Intent;
import android.util.Log;
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.recyclerview.widget.RecyclerView;

import com.example.leudaemialikeme.Activity.BaseActivity;
import com.example.leudaemialikeme.Activity.ChatActivity;
import com.example.leudaemialikeme.Dao.MessageDao;
import com.example.leudaemialikeme.Model.Chat;
import com.example.leudaemialikeme.R;

import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    private List<Chat> chatList;
    private MessageDao messageDao = new MessageDao();

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView portrait;
        TextView nickname;
        TextView introduction;
        LinearLayout linear_msg;
        ImageView point;
        TextView chat_friend_id;
        public ViewHolder(View view){
            super(view);
            portrait = (ImageView)view.findViewById(R.id.img_portrait);
            nickname = (TextView)view.findViewById(R.id.text_nickname);
            introduction = (TextView)view.findViewById(R.id.text_introduction);
            linear_msg = (LinearLayout)view.findViewById(R.id.linear_msg);
            point = (ImageView)view.findViewById(R.id.img_unread);
            chat_friend_id = (TextView)view.findViewById(R.id.chat_friend_id);
        }

    }

    public MsgAdapter(List<Chat> chatList) {
        this.chatList = chatList;
    }

    @NonNull
    @Override
    public MsgAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.pivate_msg_item,parent,false);
        MsgAdapter.ViewHolder holder=new MsgAdapter.ViewHolder(view);
//        holder.linear_msg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//                Intent intent = new Intent(view.getContext(), ChatActivity.class);
//                intent.putExtra("friendId", fdddddddd);
//            }
//        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MsgAdapter.ViewHolder holder, int position) {
        Chat chat = chatList.get(position);
        holder.introduction.setText(String.valueOf(chat.getLastMessage()));
        holder.nickname.setText(chat.getFriendName());
        holder.chat_friend_id.setText(String.valueOf(chat.getFriendId()));
        Log.d("MsgAdapter","chat_friend_id:"+chat.getFriendId());
        if(chat.getFlag()==1){
            holder.point.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                messageDao.changeMsgStatus(Integer.parseInt(holder.chat_friend_id.getText().toString()), BaseActivity.owner.getNetId());
                Intent intent = new Intent(v.getContext(), ChatActivity.class);
                intent.putExtra("friendNetId",chat.getFriendId());
                v.getContext().startActivity(intent);
                holder.point.setVisibility(View.GONE);
//                checkFriendRequest(params);
            }
        });
    }


    @Override
    public int getItemCount() {
        return chatList.size();
    }
}
