<commit_before>package org.clintonhealthaccess.lmis.app.adapters;

import android.content.Context;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import org.clintonhealthaccess.lmis.app.R;
import org.clintonhealthaccess.lmis.app.activities.viewmodels.LossesCommodityViewModel;
import org.clintonhealthaccess.lmis.app.activities.viewmodels.LossesViewModelCommands;
import org.clintonhealthaccess.lmis.app.events.CommodityToggledEvent;
import org.clintonhealthaccess.lmis.app.watchers.LmisTextWatcher;

import java.util.List;

import de.greenrobot.event.EventBus;

public class LossesCommoditiesAdapter extends ArrayAdapter<LossesCommodityViewModel> {

    private int resource;

    public LossesCommoditiesAdapter(Context context, int resource, List<LossesCommodityViewModel> commodities) {
        super(context, resource, commodities);

        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(resource, parent, false);

        final LossesCommodityViewModel viewModel = getItem(position);

        TextView textViewCommodityName = (TextView) rowView.findViewById(R.id.textViewCommodityName);
        textViewCommodityName.setText(viewModel.getName());

        EditText editTextWastages = (EditText) rowView.findViewById(R.id.editTextWastages);
        EditText editTextDamages = (EditText) rowView.findViewById(R.id.editTextDamages);
        EditText editTextExpiries = (EditText) rowView.findViewById(R.id.editTextExpiries);
        EditText editTextMissing = (EditText) rowView.findViewById(R.id.editTextMissing);

        preloadDataInto(viewModel, editTextWastages, editTextDamages, editTextExpiries, editTextMissing);

        syncChangesWithViewModelFrom(textViewCommodityName, viewModel, editTextWastages, editTextDamages, editTextExpiries, editTextMissing);

        ImageButton imageButtonCancel = (ImageButton)rowView.findViewById(R.id.imageButtonCancel);
        activateCancelButton(imageButtonCancel, viewModel);
        return rowView;
    }

    private void syncChangesWithViewModelFrom(TextView textViewCommodityName, LossesCommodityViewModel viewModel, EditText editTextWastages, EditText editTextDamages, EditText editTextExpiries, EditText editTextMissing) {
        setupTextWatcher(textViewCommodityName, editTextWastages, new LossesViewModelCommands.SetWastageCommand(), viewModel);
        setupTextWatcher(textViewCommodityName, editTextDamages, new LossesViewModelCommands.SetDamagesCommand(), viewModel);
        setupTextWatcher(textViewCommodityName, editTextExpiries, new LossesViewModelCommands.SetExpiriesCommand(), viewModel);
        setupTextWatcher(textViewCommodityName, editTextMissing, new LossesViewModelCommands.SetMissingCommand(), viewModel);
    }

    private void preloadDataInto(LossesCommodityViewModel viewModel, EditText editTextWastages, EditText editTextDamages, EditText editTextExpiries, EditText editTextMissing) {
        editTextWastages.setText(String.valueOf(viewModel.getWastage()));
        editTextDamages.setText(String.valueOf(viewModel.getDamages()));
        editTextExpiries.setText(String.valueOf(viewModel.getExpiries()));
        editTextMissing.setText(String.valueOf(viewModel.getMissing()));
    }

