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

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

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

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

    private ArrayList<CartProductModel> cartProductList = new ArrayList<>();
    private RecyclerItemClickListener recyclerItemClickListener;

    public static class CartProductViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.cartProductCountTextView) TextView cartProductCountTextView;
        @BindView(R.id.cartProductCategoryNameTextView) TextView cartProductCategoryNameTextView;
        @BindView(R.id.cartProductNameTextView) TextView cartProductNameTextView;
        @BindView(R.id.cartProductTotalPriceTextView) TextView cartProductTotalPriceTextView;
        @BindView(R.id.removeProductFromCartImageView) ImageView removeProductFromCartImageView;
        @BindView(R.id.cartProductInfoTextView) TextView cartProductInfoTextView;

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

        }
    }

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

    @Override
    public int getItemViewType(int position) {
        if(position == cartProductList.size())
            return HOLDER_SPACE;
        return HOLDER_CART_PRODUCT;
    }

    public CartRecyclerAdapter(ArrayList<CartProductModel> cartProductList,
                                       RecyclerItemClickListener recyclerItemClickListener){
        this.cartProductList = cartProductList;
        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_CART_PRODUCT:
                view = inflater.inflate(R.layout.row_cart, viewGroup, false);
                viewHolder = new CartProductViewHolder(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_cart, viewGroup, false);
                viewHolder = new CartProductViewHolder(view, recyclerItemClickListener);
                break;
        }

        return viewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        switch (holder.getItemViewType()){
            case HOLDER_CART_PRODUCT :
                CartProductViewHolder cartProductViewHolder = (CartProductViewHolder) holder;
                cartProductViewHolder.cartProductCountTextView.setText(cartProductList.get(position).getQuantity() + " x ");
                cartProductViewHolder.cartProductCategoryNameTextView.setText(cartProductList.get(position).getModel());
                cartProductViewHolder.cartProductNameTextView.setText(cartProductList.get(position).getName());
                cartProductViewHolder.cartProductTotalPriceTextView.setText(PriceHelper.roundFractions(cartProductList.get(position).getTotal()));
                setCartInfoText(cartProductViewHolder.cartProductInfoTextView, cartProductList.get(position));
                break;

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

    }

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

    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();
    }

    /*
    private String getCartInfoText(CartProductModel cartProductModel){

        StringBuilder stringBuilder = new StringBuilder();

        for(CartProductOptionModel cartProductOptionModel : cartProductModel.getOption()){
            if(!cartProductOptionModel.getType().toLowerCase().equals("checkbox")){
                stringBuilder.append(cartProductOptionModel.getValue())
                        .append("\n");
            }
        }

        // stringBuilder.append("\n");

        for(CartProductOptionModel cartProductOptionModel : cartProductModel.getOption()){
            if(cartProductOptionModel.getType().toLowerCase().equals("checkbox")){
                stringBuilder.append(cartProductOptionModel.getValue())
                        .append("\n");
            }
        }

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

}
