package com.example.martin.projetv5;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by martin on 27/04/2017.
 */

public class PriseAdapterMenu extends BaseAdapter {
    public List<Prise2> mListP;
    public Context mContext;
    public LayoutInflater mInflater;
    public StringBDD eventBDD;

    public PriseAdapterMenu(Context context, List<Prise2> aListP){
        mContext = context;
        mListP = aListP;
        mInflater = LayoutInflater.from(mContext);
    }

    public int getCount(){
        return mListP.size();
    }

    public Object getItem(int position){
        return mListP.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        RelativeLayout layoutItem;

        if (convertView == null) {
            layoutItem = (RelativeLayout) mInflater.inflate(R.layout.prise_menu, parent, false);
        } else {
            layoutItem = (RelativeLayout) convertView;
        }

        ImageView logoMedoc = (ImageView) layoutItem.findViewById(R.id.logoMenu);
        TextView horloge = (TextView) layoutItem.findViewById(R.id.horlogeMenu);
        TextView nom = (TextView) layoutItem.findViewById(R.id.nomMenu);
        ImageButton change = (ImageButton) layoutItem.findViewById(R.id.boutonConfirmMenu);

        eventBDD = new StringBDD(null);
        eventBDD.open();
        Medicament m = eventBDD.getMedocWithId(mListP.get(position).getIdMedoc());
        nom.setText(m.getNom1());
        File imgFile = new  File(m.getLogo());
        if(imgFile.exists()){
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            logoMedoc.setImageBitmap(myBitmap);
        }


        horloge.setText(mListP.get(position).getHeure());
        change.setImageResource(R.drawable.edit);

        change.setTag(position);
        change.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Integer position = (Integer)v.getTag();
                sendListener(mListP.get(position),position);
            }
        });

        return layoutItem;
    }

    public interface PriseAdapterListener{
        void onClickButton(Prise2 item, int position);
    }

    private ArrayList<PriseAdapterListener> mListListener = new ArrayList<PriseAdapterListener>();

    public void addListener(PriseAdapterListener aListener){
        mListListener.add(aListener);
    }

    private void sendListener(Prise2 item, int position){
        for(int i = mListListener.size()-1; i>=0; i--){
            mListListener.get(i).onClickButton(item, position);
        }
    }
}
