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.activity.BaseActivity;
import ch.pizzalemon.android.helper.PriceHelper;
import ch.pizzalemon.android.helper.TextHelper;
import ch.pizzalemon.android.model.cart.CartProductModel;

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

    private ArrayList<CartProductModel> cartProductList;

    public static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.productCountTextView) TextView productCountTextView;
        @BindView(R.id.productNameTextView) TextView productNameTextView;
        @BindView(R.id.productPropertiesTextView) TextView productPropertiesTextView;
        @BindView(R.id.productPriceTextView) TextView productPriceTextView;

        public ViewHolder(final View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public ProductOnOrderSummaryRecyclerAdapter(ArrayList<CartProductModel> cartProductList){
        this.cartProductList = cartProductList;
    }

    @Override
    public ProductOnOrderSummaryRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View root = LayoutInflater.from(BaseActivity.currentActivity).inflate(R.layout.row_product_on_order_summary, viewGroup, false);
        return new ProductOnOrderSummaryRecyclerAdapter.ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(ProductOnOrderSummaryRecyclerAdapter.ViewHolder holder, int position) {
        holder.productCountTextView.setText(cartProductList.get(position).getQuantity());
        holder.productNameTextView.setText(cartProductList.get(position).getName());
        holder.productPriceTextView.setText(PriceHelper.roundFractions(cartProductList.get(position).getTotal()));
        setCartInfoText(holder.productPropertiesTextView, cartProductList.get(position));
    }

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

    private void setCartInfoText(TextView textView, CartProductModel cartProductModel){

        if(cartProductModel.getOption() == null || cartProductModel.getOption().size() == 0){
            textView.setVisibility(View.GONE);
            return;
        }

        textView.setVisibility(View.VISIBLE);
        TextHelper.setTextFromHTML(textView, getCartInfoText(cartProductModel));
    }

    private String getCartInfoText(CartProductModel cartProductModel){

        StringBuilder stringBuilder = new StringBuilder();

        for(int i = 0; i < cartProductModel.getOption().size(); i++){

            if(!stringBuilder.toString().contains(cartProductModel.getOption().get(i).getName())){

                /*
                if(!stringBuilder.toString().isEmpty()){
                    stringBuilder.append("<br/>");
                }
                */
                stringBuilder
                        .append("<b>")
                        .append("<u>")
                        .append(cartProductModel.getOption().get(i).getName())
                        .append("</u>")
                        .append("</b>")
                        //.append("<br/>")
                        .append("<br/>");
            }
            stringBuilder
                    .append(cartProductModel.getOption().get(i).getValue())
                    .append("<br/>");
        }

        return stringBuilder.toString().trim();
    }

}