package com.project.ilsaenterprise.adapter;

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

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.project.ilsaenterprise.R;
import com.project.ilsaenterprise.models.GroupMessageModel;
import com.squareup.picasso.Picasso;
import java.util.List;
import java.util.Objects;

public class MessageGroupListAdapter extends RecyclerView.Adapter {
    private static final int VIEW_TYPE_MESSAGE_SENT = 1;
    private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
    //private FirebaseAuth mAuth = FirebaseAuth.getInstance();

    private Context mContext;
    private List<GroupMessageModel> mMessageList;
    private String UID;

    public MessageGroupListAdapter(Context context, List<GroupMessageModel> messageList, String getUID) {
        mContext = context;
        mMessageList = messageList;
        UID = getUID;
    }

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

    @Override
    public int getItemViewType(int position) {
        GroupMessageModel message = (GroupMessageModel) mMessageList.get(position);

        if (message.getSenderID().equals(UID)) {
            return VIEW_TYPE_MESSAGE_SENT;
        } else {
            return VIEW_TYPE_MESSAGE_RECEIVED;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;

        if (viewType == VIEW_TYPE_MESSAGE_SENT) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_send, parent, false);
            return new SentMessageHolder(view);
        } else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_message_received, parent, false);
            return new ReceivedMessageHolder(view);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        GroupMessageModel message = (GroupMessageModel) mMessageList.get(position);

        switch (holder.getItemViewType()) {
            case VIEW_TYPE_MESSAGE_SENT:
                ((SentMessageHolder) holder).bind(message);
                break;
            case VIEW_TYPE_MESSAGE_RECEIVED:
                ((ReceivedMessageHolder) holder).bind(message);
        }
    }

    private class SentMessageHolder extends RecyclerView.ViewHolder {
        TextView messageText, timeText;

        SentMessageHolder(View itemView) {
            super(itemView);

            messageText = (TextView) itemView.findViewById(R.id.text_message_body);
            timeText = (TextView) itemView.findViewById(R.id.text_message_time);
        }

        void bind(GroupMessageModel message) {
            messageText.setText(message.getMessage());

            timeText.setText(message.getTime());
        }
    }

    private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
        TextView messageText, timeText, nameText, jabatan1, jabatan2, jabatan3, jabatanNew;
        ImageView profileImage;
        DatabaseReference userJabatan = FirebaseDatabase.getInstance().getReference().child("users");

        ReceivedMessageHolder(View itemView) {
            super(itemView);

            messageText = itemView.findViewById(R.id.text_message_body);
            jabatan1 = itemView.findViewById(R.id.jabatanLevel1);
            jabatan2 = itemView.findViewById(R.id.jabatanLevel2);
            jabatan3 = itemView.findViewById(R.id.jabatanLevel3);
            jabatanNew = itemView.findViewById(R.id.jabatanLevel4);
            timeText = itemView.findViewById(R.id.text_message_time);
            nameText = itemView.findViewById(R.id.text_message_name);
            profileImage = itemView.findViewById(R.id.image_message_profile);
        }

        void bind(GroupMessageModel message) {
            messageText.setText(message.getMessage());
            timeText.setText(message.getTime());
            nameText.setText(message.getName());

            userJabatan.child(message.getSenderID()).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if((snapshot.hasChild("role")))
                    {
                        if(Objects.equals(snapshot.child("role").getValue(), "Level 1")){
                            jabatan1.setText(Objects.requireNonNull(snapshot.child("role").getValue()).toString());
                            jabatan1.setVisibility(View.VISIBLE);
                        }
                        if(Objects.equals(snapshot.child("role").getValue(), "Level 2")){
                            jabatan2.setText(Objects.requireNonNull(snapshot.child("role").getValue()).toString());
                            jabatan2.setVisibility(View.VISIBLE);
                        }
                        if(Objects.equals(snapshot.child("role").getValue(), "Level 3")){
                            jabatan3.setText(Objects.requireNonNull(snapshot.child("role").getValue()).toString());
                            jabatan3.setVisibility(View.VISIBLE);
                        }
                    }
                    else {
                        jabatanNew.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });

            userJabatan.child(message.getSenderID()).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if((snapshot.hasChild("image")))
                    {
                        String imageUrl = snapshot.child("image").getValue().toString();
                        Picasso.get().load(imageUrl).placeholder(R.drawable.profile_image).into(profileImage);
                    }
                    else
                        return;
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });

        }
    }
}
