package de.tudarmstadt.informatik.hostage.model;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;

/**
 * @author Alexander Brakowski
 * @created 14.01.14 18:04
 */
public class Profile {
	public String mText;
	public String mLabel;
	public int mId;
	public boolean mActivated;
	public Bitmap mIcon;
	public int mIconId;
	public String mIconName;

	public String mIconPath;

	public boolean mIsBackVisible = false;
	public boolean mEditable = false;

	public Profile(){
		this.mEditable = true;
		this.mActivated = false;
		this.mId = -1;
	}

	public Profile(int id, String label, String text, Bitmap icon, boolean editable){
		this.mId = id;
		this.mLabel = text;
		this.mText = label;
		this.mActivated = false;
		this.mIcon = icon;
		this.mEditable = editable;
	}

	public Profile(int id, String label, String text, int icon, boolean editable){
		this(id, text, label, BitmapFactory.decodeResource(MainActivity.context.getResources(), icon), editable);
		mIconId = icon;
		mIconName = MainActivity.context.getResources().getResourceName(icon);
	}

	public Profile(int id, String label, String text, String iconPath, boolean editable){
		this.mId = id;
		this.mLabel = label;
		this.mText = text;
		this.mActivated = false;
		this.mIconPath = iconPath;
		this.mEditable = editable;
	}

	public void setIcon(Bitmap bitmap){
		this.mIcon = bitmap;
	}

	public void setIcon(int icon){
		this.mIcon = BitmapFactory.decodeResource(MainActivity.context.getResources(), icon);
	}

	public Bitmap getIconBitmap(){
		if(this.mIcon != null) return mIcon;

		if(this.mIconId != 0){
			this.mIcon = BitmapFactory.decodeResource(MainActivity.context.getResources(), mIconId);
			return this.mIcon;
		}

		if(!this.mIconName.isEmpty()){
			this.mIconId = MainActivity.context.getResources().getIdentifier(this.mIconName,
					"drawable", "de.tudarmstadt.informatik.hostage");
			this.mIcon = BitmapFactory.decodeResource(MainActivity.context.getResources(), this.mIconId);

			return this.mIcon;
		}

		if(this.mIconPath != null){
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inPreferredConfig = Bitmap.Config.ARGB_8888;
			Bitmap bitmap = BitmapFactory.decodeFile(this.mIconPath, options);

			return bitmap;
		}

		return null;
	}

	public Drawable getIconDrawable(){
		return new BitmapDrawable(getIconBitmap());
	}

	public boolean isEditable(){
		return this.mEditable;
	}

	public Profile cloneProfile(){
		return new Profile(mId, mLabel, mText, mIcon, mEditable);
	}
}