package de.tudarmstadt.informatik.hostage.ui2.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
import de.tudarmstadt.informatik.hostage.model.Profile;
import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
import de.tudarmstadt.informatik.hostage.ui2.activity.ProfileEditActivity;
import de.tudarmstadt.informatik.hostage.ui2.swipelist.SwipeListView;

/**
 * @author Alexander Brakowski
 * @created 14.01.14 18:00
 */
public class ProfileManagerListAdapter extends ArrayAdapter<Profile> {
	private class ViewHolder {
		public TextView labelView;
		public TextView textView;
		public ImageView imageSelected;
		public ImageView itemIcon;
		public ImageButton buttonEdit;
		public ImageButton buttonDelete;
		public View seperator;
	}

    private final Context context;
    private final List<Profile> values;

    public ProfileManagerListAdapter(Context context, List<Profile> objects) {
        super(context, R.layout.profile_manager_list_item, objects);
        this.context = context;
        this.values  = objects;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
	    LayoutInflater inflater = (LayoutInflater) context
			    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = convertView;
	    ViewHolder holder = null;

	    final Profile item = values.get(position);

	    if(rowView == null){
		    rowView = inflater.inflate(R.layout.profile_manager_list_item, parent, false);

		    holder = new ViewHolder();
		    holder.labelView = (TextView) rowView.findViewById(R.id.profile_manager_item_label);
		    holder.textView = (TextView) rowView.findViewById(R.id.profile_manager_item_text);
		    holder.imageSelected = (ImageView) rowView.findViewById(R.id.profile_manager_item_activated);
		    holder.itemIcon = (ImageView) rowView.findViewById(R.id.profile_manager_item_image);
		    holder.buttonEdit = (ImageButton) rowView.findViewById(R.id.profile_manager_item_button_edit);
		    holder.buttonDelete = (ImageButton) rowView.findViewById(R.id.profile_manager_item_button_delete);
			holder.seperator = rowView.findViewById(R.id.profile_manager_item_seperator);

		    rowView.setTag(holder);
	    } else {
		    holder = (ViewHolder) rowView.getTag();
	    }

	    ((SwipeListView)parent).recycle(rowView, position);

	    holder.textView.setText(item.mText);
	    holder.labelView.setText(item.mLabel);

	    if(item.getIconBitmap() != null){
	        //Bitmap bitmap = Bitmap.createScaledBitmap(item.getIconBitmap(), 32, 32, true);
	        holder.itemIcon.setImageBitmap(item.getIconBitmap());
	    } else {
		    holder.itemIcon.setImageBitmap(BitmapFactory.decodeResource(MainActivity.context.getResources(), R.drawable.ic_launcher));
	    }

		holder.buttonEdit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, ProfileEditActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("profile_id", item.mId);
				context.startActivity(intent);
			}
		});

		holder.buttonDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				new AlertDialog.Builder(context)
						.setTitle(R.string.delete_profile)
						.setMessage(R.string.really_want_delete_profiel)
						.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {

							}
						})
						.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								ProfileManager profileManager = ProfileManager.getInstance();

								profileManager.deleteProfile(item);
								profileManager.getProfileListAdapter().notifyDataSetChanged();

							}
						})
						.setIcon(android.R.drawable.ic_dialog_alert)
						.show();
			}
		});

		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.textView.getLayoutParams();

        if(!item.mActivated){
            lp.setMargins(0, 0, 0, 0);

	        holder.textView.setLayoutParams(lp);

	        holder.imageSelected.setVisibility(View.GONE);
        } else {
			/*float d = context.getResources().getDisplayMetrics().density;
			int dm = (int)(20 * d);

			lp.setMargins(0,0,dm,0);

			holder.textView.setLayoutParams(lp);*/

			holder.imageSelected.setVisibility(View.VISIBLE);
		}


		if(!item.isEditable()){
			holder.buttonDelete.setVisibility(View.GONE);
			holder.seperator.setVisibility(View.GONE);
		} else {
			holder.buttonDelete.setVisibility(View.VISIBLE);
			holder.seperator.setVisibility(View.VISIBLE);
		}

        return rowView;
    }


}
