/*
 * Copyright 2013 The GDG Frisbee Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gdg.frisbee.android.achievements;

import android.content.SharedPreferences;
import android.os.Handler;

import com.google.android.gms.games.GamesClient;

import org.gdg.frisbee.android.Const;
import org.gdg.frisbee.android.utils.PlayServicesHelper;

/**
 *
 * Simple Achievements action handler used not to clutter activity code with achievements unlocking.
 *
 * @author Bartek Przybylski <bart.p.pl@gmail.com>
 */
public class AchievementActionHandler {
    private Handler mHandler;
    private PlayServicesHelper mPlayHelper;
    private SharedPreferences mPreferences;

    private static final int ONE_SEC_IN_MILLISECONDS = 1000;

    public AchievementActionHandler(Handler handler,
                                    PlayServicesHelper playHelper,
                                    SharedPreferences preferences) {
        mHandler = handler;
        mPlayHelper = playHelper;
        mPreferences = preferences;
    }

    public void handleSignIn() {
        postAchievementUnlockedEvent(Achievements.ACHIEVEMENT_SIGNIN);
    }

    public void handleAppStarted() {
        int appStartedCounter = mPreferences.getInt(Const.SETTINGS_APP_STARTS, 0);

        if (appStartedCounter >= 10)
            postAchievementUnlockedEvent(Achievements.ACHIEVEMENT_RETURN);

        if (appStartedCounter >= 50)
            postAchievementUnlockedEvent(Achievements.ACHIEVEMENT_KING_OF_THE_HILL);
    }

    public void handleVideoViewed() {
        SharedPreferences.Editor editor = mPreferences.edit();
        int videoPlayed = mPreferences.getInt(Const.SETTINGS_VIDEOS_PLAYED, 0);
        videoPlayed++;

        editor.putInt(Const.SETTINGS_VIDEOS_PLAYED, videoPlayed);
        editor.apply();

        if(videoPlayed >= 10)
            postAchievementUnlockedEvent(Achievements.ACHIEVEMENT_CINEPHILE);
    }

    public void handleGDLVideoViewed() {
        SharedPreferences.Editor editor = mPreferences.edit();
        int videoPlayed = mPreferences.getInt(Const.SETTINGS_GDL_VIDEOS_PLAYED, 0);
        videoPlayed++;

        editor.putInt(Const.SETTINGS_GDL_VIDEOS_PLAYED, videoPlayed);
        editor.apply();

        if(videoPlayed >= 5)
            postAchievementUnlockedEvent(Achievements.ACHIEVEMENT_GDL_ADDICT);
    }

    private void postAchievementUnlockedEvent(final String achievementName) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPlayHelper.getGamesClient(new PlayServicesHelper.OnGotGamesClientListener() {
                    @Override
                    public void onGotGamesClient(GamesClient c) {
                        c.unlockAchievement(achievementName);
                    }
                });
            }
        }, ONE_SEC_IN_MILLISECONDS);
    }
}

@@ -2066,63 +2066,83 @@
     
-SharedPreferences.Editor editor = mPreferences.edit(
+int videoPlayed = updateVideoViewedCounter(Const.SETTINGS_VIDEOS_PLAYED
 );%0A
+%0A
     
@@ -2138,35 +2138,34 @@
 YED);%0A%0A        i
-nt 
+f(
 videoPlayed = mP
@@ -2164,109 +2164,191 @@
 yed 
+%3E
 = 
-mPreferences.getInt(Const.SETTINGS_VIDEOS_PLAYED, 0
+10)%0A            postAchievementUnlockedEvent(Achievements.ACHIEVEMENT_CINEPHILE
 );%0A    
+%7D%0A%0A
     
-videoPlayed++;%0A%0A        editor.putInt
+public void handleGDLVideoViewed() %7B%0A        int videoPlayed = updateVideoViewedCounter
 (Con
@@ -2351,32 +2351,36 @@
 (Const.SETTINGS_
+GDL_
 VIDEOS_PLAYED, v
@@ -2380,45 +2380,8 @@
 AYED
-, videoPlayed);%0A        editor.apply(
 );%0A%0A
@@ -2398,34 +2398,33 @@
 (videoPlayed %3E= 
-10
+5
 )%0A            po
@@ -2471,33 +2471,34 @@
 ACHIEVEMENT_
-CINEPHILE
+GDL_ADDICT
 );%0A    %7D%0A%0A  
@@ -2504,40 +2504,68 @@
    p
-ublic void handleGDLVideoViewed(
+rivate int updateVideoViewedCounter(String videoCategoryName
 ) %7B%0A
@@ -2677,40 +2677,25 @@
 Int(
-Const.SETTINGS_GDL_VIDEOS_PLAYED
+videoCategoryName
 , 0)
@@ -2746,40 +2746,25 @@
 Int(
-Const.SETTINGS_GDL_VIDEOS_PLAYED
+videoCategoryName
 , vi
@@ -2799,28 +2799,31 @@
 pply();%0A
-%0A
+ 
        
- if(
+return 
 videoPla
@@ -2829,92 +2829,8 @@
 ayed
- %3E= 5)%0A            postAchievementUnlockedEvent(Achievements.ACHIEVEMENT_GDL_ADDICT)
 ;%0A  

Move video counter update to different function