package ch.pizzalemon.android.adapter.recycler;

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 java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.interfaces.RecyclerItemClickListener;
import ch.pizzalemon.android.model.AddressModel;

/**
 * Created by cimenmus on 24.10.2017.
 */

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

    private final int HOLDER_ADDRESS = 0;
    private final int HOLDER_SPACE = 1;

    private ArrayList<AddressModel> addressList = new ArrayList<>();
    private RecyclerItemClickListener recyclerItemClickListener;

    public static class OrderViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.addressTextView) TextView addressTextView;
        @BindView(R.id.deleteAddressImageView) ImageView deleteAddressImageView;

        public OrderViewHolder(final View view, final RecyclerItemClickListener recyclerItemClickListener) {
            super(view);
            ButterKnife.bind(this, view);
            deleteAddressImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(recyclerItemClickListener != null)
                        recyclerItemClickListener.onItemClick(deleteAddressImageView, getAdapterPosition());
                }
            });
        }
    }

    public static class SpaceViewHolder extends RecyclerView.ViewHolder{
        public SpaceViewHolder(final View view) {
            super(view);
        }
    }

    @Override
    public int getItemViewType(int position) {

        if(position == addressList.size())
            return HOLDER_SPACE;

        return HOLDER_ADDRESS;
    }

    public MyAddressesRecyclerAdapter(ArrayList<AddressModel> addressList,
                                      RecyclerItemClickListener recyclerItemClickListener){
        this.addressList = addressList;
        this.recyclerItemClickListener = recyclerItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        RecyclerView.ViewHolder viewHolder;
        View view;
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        switch (viewType){

            case HOLDER_ADDRESS:
                view = inflater.inflate(R.layout.row_my_address, viewGroup, false);
                viewHolder = new OrderViewHolder(view, recyclerItemClickListener);
                break;

            case HOLDER_SPACE:
                view = inflater.inflate(R.layout.row_space, viewGroup, false);
                viewHolder = new SpaceViewHolder(view);
                break;

            default:
                view = inflater.inflate(R.layout.row_my_address, viewGroup, false);
                viewHolder = new OrderViewHolder(view, recyclerItemClickListener);
                break;
        }

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (holder.getItemViewType()){
            case HOLDER_ADDRESS :
                OrderViewHolder orderViewHolder = (OrderViewHolder) holder;
                orderViewHolder.addressTextView.setText(addressList.get(position).getAddress());
                break;

            case HOLDER_SPACE :
                SpaceViewHolder spaceViewHolder = (SpaceViewHolder) holder;
                break;
        }
    }

    @Override
    public int getItemCount() {
        return addressList.size() + 1 ;
    }
}