package com.project.ilsaenterprise.adapter;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialog;
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.HistoryAttendance;
import com.project.ilsaenterprise.MapActivity;
import com.project.ilsaenterprise.R;
import com.project.ilsaenterprise.models.Attendance;

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

public class AttendanceAdapter 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 AttendanceAdapter(List<Attendance> attendanceList, Context context){
        super(context, R.layout.history_attendance_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_attendance_row, null, true);
        holder = new viewHolder();

        holder.name = convertView.findViewById(R.id.nameTxt);
        holder.date = convertView.findViewById(R.id.tvDate);
        holder.time = convertView.findViewById(R.id.tvHour);
        holder.lat = convertView.findViewById(R.id.lat_txt);
        holder.lng = convertView.findViewById(R.id.long_txt);
        holder.location = convertView.findViewById(R.id.tapping_txt);
        holder.sideMarker = convertView.findViewById(R.id.sideMarker);
//        holder.ket = convertView.findViewById(R.id.ket_text);
        convertView.setTag(holder);

        Attendance attendance = attendanceList.get(position);

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

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

        if(attendance.getKeterangan().equals("Masuk"))
        {
            holder.name.setText("Masuk");
            holder.sideMarker.setBackgroundColor(convertView.getResources().getColor(R.color.Dark));
        }
        if(attendance.getKeterangan().equals("Keluar")){
            holder.name.setText("Keluar");
            holder.sideMarker.setBackgroundColor(convertView.getResources().getColor(R.color.YellowLight));
        }

        return convertView;
    }

    static class viewHolder{
        TextView name;
        TextView date;
        TextView lat;
        TextView lng;
        TextView time;
        TextView location;
        View sideMarker;
    }
}

