package com.utopiaindustries.qualitychecker.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.utopiaindustries.qualitychecker.R;
import com.utopiaindustries.qualitychecker.models.InspectionReport;

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

public class ReportAdapter extends
        RecyclerView.Adapter<ReportAdapter.ViewHolder>{
    private final List<InspectionReport> reports;

    public ReportAdapter(List<InspectionReport> reports) {
        this.reports = reports;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        try {
            holder.bindDate( reports.get( position ) );
        } 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,createdByTv, remarksTv,dateTv,status;
        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());

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

        public void bindDate( InspectionReport report ) throws ParseException {
            codeTv.setText( report.getCode() );
            createdByTv.setText( report.getGeneratedBy() );
            remarksTv.setText( report.getSku() );

            Date date = jsonDateFormat.parse( report.getGeneratedAt() );
            // Format the date in 12-hour format
            String formattedDateTime = displayDateFormat.format( date );
            dateTv.setText( formattedDateTime );

            if (report.getReportResult() != null && !report.getReportResult().isEmpty()) {
                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( report.getReportResult()  );
            }

        }
    }
}
