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

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;

/**
 * Created by neox on 30.05.16.
 * Адаптер для списка в адресной книге
 */
public class AddressBookAdapter extends ArrayAdapter<AddressBookModel> {

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

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

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

    @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);
        }

        someView.setBackgroundColor(context.getResources().getColor((list.get(position).isSelected()
                ? R.color.selected_item_list_color : android.R.color.transparent)));

        TextView userName = (TextView) someView.findViewById(R.id.item_users_name);
        userName.setTypeface(Fonts.getRoman(context));

        TextView userPosition = (TextView) someView.findViewById(R.id.item_users_position);
        userPosition.setTypeface(Fonts.getRoman(context));

        if (list.get(position).getItemType().equals(AddressBookConst.ITEM_TYPE_PERSON)) {
            userName.setText(list.get(position).getLastName() + " " + list.get(position).getFirstName() + " " + list.get(position).getPatronymic());
            if (list.get(position).getWorkPlaces().length != 0) {
                userPosition.setText(list.get(position).getWorkPlaces()[0].getOrganizationName());
                userPosition.setVisibility(View.VISIBLE);
            } else {
                userPosition.setVisibility(View.GONE);
            }
        } else if (list.get(position).getItemType().equals(AddressBookConst.ITEM_TYPE_ORGANIZATION)) {
            userName.setText(list.get(position).getName());
            AddressBookModel.ItemsGroup[] itemsGroups = list.get(position).getItemsGroup();
            if (itemsGroups.length != 0) {
                String address = "";
                for (AddressBookModel.ItemsGroup group : itemsGroups) {
                    if (group.getCategoryID().equals(AddressBookConst.ADDRESS_ORG)) {
                        if (group.getItems().length != 0) {
                            address = group.getItems()[0].getContact();
                            break;
                        }
                    }
                }
                if (!address.isEmpty()) {
                    userPosition.setVisibility(View.VISIBLE);
                    userPosition.setText(address);
                } else {
                    userPosition.setVisibility(View.GONE);
                }
            } else {
                userPosition.setVisibility(View.GONE);
            }
        }

        ImageView checker = (ImageView) someView.findViewById(R.id.item_users_checkbox_image);
        checker.setVisibility(View.GONE);

        return someView;
    }
}