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

import android.content.Context;
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.DepartmentModel;

/**
 * Created by neox on 05.04.16.
 * Адаптер для выбора департаментов
 */
public class DepartmentsChooserAdapter extends ArrayAdapter<DepartmentModel> {

    /**
     * Список департаментов
     */
    private List<DepartmentModel> departments;

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

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

    /**
     * Конструктор адаптера департаментов
     *
     * @param context     контекст
     * @param departments список департаментов
     */
    public DepartmentsChooserAdapter(Context context, List<DepartmentModel> departments, boolean multiSelect) {
        super(context, R.layout.users_chooser_activity, departments);
        this.context = context;
        this.departments = departments;
        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(departments.get(position).getDepartmentName());
        userName.setTypeface(Fonts.getRoman(context));

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

        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((departments.get(position).isChecked() ? View.VISIBLE : View.GONE));
        } else {
            checker.setVisibility(View.GONE);
            checkerBackground.setVisibility(View.GONE);
            someView.setBackgroundColor(context.getResources().getColor((departments.get(position).isChecked()
                    ? R.color.selected_item_list_color : android.R.color.transparent)));
        }

        return someView;
    }
}