package com.project.ilsaenterprise.adapter.monitoring;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

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.MonitoringListModel;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Objects;

public class MonitoringAdapter extends ArrayAdapter<MonitoringListModel> {
    private static final String TAG = "MonitoringAdapater" ;
    Context context;
    private List<MonitoringListModel> models;

    public MonitoringAdapter(List<MonitoringListModel> models, Context context){
        super(context, R.layout.row_data, models);
        this.context = context;
        this.models = models;
    }

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

        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.row_data, null, true);
        DatabaseReference userJabatan = FirebaseDatabase.getInstance().getReference().child("users");
        holder = new viewHolder();

        holder.imageData = (ImageView) convertView.findViewById(R.id.imageData);
//        holder.userImage = (ImageView) convertView.findViewById(R.id.image_comment_profile);
        holder.username = convertView.findViewById(R.id.uploaderTxt);
        holder.uploadDate = convertView.findViewById(R.id.dayTxt);
        holder.district = convertView.findViewById(R.id.distrikPost);
        holder.farm = convertView.findViewById(R.id.kebunPost);
        holder.decription = convertView.findViewById(R.id.tvDescription);
        holder.timestamp = convertView.findViewById(R.id.idMonitoring);
        holder.totalSee = convertView.findViewById(R.id.countSee);
        holder.layContent = convertView.findViewById(R.id.layContent);
//        holder.jabatan1 = convertView.findViewById(R.id.jabatanLevel1);
//        holder.jabatan2 = convertView.findViewById(R.id.jabatanLevel2);
//        holder.jabatan3 = convertView.findViewById(R.id.jabatanLevel3);
//        holder.jabatanNew = convertView.findViewById(R.id.jabatanLevel4);
        holder.totalComment = convertView.findViewById(R.id.countComment);

        convertView.setTag(holder);

        MonitoringListModel monitoringModel = models.get(position);

//        try{
//            userJabatan.child(monitoringModel.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
//                @Override
//                public void onDataChange(@NonNull DataSnapshot snapshot) {
//                    if((snapshot.hasChild("role")))
//                    {
//                        if(Objects.equals(snapshot.child("role").getValue(), "Level 1")){
//                            holder.jabatan1.setText(Objects.requireNonNull(snapshot.child("role").getValue()).toString());
//                            holder.jabatan1.setVisibility(View.VISIBLE);
//                        }
//                        if(Objects.equals(snapshot.child("role").getValue(), "Level 2")){
//                            holder.jabatan2.setText(Objects.requireNonNull(snapshot.child("role").getValue()).toString());
//                            holder.jabatan2.setVisibility(View.VISIBLE);
//                        }
//                        if(Objects.equals(snapshot.child("role").getValue(), "Level 3")){
//                            holder.jabatan3.setText(Objects.requireNonNull(snapshot.child("role").getValue()).toString());
//                            holder.jabatan3.setVisibility(View.VISIBLE);
//                        }
//                    }
//                }
//
//                @Override
//                public void onCancelled(@NonNull DatabaseError error) {
//
//                }
//            });
//        }catch (Exception e){
//            holder.jabatanNew.setVisibility(View.VISIBLE);
//        }

        String imgUrl = monitoringModel.getImageUrl();
        Picasso.get().load(imgUrl).into(holder.imageData);
//        Picasso.get().load(monitoringModel.getImgLink()).into(holder.userImage);
        holder.username.setText(monitoringModel.getFullName());
        holder.uploadDate.setText(monitoringModel.getDateUpload());
        holder.district.setText(monitoringModel.getDistrict());
        holder.decription.setText(monitoringModel.getDeskripsi());
        holder.farm.setText(monitoringModel.getFarm());
        holder.timestamp.setText(monitoringModel.getTimestamp());
        holder.totalSee.setText(String.valueOf(monitoringModel.getCountSee()));
        holder.totalComment.setText(String.valueOf(monitoringModel.getCountComment()));

        return convertView;
    }



    static class viewHolder{
        ImageView imageData, userImage;
        TextView username, district, farm, decription, uploadDate, timestamp, totalSee, totalComment;
        RelativeLayout layContent;
//        jabatan1, jabatan2, jabatan3, jabatanNew,
    }
}
