package com.cygnusa.hyperm.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

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.RedeemProductDetailsActivity;
import com.cygnusa.hyperm.wrappers.Bulletin;
import com.cygnusa.hyperm.wrappers.ProductListDetails;
import com.cygnusa.hyperm.wrappers.RedeemProduct;

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





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

    private Context context;
    RedeemProduct productList = new RedeemProduct();

    public RedeemProductListAdapter(Context context, RedeemProduct items) {

        this.productList = items;
        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) {

                ProductListDetails item = (ProductListDetails)v.getTag();
                RedeemProductDetailsActivity.productList = item;
                context.startActivity(new Intent(context, RedeemProductDetailsActivity.class));
                ((Activity)context).finish();

            }
        });


        return new ViewHolder(v);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        ProductListDetails item = productList.get_embedded().getProduct().get(position);
       holder.view.setTag(item);
        holder.title.setText(productList.get_embedded().getProduct().get(position).getName());
        holder.pin.setVisibility(View.GONE);
        holder.status.setVisibility(View.GONE);
        holder.text_expiry_date.setVisibility(View.GONE);
        holder.desc.setText(productList.get_embedded().getProduct().get(position).getName());
        holder.Amount.setText("Rs "+productList.get_embedded().getProduct().get(position).getMin_custom_price()+" to "+productList.get_embedded().getProduct().get(position).getMax_custom_price());
        holder.Amount.setSelected(true);
        Glide.with(context).load(productList.get_embedded().getProduct().get(position).getImages().getThumbnail())
                .thumbnail(Glide.with(context).load(R.drawable.gif_loader))
                .apply(new RequestOptions().fitCenter().diskCacheStrategy(DiskCacheStrategy.ALL)
                        .placeholder(R.drawable.placeholder)).into(holder.redeemImage);

    }


    @Override
    public int getItemCount() {
        return productList.get_embedded().getProduct().size();
    }

    public void add(Bulletin item, int position) {

    }

    public void add(Bulletin item) {


    }

    public void remove(int position) {

    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView redeemImage;
        TextView title, desc, Amount, 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);
            Amount = itemView.findViewById(R.id.redeem_price);
            pin = itemView.findViewById(R.id.text_pin);
            status = itemView.findViewById(R.id.text_status);
            text_expiry_date = itemView.findViewById(R.id.text_expiry_date);

        }

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