package ch.pizzalemon.android.adapter.recycler;

import android.content.Context;
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.model.PersonalCouponModel;

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

    private Context context;
    private ArrayList<PersonalCouponModel> personalCouponCodeList;

    public static class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.personalCouponCodeTextView) TextView personalCouponCodeTextView;
        @BindView(R.id.personalCouponTotalAmountTextView) TextView personalCouponTotalAmountTextView;
        @BindView(R.id.personalCouponUsedAmountTextView) TextView personalCouponUsedAmountTextView;
        @BindView(R.id.personalCouponRemainingAmountTextView) TextView personalCouponRemainingAmountTextView;
        @BindView(R.id.personalCouponStatusTextView) TextView personalCouponStatusTextView;

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

    public PersonalCouponCodesRecyclerAdapter(Context context,
                                              ArrayList<PersonalCouponModel> personalCouponCodeList){
        this.context = context;
        this.personalCouponCodeList = personalCouponCodeList;
    }

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

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

        PersonalCouponModel couponModel = personalCouponCodeList.get(position);

        holder.personalCouponCodeTextView.setText(couponModel.getCode());
        holder.personalCouponTotalAmountTextView.setText(couponModel.getAmount());
        holder.personalCouponRemainingAmountTextView.setText(couponModel.getTotal());

        double totalAmount = toDoubleSafe(PriceHelper.removeCurrencyFromPrice(couponModel.getAmount()));
        double remainingAmount = toDoubleSafe(PriceHelper.removeCurrencyFromPrice(couponModel.getTotal()));
        double usedAmount = totalAmount - remainingAmount;
        holder.personalCouponUsedAmountTextView.setText(doubleToStringWithCurrency(usedAmount));

        String statusMessage = "";
        if(personalCouponCodeList.get(position).isActive()){
            statusMessage = context.getString(R.string.yes);
        }
        else {
            statusMessage = context.getString(R.string.no);
        }
        holder.personalCouponStatusTextView.setText(statusMessage);
    }

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

    private double toDoubleSafe(String stringToDouble){
        double d = 0;
        try{
            d = Double.parseDouble(stringToDouble);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return d;
    }

    private String doubleToStringWithCurrency(double d){
        String dString = String.valueOf(d);
        int index = dString.indexOf(".");
        if(index != -1 && dString.length() - index == 2){
            dString = dString.concat("0");
        }
        return "CHF " + dString;
    }

}
