package de.apps4ics.mountainnavigation.adapters;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;

import de.apps4ics.mountainnavigation.MainActivity;
import de.apps4ics.mountainnavigation.R;

/**
 * Created by Vinz on 09.10.2015.
 */
public class CheckboxImageAdapter extends BaseAdapter {
    Activity context;
    boolean[] itemChecked;
    int nrOfItems;
    String[] entries;
    String[] entryDesc;
    Integer[] imgs;

    public CheckboxImageAdapter(Activity context, String[] entries, String[] entryDesc, Integer[] entryImgs){
        super();
        this.context = context;
        itemChecked = new boolean[entries.length];
        nrOfItems = 0;
        this.entries = entries;
        this.entryDesc = entryDesc;
        imgs = entryImgs;
    }

    private class ViewHolder {
        TableRow tr;
        ImageView iv;
        TextView tv_title;
        TextView tv_desc;
        CheckBox cb;
    }

    public int[] getCheckedItems(){
        int[] items = new int[nrOfItems];
        int added = 0;
        for(int i=0; i<itemChecked.length; ++i){
            if(itemChecked[i]){
                items[added++] = i;
            }
        }
        return items;
    }

    @Override
    public int getCount() {
        return entries.length;
    }

    @Override
    public Object getItem(int position) {
        return entries[position];
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        LayoutInflater inflater = context.getLayoutInflater();

        if(convertView == null){
            convertView = inflater.inflate(R.layout.checkbox_image_item, null);
            holder = new ViewHolder();
            holder.tr = (TableRow) convertView.findViewById(R.id.path_table_row);
            holder.iv = (ImageView) convertView.findViewById(R.id.path_img);
            holder.tv_title = (TextView) convertView.findViewById(R.id.path_text_title);
            holder.tv_desc = (TextView) convertView.findViewById(R.id.path_text_desc);
            holder.cb = (CheckBox) convertView.findViewById(R.id.path_checkbox);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String item_title = (String) getItem(position);
        String item_desc;
        if(entryDesc != null) item_desc = entryDesc[position];
        else item_desc = "";
        Drawable img = context.getResources().getDrawable(imgs[position]);
        holder.iv.setImageDrawable(img);
        holder.tv_title.setText(item_title);
        holder.tv_desc.setText(item_desc);
        holder.cb.setChecked(false);

        if(itemChecked[position]){
            holder.cb.setChecked(true);
            setImageFilter(holder, false);
        } else {
            holder.cb.setChecked(false);
            setImageFilter(holder, true);
        }
        holder.tr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.cb.performClick();
            }
        });
        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateCheckbox(holder, position);
            }
        });
        return convertView;
    }

    private void setImageFilter(ViewHolder holder, boolean set) {
        ImageView iv = holder.iv;
        if(!set && (iv.getTag() == null || iv.getTag() == "disabled")){
            iv.setColorFilter(null);
            iv.setTag("");
        } else {
            iv.setColorFilter(MainActivity.getRes().getColor(R.color.icon_unselected_gray));
            iv.setTag("disabled");
        }
    }

    private void updateCheckbox(ViewHolder holder, int position){
        if(holder.cb.isChecked()){
            setImageFilter(holder, false);
            itemChecked[position] = true;
            nrOfItems++;
        } else {
            setImageFilter(holder, true);
            itemChecked[position] = false;
            nrOfItems--;
        }
    }
}
