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.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ch.pizzalemon.android.R;
import ch.pizzalemon.android.helper.PriceHelper;
import ch.pizzalemon.android.interfaces.RecyclerItemClickListener;
import ch.pizzalemon.android.model.history.OrderHistoryModel;

/**
 * Created by cimenmus on 04/10/2017.
 */

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

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

    private ArrayList<OrderHistoryModel> orderHistoryList = new ArrayList<>();
    private RecyclerItemClickListener recyclerItemClickListener;

    public static class OrderViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.orderDateTextView) TextView orderDateTextView;
        @BindView(R.id.orderTotalTextView) TextView orderTotalTextView;
        @BindView(R.id.repeatOrderTextView) TextView repeatOrderTextView;
        //@BindView(R.id.orderStatusTextView) TextView orderStatusTextView;

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

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

    @Override
    public int getItemViewType(int position) {

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

        return HOLDER_ORDER;
    }

    public OrderHistoryRecyclerAdapter(ArrayList<OrderHistoryModel> orderHistoryList,
                                RecyclerItemClickListener recyclerItemClickListener){
        this.orderHistoryList = orderHistoryList;
        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_ORDER:
                view = inflater.inflate(R.layout.row_order_history, 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_order_history, 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_ORDER :
                OrderViewHolder orderViewHolder = (OrderViewHolder) holder;
                orderViewHolder.orderDateTextView.setText(orderHistoryList.get(position).getFormattedCreateDate());
                orderViewHolder.orderTotalTextView.setText(PriceHelper.roundFractions(orderHistoryList.get(position).getTotalString()));
                //orderViewHolder.orderStatusTextView.setText(orderHistoryList.get(position).getStatus());
                break;

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

    }

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

    private String createOrderInfoText(OrderHistoryModel orderHistoryModel){
        return new StringBuilder()
                .append(orderHistoryModel.getFormattedCreateDate())
                .append("\n\n")
                .append(orderHistoryModel.getStatus())
                .toString();
    }

}