package ch.threema.app.location;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.List;
import java.util.Locale;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import ch.threema.app.R;
import ch.threema.app.ThreemaApplication;
import ch.threema.app.ui.EmptyRecyclerView;

import static ch.threema.common.JavaCompat.isNullOrEmpty;

public class LocationAutocompleteAdapter extends EmptyRecyclerView.Adapter<EmptyRecyclerView.ViewHolder> {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;

    private List<NamedPoi> places;
    private OnItemClickListener onItemClickListener;

    LocationAutocompleteAdapter(List<NamedPoi> places) {
        this.places = places;
    }

    public void setPlaces(@NonNull List<NamedPoi> places) {
        this.places = places;
    }

    @Override
    public void onBindViewHolder(@NonNull EmptyRecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PlacesViewHolder) {
            ((PlacesViewHolder) holder).onBind(position);
            if (this.onItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (places != null && position < places.size()) {
                            onItemClickListener.onClick(places.get(position), position);
                        }
                    }
                });
            }
        }
    }

    @NonNull
    @Override
    public EmptyRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            return new PlacesViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_location_picker_place, parent, false));
        } else {
            return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_location_picker_copyright, parent, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position >= places.size() ? TYPE_FOOTER : TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        if (places != null && !places.isEmpty()) {
            return places.size() + 1;
        } else {
            return 0;
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public class PlacesViewHolder extends EmptyRecyclerView.ViewHolder {
        private int currentPosition;
        TextView name, description, distance;

        PlacesViewHolder(View itemView) {
            super(itemView);

            name = itemView.findViewById(R.id.name);
            description = itemView.findViewById(R.id.address);
            distance = itemView.findViewById(R.id.distance);
        }

        protected void clear() {
            name.setText("");
            description.setText("");
            distance.setText("");
        }

        void onBind(int position) {
            currentPosition = position;
            clear();

            final NamedPoi place = places.get(position);

            name.setText(place.getName());

            description.setText(getLocalizedDescription(ThreemaApplication.getAppContext(), place.getDescription()));

            String pattern = "#.#";
            if (place.getDistance() > 10000) {
                pattern = "#,###";
            }
            String distanceS = new DecimalFormat(pattern + " km", DecimalFormatSymbols.getInstance(Locale.getDefault())).format((float) place.getDistance() / 1000);
            distance.setText(distanceS);
        }
    }

    private @NonNull String getLocalizedDescription(Context context, String id) {
        if (!isNullOrEmpty(id)) {
            @StringRes int resId = context.getResources().getIdentifier(id, "string", context.getPackageName());

            if (resId != 0) {
                String value = context.getString(resId);
                if (!isNullOrEmpty(value)) {
                    return value;
                }
            }
        }
        return "";
    }

    public class FooterViewHolder extends EmptyRecyclerView.ViewHolder {
        FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(NamedPoi poi, int position);
    }
}
