package com.cygnusa.hyperm.adapter;

import android.content.Context;
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.cygnusa.hyperm.R;
import com.cygnusa.hyperm.util.PositionUpdateListener;
import com.cygnusa.hyperm.wrappers.RewardPoints;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


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

    private ArrayList<RewardPoints> REWARD_POINTS_LIST;
    private PositionUpdateListener listener;
    private Context context;
    private boolean isEarnedHistory;

    public RedeemPointsAdapter(Context context, ArrayList<RewardPoints> items, PositionUpdateListener l, boolean isEarned) {
        this.REWARD_POINTS_LIST = items;
        this.listener = l;
        this.context = context;
        this.isEarnedHistory = isEarned;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//        RewardPoints rewardPoints = REWARD_POINTS_LIST.get(position);
//        holder.view.setTag(rewardPoints);
//        holder.title.setText(rewardPoints.getDescription());
//        holder.points.setText(rewardPoints.getPoints() + " Points");
        if (isEarnedHistory) {
            holder.points.setText("1000" + " Points");
            holder.textView.setText("Earned");
            //  holder.dateTime.setText("Earned On: " + dateFormate(rewardPoints.getModified_on()));
        } else {
            holder.points.setText("500" + " Points");
            holder.textView.setText("Redeemed");
            //  holder.dateTime.setText("Redeemed On: " + dateFormate(rewardPoints.getModified_on()));
        }


    }

    public String dateFormate(String dte) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date newDate = null;
        try {
            newDate = format.parse(dte);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        format = new SimpleDateFormat("dd-MMM-yyyy");
        String date = format.format(newDate);
        return date;
    }

    @Override
    public int getItemCount() {
//        return REWARD_POINTS_LIST.size();
        return 1;
    }

    public void add(RewardPoints item, int position) {
        REWARD_POINTS_LIST.add(position, item);
        notifyItemInserted(position);
    }

    public void add(RewardPoints item) {
        REWARD_POINTS_LIST.add(item);
        notifyItemInserted(REWARD_POINTS_LIST.size());
    }

    public void remove(int position) {
        REWARD_POINTS_LIST.remove(position);
        notifyItemRemoved(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView title, points, dateTime, textView;
        View view;

        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            textView = itemView.findViewById(R.id.text1);
            title = itemView.findViewById(R.id.text_title);
            points = itemView.findViewById(R.id.text_points);
            dateTime = itemView.findViewById(R.id.text_date);

        }

    }
}
