package kz.arta.synergy.workflow.mobile.android.forms.general.components.users.adapters;

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

import java.util.List;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;
import kz.arta.synergy.workflow.mobile.android.forms.general.components.users.model.UserModel;

/**
 * Created by neox on 14.04.14.
 * Адаптер списка пользователей
 */
public class UsersChooserAdapter extends ArrayAdapter<UserModel> {

    /**
     * Список пользователей
     */
    private List<UserModel> users;

    /**
     * Контекст
     */
    private Context context;

    /**
     * Используется ли мультивыбор
     */
    private boolean multiSelect;

    /**
     * Адаптер списка пользователей
     *
     * @param context контекст
     * @param users   список пользователей
     */
    public UsersChooserAdapter(Context context, List<UserModel> users, boolean multiSelect) {
        super(context, R.layout.users_chooser_activity, users);
        this.context = context;
        this.users = users;
        this.multiSelect = multiSelect;
    }

    @Override
    public View getView(int position, View someView, ViewGroup arg2) {

        LayoutInflater inflater = LayoutInflater.from(context);

        if (someView == null) {
            someView = inflater.inflate(R.layout.item_users_list, arg2, false);
        }

        TextView userName = (TextView) someView.findViewById(R.id.item_users_name);
        userName.setText(users.get(position).getPersonName());
        userName.setTypeface(Fonts.getRoman(context));

        TextView userPosition = (TextView) someView.findViewById(R.id.item_users_position);
        userPosition.setText(users.get(position).getPositionName());
        userPosition.setTypeface(Fonts.getRoman(context));

        TextView customText = (TextView) someView.findViewById(R.id.item_users_custom_text);
        customText.setText(users.get(position).getPositionName());
        customText.setTypeface(Fonts.getRoman(context));

        if (users.get(position).getCustomFields() != null && users.get(position).getCustomFields().getCalendarStatusLabel() != null) {
            customText.setVisibility(View.VISIBLE);
            customText.setText(users.get(position).getCustomFields().getCalendarStatusLabel());
            customText.setTextColor(Color.parseColor(users.get(position).getCustomFields().getCalendarColor()));
        } else {
            customText.setVisibility(View.GONE);
        }

        ImageView checkerBackground = (ImageView) someView.findViewById(R.id.item_users_checkbox_image);
        ImageView checker = (ImageView) someView.findViewById(R.id.item_users_checker_image);

        if (multiSelect) {
            checker.setVisibility((users.get(position).isChecked() ? View.VISIBLE : View.GONE));
        } else {
            checker.setVisibility(View.GONE);
            checkerBackground.setVisibility(View.GONE);
            someView.setBackgroundColor(context.getResources().getColor((users.get(position).isChecked()
                    ? R.color.selected_item_list_color : android.R.color.transparent)));
        }
        return someView;
    }
}