package com.project.ilsaenterprise.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

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.auth.FirebaseUser;
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.Comment;
import com.project.ilsaenterprise.models.ContactModel;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import de.hdodenhof.circleimageview.CircleImageView;

public class CommentAdapter extends ArrayAdapter<Comment> {
    private static final String TAG = "CommentAdapter" ;
    private Context context;
    private List<Comment> commentList;
    DatabaseReference refUser = FirebaseDatabase.getInstance().getReference("users");
    private GoogleSignInAccount googleLogin;
    private FirebaseAuth mAuth;
    private FirebaseUser userFirebase;

    public CommentAdapter(List<Comment> commentList, Context context){
        super(context, R.layout.panen_row, commentList);
        this.context = context;
        this.commentList = commentList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        viewHolder holder;

        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.comment_row, null, true);
        holder = new viewHolder();

        holder.nama = convertView.findViewById(R.id.namaComment);
        holder.waktu = convertView.findViewById(R.id.timeComment);
        holder.komentar = convertView.findViewById(R.id.commentTv);
        holder.image = convertView.findViewById(R.id.imageComment);
        convertView.setTag(holder);

        googleLogin = GoogleSignIn.getLastSignedInAccount(context);
        mAuth = FirebaseAuth.getInstance();
        userFirebase = mAuth.getCurrentUser();
        Comment comment = commentList.get(position);

        refUser.child(comment.getUser()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                ContactModel contactModel = snapshot.getValue(ContactModel.class);

                if(contactModel.getImage() != null)
                    Picasso.get().load(contactModel.getImage()).placeholder(R.drawable.employee_img).into(holder.image);
                if(contactModel.getUid().equals(userFirebase.getUid()))
                    Picasso.get().load(userFirebase.getPhotoUrl()).placeholder(R.drawable.employee_img).into(holder.image);

                holder.nama.setText(contactModel.getNamaLengkap());
                holder.waktu.setText(comment.getTimestamp());
                holder.komentar.setText(comment.getKomentar());
            }

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

            }
        });



        return convertView;
    }

    static class viewHolder{
        TextView nama, waktu, komentar;
        CircleImageView image;
    }

}