/*
 * DSBDirect
 * Copyright (C) 2019 Fynn Godau
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * This software is not affiliated with heinekingmedia GmbH, the
 * developer of the DSB platform.
 */

package godau.fynn.dsbdirect;

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

import java.util.ArrayList;

import godau.fynn.dsbdirect.Entry;
import godau.fynn.dsbdirect.R;


public class Adapter extends ArrayAdapter<String> {

    private final Context mContext;
    private final ArrayList<Entry> mValues;

    public Adapter(Context context, ArrayList<Entry> values) {
        super(context, R.layout.rowlayout,
                new String[values.size()] /* contents don't matter, but this is used to determine
                                           * amount of fields */);

        mContext = context;
        mValues = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        Entry entry = mValues.get(position);

        // recycle stuff – I don't know how it works but it reduces lag
        View rowView;
        if (convertView != null) {
            rowView = convertView;
        } else {
            rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        }



        // populate all fields
        fillField(entry.getAffectedClass(), (TextView) rowView.findViewById(R.id.affectedClass));
        fillField(entry.getLesson(), (TextView) rowView.findViewById(R.id.lesson));
        fillField(entry.getReplacementTeacher(), (TextView) rowView.findViewById(R.id.replacementTeacher));
        fillField(entry.getInfo(), (TextView) rowView.findViewById(R.id.info));


        TextView dateView = rowView.findViewById(R.id.date);
        try {
            if (position != 0 && !mValues.get(position - 1).getDate().equals(entry.getDate())) {
                String date = Utility.formatDate(entry.getDate());
                dateView.setText(date);
                dateView.setVisibility(View.VISIBLE);
            } else {
                dateView.setVisibility(View.GONE);
            }
        } catch (NullPointerException e) {
            e.printStackTrace(); // Bad parser
        }


        return rowView;
    }

    private void fillField(String s, TextView v) {
        if (!s.equals("")) {
            v.setVisibility(View.VISIBLE);
            v.setText(s);
        } else {
            v.setVisibility(View.GONE);
        }
    }
}