package com.utopiaindustries.qualitychecker.ui.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.utopiaindustries.qualitychecker.R;
import com.utopiaindustries.qualitychecker.db.ReportRepository;
import com.utopiaindustries.qualitychecker.models.InspectionReport;
import com.utopiaindustries.qualitychecker.models.InspectionReportWrapper;
import com.utopiaindustries.qualitychecker.utils.SelectListener;

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

public class DraftReportAdapter extends
        RecyclerView.Adapter<DraftReportAdapter.ViewHolder>{
    private final List<InspectionReportWrapper> reports;
    private SelectListener listener;

    public DraftReportAdapter(List<InspectionReportWrapper> reports, SelectListener listener) {
        this.reports = reports;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from( parent.getContext() ).inflate(R.layout.draft_report_item , parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        try {
            holder.bindDate( reports.get( position ) );
            holder.cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClicked(reports.get(position));
                }
            });

            holder.delete_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("Delete-Report: ","********");
                    new ReportRepository(v.getContext()).deleteDraftRecordById(reports.get(position).getId());
                    reports.remove(position);    // Remove from the list
                    notifyItemRemoved(position);    // Notify RecyclerView about the item removal
                    notifyItemRangeChanged(position, reports.size());

                }
            });
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView codeTv, remarksTv,dateTv,status;//createdByTv,;
        private final SimpleDateFormat jsonDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
        private final SimpleDateFormat displayDateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm a", Locale.getDefault());
        private final CardView cardView;
        private final ImageButton delete_image;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            codeTv = itemView.findViewById( R.id.code );
            //createdByTv = itemView.findViewById( R.id.created_by );
            delete_image = itemView.findViewById(R.id.delete_image);
            remarksTv = itemView.findViewById( R.id.remarks );
            dateTv = itemView.findViewById( R.id.date );
            status = itemView.findViewById( R.id.status );
            cardView = itemView.findViewById(R.id.main_container);
        }

        public void bindDate( InspectionReportWrapper report ) throws ParseException {
            codeTv.setText( "Report- " + (getAdapterPosition() + 1) );
            //createdByTv.setText("" );
            remarksTv.setText( "" );

            //Date date = jsonDateFormat.parse( new Date().toString() );
            // Format the date in 12-hour format
            //String formattedDateTime = displayDateFormat.format( date );
            String date = displayDateFormat.format(new Date());
            dateTv.setText(date);
           /* if( report.getReportResult().equalsIgnoreCase("FAILED") ){
                status.setBackgroundResource( R.drawable.failed_bg );
            }
            if( report.getReportResult().equalsIgnoreCase("PASSED") ){
                status.setBackgroundResource( R.drawable.passed_bg );
            }
            if( report.getReportResult().equalsIgnoreCase("ON_HOLD") ){
                status.setBackgroundResource( R.drawable.hold_bg );
            }*/
            status.setText("Draft");
        }
    }
}
