package com.project.ilsaenterprise.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
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.Attendance;

import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class HistoryWfhAdapter extends ArrayAdapter<Attendance> {
    private static final String TAG = "AttendanceAdapter" ;
    private Context context;
    private List<Attendance> attendanceList;
    private DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("users");
    private String workPos;

    public HistoryWfhAdapter(List<Attendance> attendanceList, Context context){
        super(context, R.layout.history_wfh_row, attendanceList);
        this.context = context;
        this.attendanceList = attendanceList;
    }

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

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

        holder.name = convertView.findViewById(R.id.nameTxt);
        holder.date = convertView.findViewById(R.id.timeAtt);
        holder.lat = convertView.findViewById(R.id.lat_txt);
        holder.lng = convertView.findViewById(R.id.long_txt);
        holder.location = convertView.findViewById(R.id.lokasi_txt);
        holder.in = convertView.findViewById(R.id.enterIv);
        holder.out = convertView.findViewById(R.id.outIv);
        holder.locString = convertView.findViewById(R.id.tapping_txt);
//        holder.ket = convertView.findViewById(R.id.ket_text);
        convertView.setTag(holder);

        Attendance attendance = attendanceList.get(position);

        reference.child(attendance.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                workPos = snapshot.child("penempatan").getValue(String.class);
                holder.date.setText(attendance.getTime());
                holder.lat.setText(attendance.getLat());
                holder.lng.setText(attendance.getLng());
                holder.location.setText(workPos);
                holder.locString.setText(attendance.getStrLoc());
            }

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

            }
        });

        if(attendance.getKeterangan().equals("Masuk"))
        {
            holder.name.setText("Masuk");
            holder.in.setVisibility(View.VISIBLE);
            holder.out.setVisibility(View.INVISIBLE);
        }
        if(attendance.getKeterangan().equals("Keluar")){
            holder.name.setText("Keluar");
            holder.in.setVisibility(View.INVISIBLE);
            holder.out.setVisibility(View.VISIBLE);
        }

//        holder.ket.setText(attendance.getKeterangan());

        return convertView;
    }

    private String getTime(long time) {
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(time);
        String date = DateFormat.format("HH:mm:ss", cal).toString();
        return date;
    }

    private String getDate(long time) {
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(time);
        String date = DateFormat.format("dd MMMM yyyy", cal).toString();
        return date;
    }

    static class viewHolder{
        TextView name;
        TextView date;
        TextView lat;
        TextView lng;
        TextView location;
        TextView timein;
        ImageView out, in;
        TextView locString;
    }
}