    private void setupTextWatcher(final TextView textViewCommodityName, final EditText editText, final LossesViewModelCommands.Command command, final LossesCommodityViewModel viewModel) {
        editText.addTextChangedListener(new LmisTextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                command.execute(viewModel, editable);
                int losses = viewModel.totalLosses();
                int stockOnHand = viewModel.getStockOnHand();
                if(losses > stockOnHand) {
                    textViewCommodityName.setError(String.format(getContext().getString(R.string.totalLossesGreaterThanStockAtHand), losses, stockOnHand));
                }else{
                    textViewCommodityName.setError(null);
                }
            }
        });
    }

    private void activateCancelButton(ImageButton imageButtonCancel, final LossesCommodityViewModel viewModel) {
        imageButtonCancel.setOnClickListener(new View.OnClickListener() {
            @Override<commit_msg>Job Nim - refactor to take care of pmd debt<commit_after>package org.clintonhealthaccess.lmis.app.adapters;

import android.content.Context;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import org.clintonhealthaccess.lmis.app.R;
import org.clintonhealthaccess.lmis.app.activities.viewmodels.LossesCommodityViewModel;
import org.clintonhealthaccess.lmis.app.activities.viewmodels.LossesViewModelCommands;
import org.clintonhealthaccess.lmis.app.events.CommodityToggledEvent;
import org.clintonhealthaccess.lmis.app.watchers.LmisTextWatcher;

import java.util.List;

import de.greenrobot.event.EventBus;

public class LossesCommoditiesAdapter extends ArrayAdapter<LossesCommodityViewModel> {

    private final int resource;

    public LossesCommoditiesAdapter(Context context, int resource, List<LossesCommodityViewModel> commodities) {
        super(context, resource, commodities);
        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(resource, parent, false);
        final LossesCommodityViewModel viewModel = getItem(position);
        TextView textViewCommodityName = (TextView) rowView.findViewById(R.id.textViewCommodityName);
        textViewCommodityName.setText(viewModel.getName());

        setUpDamages(textViewCommodityName, viewModel, (EditText) rowView.findViewById(R.id.editTextDamages));
        setUpWastages(textViewCommodityName, viewModel, (EditText) rowView.findViewById(R.id.editTextWastages));
        setUpExpiries(textViewCommodityName, viewModel, (EditText) rowView.findViewById(R.id.editTextExpiries));
        setUpMissing(textViewCommodityName, viewModel, (EditText) rowView.findViewById(R.id.editTextMissing));
        activateCancelButton((ImageButton)rowView.findViewById(R.id.imageButtonCancel), viewModel);
        return rowView;
    }

    private void setUpWastages(TextView textViewCommodityName, LossesCommodityViewModel viewModel, EditText editTextWastages){
        editTextWastages.setText(String.valueOf(viewModel.getWastage()));
        setupTextWatcher(textViewCommodityName, editTextWastages, new LossesViewModelCommands.SetWastageCommand(), viewModel);
    }

    private void setUpDamages(TextView textViewCommodityName, LossesCommodityViewModel viewModel, EditText editTextDamages){
        editTextDamages.setText(String.valueOf(viewModel.getDamages()));
        setupTextWatcher(textViewCommodityName, editTextDamages, new LossesViewModelCommands.SetDamagesCommand(), viewModel);
    }

    private void setUpExpiries(TextView textViewCommodityName, LossesCommodityViewModel viewModel, EditText editTextExpiries){
        editTextExpiries.setText(String.valueOf(viewModel.getExpiries()));
        setupTextWatcher(textViewCommodityName, editTextExpiries, new LossesViewModelCommands.SetExpiriesCommand(), viewModel);
    }

    private void setUpMissing(TextView textViewCommodityName, LossesCommodityViewModel viewModel, EditText editTextMissing){
        editTextMissing.setText(String.valueOf(viewModel.getMissing()));
        setupTextWatcher(textViewCommodityName, editTextMissing, new LossesViewModelCommands.SetMissingCommand(), viewModel);
    }

    private void setupTextWatcher(final TextView textViewCommodityName, final EditText editText, final LossesViewModelCommands.Command command, final LossesCommodityViewModel viewModel) {
        editText.addTextChangedListener(new LmisTextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                command.execute(viewModel, editable);
                int losses = viewModel.totalLosses();
                int stockOnHand = viewModel.getStockOnHand();
                if(losses > stockOnHand) {
                    textViewCommodityName.setError(String.format(getContext().getString(R.string.totalLossesGreaterThanStockAtHand), losses, stockOnHand));
                }else{
                    textViewCommodityName.setError(null);
                }
            }
        });
    }

    private void activateCancelButton(ImageButton imageButtonCancel, final LossesCommodityViewModel viewModel) {
        imageButtonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
{'fraction_non_alphanumeric': 0.07988357050452781, 'fraction_numerical': 0.0, 'mean_word_length': 4.708923076923077, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '423813', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2516, 'n_words': 422}