package cz.mendelu.xmarik.train_manager.adapters;

import android.content.Context;
import android.content.ContextWrapper;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.os.Handler;

import java.util.ArrayList;

import cz.mendelu.xmarik.train_manager.R;
import cz.mendelu.xmarik.train_manager.models.TrainFunction;
import cz.mendelu.xmarik.train_manager.activities.TrainHandler;

/**
 * FunctionCheckBoxAdapter is a ListView` checkbox items with behavior connected to function
 * setting.
 */
public class FunctionCheckBoxAdapter extends ArrayAdapter<TrainFunction> {
    private LayoutInflater vi;
    private ArrayList<TrainFunction> trainList;
    private boolean m_enabled;

    public FunctionCheckBoxAdapter(Context context, int textViewResourceId,
                                   ArrayList<TrainFunction> trainList, boolean enabled) {
        super(context, textViewResourceId, trainList);
        this.trainList = new ArrayList<>();
        this.trainList.addAll(trainList);
        this.m_enabled = enabled;
        vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = vi.inflate(R.layout.lok_function, null);
            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.code);
            holder.name = (CheckBox) convertView.findViewById(R.id.chb_func);
            convertView.setTag(holder);

            holder.name.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    chb_onClick((CheckBox)v);
                }
            });

            convertView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox chb = ((CheckBox)v.findViewById(R.id.chb_func));
                    chb.toggle();
                    chb_onClick(chb);
                }
            });

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        TrainFunction function = trainList.get(position);

        holder.code.setText(function.name.equals("") ?
             "F" + String.valueOf(function.num) : "F" + String.valueOf(function.num) + ": " + function.name);

        holder.name.setText("");
        holder.name.setChecked(function.checked);
        holder.name.setTag(function);
        holder.name.setEnabled(m_enabled);
        convertView.setEnabled(m_enabled);

        return convertView;
    }

    void chb_onClick(CheckBox chb) {
        final CheckBox c = chb;
        final TrainFunction trainFunc = (TrainFunction)chb.getTag();
        ((TrainHandler) ((ContextWrapper) chb.getContext())).onFuncChanged(trainFunc.num, chb.isChecked());

        if (trainFunc.type == TrainFunction.TrainFunctionType.MOMENTARY && trainFunc.checked) {
            chb.setEnabled(false);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    c.setChecked(false);
                    ((TrainHandler) ((ContextWrapper)c.getContext())).onFuncChanged(trainFunc.num, c.isChecked());
                    c.setEnabled(true);
                }
            }, 750);
        }
    }

    private class ViewHolder {
        TextView code;
        CheckBox name;
        Button toggle;
    }

}
@@ -1715,20 +1715,24 @@
  holder.
-name
+chb_func
  = (Chec
@@ -1834,28 +1834,32 @@
      holder.
-name
+chb_func
 .setOnClickL
@@ -2214,24 +2214,71 @@
 chb_func));%0A
+                    if (chb.isEnabled()) %7B%0A    
             
@@ -2295,24 +2295,28 @@
 b.toggle();%0A
+    
             
@@ -2341,16 +2341,38 @@
 k(chb);%0A
+                    %7D%0A
         
@@ -2725,45 +2725,16 @@
 der.
-name.setText(%22%22);%0A        holder.name
+chb_func
 .set
@@ -2771,28 +2771,32 @@
      holder.
-name
+chb_func
 .setTag(func
@@ -2821,55 +2821,16 @@
 der.
-name.setEnabled(m_enabled);%0A        convertView
+chb_func
 .set
@@ -3747,35 +3747,16 @@
 Box 
-name;%0A        Button toggle
+chb_func
 ;%0A  

Rename variables, fix convertView onClick.