package hh.dejsem.fm;

import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.DateFormat;
import java.util.Date;

import hh.dejsem.R;
import hh.lib.D;

/**
 * adapter for file list display
 */
class FileListAdapter extends BaseAdapter {

	FileList fileList;      // seznam souborů ve formě TreeMap<String, FileAttrs>

	private D d;
	private DateFormat df;

	FileListAdapter(D d, FileList fileList) {
		this.d = d.klon(this);
		this.fileList = fileList;
		df = android.text.format.DateFormat.getDateFormat(this.d.getContext());
	}

	public int getCount() { return fileList.ls.length; }

	public Object getItem(int position) { return null; }

	public long getItemId(int position) { return position; }

	public View getView(int position, View listEntry, ViewGroup parent) {
		LinearLayout v = (LinearLayout) LayoutInflater.from(d.getContext()).inflate(R.layout.file_entry, null);
		v.setId(position);
		final FileAttrs attrs = fileList.get(fileList.ls[position]);
		TextView fname = (TextView)(v.findViewById(R.id.fname));
		fname.setText(attrs.name);
		final CheckBox chkbox = (CheckBox)(v.findViewById(R.id.fcb));
		chkbox.setChecked(attrs.isChecked());
		attrs.cb = chkbox;
		final ImageView icon = (ImageView)(v.findViewById(R.id.fic));
		if(attrs.isDir())  icon.setImageResource(R.drawable.ic_folder);
		else icon.setImageResource(R.drawable.ic_file);
		final TextView fsize = (TextView)(v.findViewById(R.id.fsize));
		fsize.setText(attrs.isFile() ? Formatter.formatFileSize(d.getContext(), attrs.size) : "");
		final TextView fdate = (TextView)(v.findViewById(R.id.fdate));
		fdate.setText("" + df.format(new Date(attrs.date)));
		return v;
	}
}
