package com.cygnusa.hyperm.adapter;

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

import com.bumptech.glide.Glide;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.cygnusa.hyperm.R;
import com.cygnusa.hyperm.wrappers.DSE;

import java.util.ArrayList;


public class AttendanceAdapter extends BaseAdapter {

    private ArrayList<DSE> items;
    private LayoutInflater inflater;
    private String organisor;
    private Context context;
    private String profile;

    public AttendanceAdapter(Context context, ArrayList<DSE> items, String organisor, String profile) {
        this.items = items;
        inflater = LayoutInflater.from(context);
        this.organisor = organisor;
        this.context = context;
        this.profile = profile;
    }

    @Override
    public int getCount() {
        return items.size() + 1;
    }

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

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.attendance_item, null);
            holder.name = convertView.findViewById(R.id.name);
            holder.status = convertView.findViewById(R.id.status);
            holder.userIcon = convertView.findViewById(R.id.userIcon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == 0) {
            holder.name.setText(organisor);
            holder.status.setVisibility(View.VISIBLE);
            holder.name.setTextColor(Color.BLACK);
            Glide.with(context).load(profile)
                    .thumbnail(Glide.with(context).load(R.drawable.gif_loader))
                    .into(holder.userIcon);
        } else {
            holder.name.setText(items.get(position - 1).getName());
            Glide.with(context).load(items.get(position - 1).getProfile())
                    .thumbnail(Glide.with(context).load(R.drawable.gif_loader))
                    .into(holder.userIcon);

            holder.status.setVisibility(View.GONE);
            if (items.get(position - 1).isActive()) {
                holder.name.setTextColor(Color.BLACK);
            } else {
                holder.name.setTextColor(Color.parseColor("#9e9e9e"));
            }
        }

        return convertView;
    }

    static class ViewHolder {
        TextView name, status;
        CircularImageView userIcon;
    }
}
