package com.wayne.www.waynelib.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import com.wayne.www.waynelib.fdc.model.Pump;
import com.wayne.www.waynelib.listener.OnDropdownControlMenuClickListener;
import com.wayne.www.waynelib.ui.view.PumpView;
import com.wayne.www.waynelib.common.Tuple;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Think on 4/29/2016.
 */
public class PumpsGridViewAdapter extends BaseAdapter {
    private Logger fileLogger = LoggerFactory.getLogger(PumpView.class);
    private static String LOG_TAG = "PumpsGridViewAdapter";
    private Context context;
    private List<Pump> pumps;
    private int numOfPumps = 0;
    private OnDropdownControlMenuClickListener onPumpViewDropdownMenuClickedListeners;
    private ReentrantLock lock = new ReentrantLock();

    private PumpsGridViewAdapter() {
    }

    public PumpsGridViewAdapter(Context context, List<Pump> pumps, OnDropdownControlMenuClickListener listener) {
        this.context = context;
        this.pumps = pumps;
        this.onPumpViewDropdownMenuClickedListeners = listener;
    }

    public PumpsGridViewAdapter(Context context, List<Pump> pumps, int numOfPumps, OnDropdownControlMenuClickListener listener) {
        this.context = context;
        this.pumps = pumps;
        this.onPumpViewDropdownMenuClickedListeners = listener;
        this.numOfPumps = numOfPumps;
    }

//    public void addOnDropdownMenuClickedListener(OnDropdownControlMenuClickListener listener) {
//        this.onPumpViewDropdownMenuClickedListeners.add(listener);
//    }

    @Override
    public int getCount() {
        if (this.pumps == null)
            return 0;

        return this.pumps.size();
    }

    @Override
    public Object getItem(int position) {
        return this.pumps.get(position);
    }

    @Override
    public long getItemId(int position) {
        return this.pumps.get(position).PumpNo;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //if (convertView == null) {
        List<Tuple<String, Integer>> menus = new ArrayList<>();
//            menus.add(new Tuple<String, Integer>("auth", 1));
//            menus.add(new Tuple<String, Integer>("de-auth", 2));
        //Log.d(LOG_TAG, "####(==null)Binding pump with No: " + this.pumps.get(position).PumpNo + " to a PumpView at gridView position: " + position);
        fileLogger.debug("Null Binding pump model with PumpNo: " + this.pumps.get(position).PumpNo + " to a PumpView at gridView position: " + position);
        PumpView pumpView;
        if (numOfPumps == 0) {
             pumpView= new PumpView(this.context, menus);
        } else {
             pumpView = new PumpView(this.context, menus, numOfPumps);
        }
        pumpView.bindPump(this.pumps.get(position));

        pumpView.addOnDropdownMenuClickedListener(this.onPumpViewDropdownMenuClickedListeners);

//        ViewGroup.LayoutParams lp = pumpView.getLayoutParams();
//        lp.width = 120;
//        lp.height = 200;
//        pumpView.requestLayout();

//        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        pumpView.setLayoutParams(lp);
        return pumpView;
//        } else {
//            fileLogger.debug("Binding pump model with PumpNo: " + this.pumps.get(position).PumpNo + " to a PumpView at gridView position: " + position);
//            ((PumpView) convertView).bindPump(this.pumps.get(position));
//            return convertView;
//        }
    }
}
