package com.smaato.sdk.demoapp;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;

public class AdConfigAdapter extends RecyclerView.Adapter<AdConfigAdapter.ConfigViewHolder> {
    @NonNull
    private final List<AdConfig> items;
    private final int numberOfDefaultItems;
    @Nullable
    private ItemClickListener itemClickListener;

    public AdConfigAdapter(@NonNull List<AdConfig> items, int numberOfDefaultItems) {
        this.items = items;
        this.numberOfDefaultItems = numberOfDefaultItems;
    }

    @Override
    @NonNull
    public ConfigViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_recycleview_row, parent, false);

        return new ConfigViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ConfigViewHolder holder, int position) {
        holder.adName.setText(items.get(position).name);
        holder.adName.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(items.get(position));
            }
        });

        holder.edit.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onItemEditClick(items.get(position));
            }
        });

        holder.delete.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onItemDeleteClick(items.get(position));
            }
        });

        if (position < numberOfDefaultItems) {
            holder.edit.setVisibility(View.GONE);
            holder.delete.setVisibility(View.GONE);
        }
    }

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

    public void addItem(@NonNull AdConfig item) {
        items.add(item);
        notifyDataSetChanged();
    }

    public void remove(@NonNull AdConfig position) {
        items.remove(position);
        notifyDataSetChanged();
    }

    public void setItemClickListener(@NonNull ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void updateItem(@NonNull AdConfig oldConfig, @NonNull AdConfig newConfig) {
        items.set(items.indexOf(oldConfig), newConfig);
        notifyDataSetChanged();
    }

    static class ConfigViewHolder extends RecyclerView.ViewHolder {
        @NonNull
        private TextView adName;
        @NonNull
        private ImageButton edit;
        @NonNull
        private ImageButton delete;

        ConfigViewHolder(@NonNull View view) {
            super(view);
            adName = view.findViewById(R.id.item);
            edit = view.findViewById(R.id.edit);
            delete = view.findViewById(R.id.delete);
        }
    }

    public interface ItemClickListener {
        void onItemClick(@NonNull AdConfig adConfig);

        void onItemEditClick(@NonNull AdConfig adConfig);

        void onItemDeleteClick(@NonNull AdConfig adConfig);
    }
}