package com.utopiaindustries.qualitychecker.ui.adapter;

import android.annotation.SuppressLint;
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.InspectionReportItem;

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

public class ItemHistoryAdapter extends
        RecyclerView.Adapter<ItemHistoryAdapter.ViewHolder> {

    private final List<InspectionReportItem> items;

    public ItemHistoryAdapter(List<InspectionReportItem> items) {
        this.items = items;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        InspectionReportItem item = items.get( position );
        try {
            holder.bind( item );
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView itemSku,itemAsin,itemDate,itemCartons,itemPacks,itemPieces;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemSku = itemView.findViewById( R.id.item_history_sku );
            itemAsin = itemView.findViewById( R.id.item_history_asin );
            itemDate = itemView.findViewById( R.id.item_history_date );
            itemCartons = itemView.findViewById( R.id.item_history_cartons );
            itemPacks = itemView.findViewById( R.id.item_history_packs );
            itemPieces = itemView.findViewById( R.id.item_history_pieces );
        }

        @SuppressLint("DefaultLocale")
        public void bind(InspectionReportItem item ) throws ParseException {
            SimpleDateFormat jsonDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
            SimpleDateFormat displayDateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm a", Locale.getDefault());
            itemSku.setText( item.getSku() );
            itemAsin.setText( item.getAsin() );
            if( item.getDateAdded() != null ){
                Date date = jsonDateFormat.parse( item.getDateAdded() );
                String formattedDateTime = displayDateFormat.format( date );
                itemDate.setText( formattedDateTime );
            } else {
                itemDate.setText("");
            }

            try {
                itemCartons.setText( String.format("Cartons : %f", item.getCartonsSelected() ) );
                itemPacks.setText( String.format("Packs : %f", item.getPacksSelected() ) );
                itemPieces.setText( String.format("Pieces : %f", item.getPiecesSelected() ) );
            }
            catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
