package de.sebse.fuplanner.fragments.canteen;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.sebse.fuplanner.R;
import de.sebse.fuplanner.services.Canteen.types.Day;
import de.sebse.fuplanner.services.Canteen.types.Meal;
import de.sebse.fuplanner.tools.Preferences;
import de.sebse.fuplanner.tools.ui.MealViewHolder;
import de.sebse.fuplanner.tools.ui.StringViewHolder;

class MealAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private String[] CATEGORY_KEYS = new String[]{"Essen", "Aktionen", "Beilagen", "Desserts", "Salate", "Suppen", "Vorspeisen"};
    @StringRes
    private int[] CATEGORY_VALS = new int[]{R.string.meals, R.string.special_meals, R.string.side_dishes, R.string.desserts, R.string.salats, R.string.soups, R.string.starters};
    @StringRes
    private int CATEGORY_OTHER = R.string.others;
    private ArrayList<Object> matchings = new ArrayList<>();

    private Day mDay = null;
    private final Context mContext;

    public MealAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.list_canteen_items, viewGroup, false);
            return new MealViewHolder(view);
        } else {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.list_all_caption, viewGroup, false);
            return new StringViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType() == 0) {
            MealViewHolder viewHolder = ((MealViewHolder) holder);
            viewHolder.reset();
            Meal meal = getMeal(position);
            viewHolder.mTitle.setText(meal.getName());
            String value;
            switch (Preferences.getString(mContext, R.array.pref_price_group)) {
                case "student":
                    value = mContext.getString(R.string.price, meal.getPriceStdnt());
                    break;
                case "employee":
                    value = mContext.getString(R.string.price, meal.getPriceEmply());
                    break;
                case "other":
                    value = mContext.getString(R.string.price, meal.getPriceOther());
                    break;
                default:
                    value = mContext.getString(R.string.prices, meal.getPriceStdnt(), meal.getPriceEmply(), meal.getPriceOther());
            }
            viewHolder.mSubTitle.setText(value);
            StringBuilder string = new StringBuilder();
            List<String> notes = meal.getNotes();
            for (int i1 = 0, notesSize = notes.size(); i1 < notesSize; i1++) {
                if (i1 != 0)
                    string.append("\n");
                String s = notes.get(i1);
                string.append(" - ").append(s);
            }
            viewHolder.mNotes.setText(string.toString());
            viewHolder.mCategory.setText(meal.getCategory());
            viewHolder.mIconVegan.setVisibility(meal.getVegan() == Meal.VEGAN_VEGAN ? View.VISIBLE : View.GONE);
            viewHolder.mIconVegetarian.setVisibility(meal.getVegan() == Meal.VEGAN_VEGETERIAN ? View.VISIBLE : View.GONE);
            viewHolder.mIconBio.setVisibility((meal.getCertificates() & Meal.CERT_BIO) != 0 ? View.VISIBLE : View.GONE);
            viewHolder.mIconMsc.setVisibility((meal.getCertificates() & Meal.CERT_MSC) != 0 ? View.VISIBLE : View.GONE);
        } else {
            StringViewHolder viewHolder = ((StringViewHolder) holder);
            viewHolder.mString.setText(getHeading(position));
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        return matchings.get(position) instanceof String ? 1 : 0;
    }

    public Meal getMeal(int position) {
        if (this.mDay != null)
            return this.mDay.get((Integer) matchings.get(position));
        else
            return null;
    }

    public String getHeading(int position) {
        return (String) matchings.get(position);
    }

    public void setDay(Day day) {
        this.mDay = day;
        this.setDay();
    }

    public void setDay() {
        HashMap<String, ArrayList<Integer>> map = new HashMap<>();
        for (int i = 0; i < this.mDay.size(); i++) {
            String category = this.mDay.get(i).getCategory();
            boolean found = false;
            for (String category_key : CATEGORY_KEYS) {
                if (category_key.equals(category)) {
                    found = true;
                    break;
                }
            }
            if (!found) category = "---";
            ArrayList<Integer> list = map.get(category);
            if (list == null) {
                list = new ArrayList<>();
                map.put(category, list);
            }
            list.add(i);
        }
        matchings.clear();
        for (int i = 0; i < CATEGORY_KEYS.length; i++) {
            ArrayList<Integer> list = map.get(CATEGORY_KEYS[i]);
            if (list != null) {
                matchings.add(mContext.getString(CATEGORY_VALS[i]));
                matchings.addAll(list);
            }
        }
        ArrayList<Integer> list = map.get("---");
        if (list != null) {
            matchings.add(mContext.getString(CATEGORY_OTHER));
            matchings.addAll(list);
        }
        this.notifyDataSetChanged();
    }
}