package com.example.leudaemialikeme.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import com.example.leudaemialikeme.Model.Friend;
import com.example.leudaemialikeme.Model.Message;
import com.example.leudaemialikeme.Model.MessageItemView;
import com.example.leudaemialikeme.Model.Owner;
import com.example.leudaemialikeme.R;

import java.util.ArrayList;
import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
    private List<Message> messageList = new ArrayList<Message>();
    private Owner owner;
    private Friend friend;

    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout msg_content_left;
        LinearLayout msg_content_right;
        TextView left_msg;
        TextView right_msg;
        List<LinearLayout> layouts = new ArrayList<>();
        public ViewHolder(View view){
            super(view);
            msg_content_left = (LinearLayout) view.findViewById(R.id.msg_content_left);
            msg_content_right = (LinearLayout) view.findViewById(R.id.msg_content_right);
            left_msg = (TextView) view.findViewById(R.id.left_msg);
            right_msg = (TextView) view.findViewById(R.id.right_msg);
            layouts.add(msg_content_left);
            layouts.add(msg_content_right);
        }

        public void setVisibility(LinearLayout layout){
            for(LinearLayout layout1: layouts){
                if(layout1==layout){
                    layout1.setVisibility(View.VISIBLE);
                }else {
                    layout1.setVisibility(View.GONE);
                }
            }
        }

    }
    public ChatAdapter(MessageItemView messageItemView){
        this.messageList = messageItemView.messages;
        this.owner = messageItemView.owner;
        this.friend = messageItemView.friend;
    }

    @NonNull
    @Override
    public ChatAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_content_item,parent,false);
        ChatAdapter.ViewHolder holder = new ChatAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ChatAdapter.ViewHolder holder, int position) {
        Message message = messageList.get(position);
        switch (message.getMessageType()){
            case 1:
                if(message.getSenderNetId()==owner.getNetId()){
                    holder.setVisibility(holder.msg_content_right);
                    holder.right_msg.setText(message.getContent());
                }else {
                    holder.setVisibility(holder.msg_content_left);
                    holder.left_msg.setText(message.getContent());
                }
            default:
                break;
        }
    }

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