package kz.arta.synergy.workflow.mobile.android.users.adapters;

/**
 * Created by neox on 18.04.14.
 * Адаптер списка пользователей с возможностью мультивыбора
 */

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;
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.common.utils.Utils;
import kz.arta.synergy.workflow.mobile.android.users.models.UserItem;

public class AdapterUsersMulti extends ArrayAdapter<UserItem> {

    private Activity activity;
    private List<UserItem> users;
    private List<String> uuid = new ArrayList<>();
    private List<String> names = new ArrayList<>();
    private List<String> selectedUsers;
    private AdapterSelectedUsers adapterSelectedUsers;
    private GridView selectedUsersGridView;
    private TextView showMoreUsers;

    public AdapterUsersMulti(Activity activity, List<UserItem> users, List<String> selectedUsers,
                             AdapterSelectedUsers adapterSelectedUsers, GridView selectedUsersGridView, TextView showMoreUsers) {
        super(activity, R.layout.user_chooser_activity, users);
        this.users = users;
        this.activity = activity;
        this.selectedUsers = selectedUsers;
        this.adapterSelectedUsers = adapterSelectedUsers;
        this.selectedUsersGridView = selectedUsersGridView;
        this.showMoreUsers = showMoreUsers;
    }

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

        LayoutInflater inflater = LayoutInflater.from(activity);
        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).getName());
        userName.setTypeface(Fonts.getRoman(activity));

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

        ImageView checker = (ImageView) someView.findViewById(R.id.item_users_checker_image);
        if (users.get(position).isChecked()) {
            checker.setVisibility(View.VISIBLE);
        } else {
            checker.setVisibility(View.GONE);
        }

        return someView;
    }

    /**
     * Метод обновляет данные списков
     *
     * @param position позиция пользователя
     */
    public void update(int position) {
        boolean checked = users.get(position).isChecked();
        if (checked) {
            for (String user : selectedUsers) {
                if (user.equals(users.get(position).getName())) {
                    return;
                }
            }
            uuid.add(users.get(position).getUserID());
            names.add(users.get(position).getName());
            selectedUsers.add(users.get(position).getName());
            adapterSelectedUsers.notifyDataSetChanged();
        } else {
            Iterator iterator = uuid.iterator();
            while (iterator.hasNext()) {
                if (iterator.next().equals(users.get(position).getUserID())) {
                    iterator.remove();
                }
            }
            iterator = names.iterator();
            while (iterator.hasNext()) {
                if (iterator.next().equals(users.get(position).getName())) {
                    iterator.remove();
                }
            }
            iterator = selectedUsers.iterator();
            while (iterator.hasNext()) {
                if (iterator.next().equals(users.get(position).getName())) {
                    iterator.remove();
                    adapterSelectedUsers.notifyDataSetChanged();
                }
            }
        }
        if (selectedUsers.size() > selectedUsersGridView.getNumColumns()) {
            Utils.gridViewHeight(selectedUsersGridView, activity);
            showMoreUsers.setVisibility(View.VISIBLE);
            showMoreUsers.setText(activity.getString(R.string.and_again) + " " +
                    String.valueOf(selectedUsers.size() - selectedUsersGridView.getNumColumns()) + " " + activity.getString(R.string.others_users));
        } else {
            Utils.gridViewHeightWrap(selectedUsersGridView);
            showMoreUsers.setVisibility(View.GONE);
        }
    }

    public List<UserItem> getUsers() {
        return users;
    }

    public List<String> getUuid() {
        return uuid;
    }

    public List<String> getNames() {
        return names;
    }

    @Override
    public int getCount() {
        return users.size();
    }

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