package com.friendvilla.OfferG;

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.squareup.picasso.Picasso;

import java.util.List;

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int MENU_ITEM_VIEW_TYPE = 0;
    private static final int UNIFIED_NATIVE_AD_VIEW_TYPE = 1;
    private final Context mContext;
    private final List<Object> mRecyclerViewItems;


    RecyclerViewAdapter(Context context, List<Object> recyclerViewItems) {
        this.mContext = context;
        this.mRecyclerViewItems = recyclerViewItems;
    }


    public static class MenuItemViewHolder extends RecyclerView.ViewHolder {
        private final TextView menuItemName;
        private final TextView menuItemCategory;
        private final ImageView menuItemImage;
        MenuItemViewHolder(View view) {
            super(view);
            menuItemImage = view.findViewById(R.id.itemImage);
            menuItemCategory = view.findViewById(R.id.itemCategory);
            menuItemName = view.findViewById(R.id.itemTitle);
        }
    }

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

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        switch (viewType) {
            case UNIFIED_NATIVE_AD_VIEW_TYPE:
                View unifiedNativeLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.ad_unified,viewGroup,false);
                return new MenuItemViewHolder(unifiedNativeLayoutView);
            case MENU_ITEM_VIEW_TYPE:
                // Fall through.
            default:
                View menuItemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                        R.layout.item_view, viewGroup, false);
                return new MenuItemViewHolder(menuItemLayoutView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        switch (viewType) {
            case UNIFIED_NATIVE_AD_VIEW_TYPE:

                break;
            case MENU_ITEM_VIEW_TYPE:

            default:
                MenuItemViewHolder menuItemHolder = (MenuItemViewHolder) holder;
                final MenuItem menuItem = (MenuItem) mRecyclerViewItems.get(position);
                String imageUrl = "http://friendvilla.in/appog/upload/thumbs/" + menuItem.getThumbnail();
                Picasso.get().load(imageUrl).placeholder(R.drawable.view).fit().into(menuItemHolder.menuItemImage);
                menuItemHolder.menuItemName.setText(menuItem.getTitle());
                switch (menuItem.getCategory()) {
                    case "8":
                        menuItemHolder.menuItemCategory.setText("Recharge Offers - " + menuItem.getDate());
                        break;
                    case "9":
                        menuItemHolder.menuItemCategory.setText("Shopping Offers - " + menuItem.getDate());
                        break;
                    case "10":
                        menuItemHolder.menuItemCategory.setText("App Offers - " + menuItem.getDate());
                        break;
                    case "11":
                        menuItemHolder.menuItemCategory.setText("Special Offers - " + menuItem.getDate());
                        break;
                    case "12":
                        menuItemHolder.menuItemCategory.setText("Other Offers - " + menuItem.getDate());
                        break;
                    default:
                        menuItemHolder.menuItemCategory.setText("OfferG Special - " + menuItem.getDate());
                        break;
                }
                menuItemHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent i = new Intent(mContext,DetailViewActivity.class);
                        i.putExtra("Category",menuItem.getCategory());
                        i.putExtra("Title",menuItem.getTitle());
                        i.putExtra("Details",menuItem.getDescription());
                        i.putExtra("Date",menuItem.getDate());
                        i.putExtra("Image",menuItem.getThumbnail());
                        mContext.startActivity(i);
                    }
                });
        }
    }

    void addMoreData(List<Object> mRecyclerViewItem) {
        mRecyclerViewItems.addAll(mRecyclerViewItem);
        notifyDataSetChanged();
    }
}

