package com.cygnusa.hyperm.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.OrderHistory;
import com.cygnusa.hyperm.wrappers.RedeemCategory;

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


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

    private ArrayList<OrderHistory> items;
    private Context context;

    public OrderHistoryAdapter(Context context, ArrayList<OrderHistory> item) {

        this.items = item;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.redeem_product_list, parent, false);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  context.startActivity(new Intent(context, RedeemProductListActivity.class));

            }
        });


        return new ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // Bulletin item = items.get(position);


        holder.title.setText(items.get(position).getProduct_name());
        if (items.get(position).getCard_data().cardnumber == null || items.get(position).getCard_data().cardnumber.equalsIgnoreCase("")) {
            holder.desc.setVisibility(View.GONE);

        }
        if (items.get(position).getCard_data().pin_or_url == null || items.get(position).getCard_data().pin_or_url.equalsIgnoreCase("")) {
            holder.pin.setVisibility(View.GONE);
        }
        if (items.get(position).getCard_data().expiry_date == null || items.get(position).getCard_data().expiry_date.equalsIgnoreCase("")) {
            holder.text_expiry_date.setVisibility(View.GONE);
        }

        if (items.get(position).getCard_data().cardnumber != null && items.get(position).getCard_data().pin_or_url != null) {

            holder.desc.setText("Card No : " + items.get(position).getCard_data().cardnumber);
            holder.pin.setText("Card PIN : " + items.get(position).getCard_data().pin_or_url);
            holder.text_expiry_date.setText("Card Expiry Date : " + finalFormate(items.get(position).getCard_data().expiry_date));
        }

        if(items.get(position).getStatus().trim().equalsIgnoreCase("0")){
            holder.status.setText("Status : Failed");
            holder.status.setTextColor(context.getResources().getColor(R.color.red));
        }else if(items.get(position).getStatus().trim().equalsIgnoreCase("1")){
            holder.status.setText("Status : Success");
            holder.status.setTextColor(context.getResources().getColor(R.color.green));
        }else if(items.get(position).getStatus().trim().equalsIgnoreCase("2")){
            holder.status.setText("Status : Progress");
            holder.status.setTextColor(context.getResources().getColor(R.color.orange));
        }


        holder.Amount.setText("Rs " + items.get(position).getProduct_amount());
        holder.Amount.setSelected(true);
        Glide.with(context).load(items.get(position).getImages())
                .thumbnail(Glide.with(context).load(R.drawable.gif_loader))
                .apply(new RequestOptions().fitCenter().diskCacheStrategy(DiskCacheStrategy.ALL)
                        .placeholder(R.drawable.placeholder)).into(holder.redeemImage);


    }
    public String finalFormate(String dte) {
        String date = "";
        if (dte.equalsIgnoreCase("0000-00-00")) {
            date = "00/00/0000";
            return date;
        } else {

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

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

        }
        return date;
    }

    @Override
    public int getItemCount() {

        return items.size();
    }

    public void add(RedeemCategory item, int position) {

    }

    public void add(RedeemCategory item) {


    }

    public void remove(int position) {

    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView redeemImage;
        TextView title, points, Amount, desc, pin, status,text_expiry_date;
        View view;

        private ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            redeemImage = itemView.findViewById(R.id.product_image);
            title = itemView.findViewById(R.id.text_title);
            desc = itemView.findViewById(R.id.text_desc);
            pin = itemView.findViewById(R.id.text_pin);
            status = itemView.findViewById(R.id.text_status);
            text_expiry_date = itemView.findViewById(R.id.text_expiry_date);

            Amount = itemView.findViewById(R.id.redeem_price);

        }

    }

    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;
    }
}
