package org.sfx.catholicjourney.rosary.model;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import org.sfx.catholicjourney.core.BundleKeys;

/**
 * Created by simonadmin on 2015-03-18.
 */
public class RosaryMysteryItem implements Parcelable {
	private static final String parc_rosary_mystery_enum = "rosaryMysteryEnum";
	private static final String parc_rosary_mystery_image_path_res_id = "rosaryMysteryImagePageResId";
	private static final String parc_rosary_mystery_image_path_string = "rosaryMysteryImagePageString";

	private RosaryMystery mRosaryMystery;
	private int mImagePathResId = -1;
	private String mImagePathString;

	public RosaryMysteryItem(RosaryMystery rosaryMystery, int imagePathResId) {
		this.mRosaryMystery = rosaryMystery;
		this.mImagePathResId = imagePathResId;
	}

	public RosaryMysteryItem(RosaryMystery rosaryMystery, String imagePathString) {
		this.mRosaryMystery = rosaryMystery;
		this.mImagePathString = imagePathString;
	}

	public RosaryMystery getRosaryMystery() {
		return mRosaryMystery;
	}

	public int getImagePathResId() {
		return mImagePathResId;
	}

	public String getImagePathString() {
		return mImagePathString;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		Bundle bundle = new Bundle();

		bundle.putInt(parc_rosary_mystery_enum, mRosaryMystery.ordinal());
		if (mImagePathResId != -1) {
			bundle.putInt(parc_rosary_mystery_image_path_res_id, mImagePathResId);
		}
		if (!TextUtils.isEmpty(mImagePathString)) {
			bundle.putString(parc_rosary_mystery_image_path_string, mImagePathString);
		}

		dest.writeBundle(bundle);
	}

	public static final Parcelable.Creator<RosaryMysteryItem> CREATOR = new Creator<RosaryMysteryItem>() {
		@Override
		public RosaryMysteryItem createFromParcel(Parcel source) {
			Bundle bundle = source.readBundle();

			// enum
			int rosaryMysteryEnumOrdinal = bundle.getInt(parc_rosary_mystery_enum);
			RosaryMystery rosaryMystery = RosaryMystery.values()[rosaryMysteryEnumOrdinal];

			RosaryMysteryItem item = null;
			// with image res id
			if (bundle.containsKey(parc_rosary_mystery_image_path_res_id)) {
				int imageResId = bundle.getInt(parc_rosary_mystery_image_path_res_id);
				item = new RosaryMysteryItem(rosaryMystery, imageResId);
			} else {
				// with image path
				String imagePath = bundle.getString(parc_rosary_mystery_image_path_string);
				item = new RosaryMysteryItem(rosaryMystery, imagePath);
			}

			return item;
		}

		@Override
		public RosaryMysteryItem[] newArray(int size) {
			return new RosaryMysteryItem[size];
		}
	};
}
