package au.com.pactera.pacterapulse.helper;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by liny1 on 14/05/2015.
 */
public class VoteManager
{

	protected static String VOTED_DATE = "VOTED_DATE";
	protected static String VOTED_VALUE = "VOTED_VALUE";
	protected static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
	private Context context;
	private SharedPreferences sharedPreferences;

	public VoteManager(Context context)
	{
		this.context = context;
	}

	/**
	 * submit the vote with the emotion value
	 *
	 * @param emotion the emotion value in integer
	 */
	public void saveVote(int emotion)
	{
		Date now = new Date();
		this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		sharedPreferences.edit().putString(VOTED_DATE, simpleDateFormat.format(now)).apply();
		sharedPreferences.edit().putInt(VOTED_VALUE, emotion).apply();
		Log.d("info", "saved");
	}

	/**
	 * check if the user has voted the emotion today
	 *
	 * @return isVoted the boolean value
	 */
	public boolean hasVotedToday()
	{
		this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		String votedDayString = sharedPreferences.getString(VOTED_DATE, "01/01/2000");
		try
		{
			Date today = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
			Date votedDay = simpleDateFormat.parse(votedDayString);
			if (votedDay.compareTo(today) == 0)
			{ // today
				return true;
			}
		}
		catch (ParseException e)
		{
			// add ...
		}
		return false;
	}
}

@@ -176,51 +176,49 @@
 oid.
-util.Log;%0A%0Aimport java.text.ParseException;
+text.TextUtils;%0Aimport android.util.Log;%0A
 %0Aimp
@@ -982,16 +982,45 @@
 ED_DATE,
+ Preference.getUID(context) +
  simpleD
@@ -1453,294 +1453,140 @@
 ;%0A%09%09
-try%0A%09%09%7B%0A%09%09%09Date today = simpleDateFormat.parse(simpleDateFormat.format(new Date()));%0A%09%09%09Date votedDay = simpleDateFormat.parse(votedDayString);%0A%09%09%09if (votedDay.compareTo(today) == 0)%0A%09%09%09%7B // today%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09catch (ParseException e)%0A%09%09%7B%0A%09%09%09// add ...%0A%09%09%7D%0A%09%09return false
+return !TextUtils.isEmpty(votedDayString) && votedDayString.equals(Preference.getUID(context) + simpleDateFormat.format(new Date()))
 ;%0A%09%7D

Fix the issue that new user can't vote after the first user voting and logout