package barchallenges.ima.ulaval.ca.barchallenge;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;

/**
 * Not a very good idea but correct for a prototype
 */
public class PrizeManager {
    private static PrizeManager Instance;

    public static PrizeManager getInstance()
    {
        if(Instance == null) {
            Instance = new PrizeManager();
        }
        return Instance;
    }

    private ArrayList<Prize> mPrizeList;

    private PrizeManager()
    {
        mPrizeList = new ArrayList<>();
        generateMockData();
    }

    public void updateData(Context pContext)
    {
        SharedPreferences pref = pContext.getSharedPreferences("IMA_PREF", pContext.MODE_PRIVATE);

        for (int i = 0; i< mPrizeList.size();i++) {
            Prize prize = mPrizeList.get(i);
            prize.setEarned(pref.getBoolean("Prize" + prize.getId() + "Earned", prize.getIsEarned()));
            prize.setIsUsed(pref.getBoolean("Prize" + prize.getId() + "Used", prize.getIsUsed()));
        }

    }

    private void generateMockData()
    {
        mPrizeList.add(new Prize(1, "Free Beer", "You can drink a beer with you friend!"));
        mPrizeList.add(new Prize(2, "10% on a drink", "Choose waht you want to get drunk"));
        mPrizeList.add(new Prize(3, "Free Shooter", "1.. 2.. 3.. SHOT!"));
        mPrizeList.add(new Prize(4, "25% on a poutine", "If you are hungry..."));
        mPrizeList.add(new Prize(5, "Free fries", "Because everyone love fries"));
    }

    public void earnNextPrize(Context pContext)
    {
        for (int i = 0; i< mPrizeList.size();i++){
            if(!mPrizeList.get(i).getIsEarned())
            {
                mPrizeList.get(i).earn();
                break;
            }
        }
        savePrizes(pContext);
    }

    public Prize getPrizeAt(int pPosition)
    {
        if(pPosition < mPrizeList.size())
        {
            return mPrizeList.get(pPosition);
        }
        else
        {
            return null;
        }
    }

    public int getPrizeCount()
    {
        return mPrizeList.size();
    }

    public void usePrize(Prize pPrize, Context pContext)
    {
        for (int i = 0; i< mPrizeList.size();i++) {
            if(mPrizeList.get(i).getName().equals(pPrize.getName())) {
                mPrizeList.get(i).useIt();
                break;
            }
        }

        savePrizes(pContext);
    }

    public void savePrizes(Context pContext)
    {
        SharedPreferences.Editor editor = pContext.getSharedPreferences("IMA_PREF", pContext.MODE_PRIVATE).edit();

        for (int i = 0; i< mPrizeList.size();i++) {
            Prize prize = mPrizeList.get(i);
            editor.putBoolean("Prize" + prize.getId() + "Earned", prize.getIsEarned());
            editor.putBoolean("Prize" + prize.getId() + "Used", prize.getIsUsed());
        }

        editor.apply();
    }

    public void reinitializePrizes(Context pContext)
    {
        SharedPreferences.Editor editor = pContext.getSharedPreferences("IMA_PREF", pContext.MODE_PRIVATE).edit();

        for (int i = 0; i< mPrizeList.size();i++) {
            Prize prize = mPrizeList.get(i);
            prize.setEarned(false);
            prize.setIsUsed(false);
            editor.putBoolean("Prize" + prize.getId() + "Earned", false);
            editor.putBoolean("Prize" + prize.getId() + "Used", false);
        }

        editor.apply();
    }
}

@@ -1147,151 +1147,154 @@
 1, %22
-Free Beer%22, %22You can drink a beer with you friend!%22));%0A        mPrizeList.add(new Prize(2, %2210%25 on a drink%22, %22Choose waht you want to get drunk
+Bi%C3%A8re gratuite%22, %22Tu peux boire une bonne bi%C3%A8re avec tes amis!%22));%0A        mPrizeList.add(new Prize(2, %2210%25 sur un verre%22, %22Choisi ce que tu veux!
 %22));
@@ -1335,20 +1335,23 @@
 3, %22
-Free 
 Shooter
+ gratuit
 %22, %22
@@ -1417,12 +1417,15 @@
 25%25 
-on a
+sur une
  pou
@@ -1436,25 +1436,21 @@
 %22, %22
-If you are hungry
+Si tu as faim
 ...%22
@@ -1496,47 +1496,51 @@
  %22Fr
-ee fries%22, %22Because everyone love fries
+ites gratuites%22, %22Car nous avons tous faim.
 %22));

Translate prize