package org.literacyapp.authentication.helper;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Environment;
import android.widget.ImageView;

import org.literacyapp.R;
import org.literacyapp.contentprovider.model.content.multimedia.Audio;
import org.literacyapp.contentprovider.model.content.multimedia.Image;
import org.literacyapp.contentprovider.model.content.multimedia.Video;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Helper class for fetching image overlay displayed during authentication.
 */
public class AuthenticationInstructionHelper {

    private static final String AUTHENTICATION_INSTRUCTION_BOY = "authentication_instruction_640";
    private static final String AUTHENTICATION_INSTRUCTION_GIRL = "authentication_instruction_girl_640";
    public static final String RESOURCES_DRAWABLE_FOLDER = "drawable";
    public static final String RESOURCES_RAW_FOLDER = "raw";

    public static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }

    public static void setAuthenticationInstructionAnimation(Context context, ImageView animationImageView){
        int randomIndex = (int) (Math.random() * 2);
        switch (randomIndex){
            case 0:
                animationImageView.setImageResource(context.getResources().getIdentifier(AUTHENTICATION_INSTRUCTION_BOY, RESOURCES_DRAWABLE_FOLDER, context.getPackageName()));
                break;
            case 1:
                animationImageView.setImageResource(context.getResources().getIdentifier(AUTHENTICATION_INSTRUCTION_GIRL, RESOURCES_DRAWABLE_FOLDER, context.getPackageName()));
                break;
            default:
                animationImageView.setImageResource(context.getResources().getIdentifier(AUTHENTICATION_INSTRUCTION_BOY, RESOURCES_DRAWABLE_FOLDER, context.getPackageName()));
                break;
        }
    }

    public static MediaPlayer getMediaPlayerTabletPlacement(Context context){
        return MediaPlayer.create(context, R.raw.auth_tablet_placement);
    }

    public static MediaPlayer getMediaPlayerTabletPlacementOverlay(Context context){
        return MediaPlayer.create(context, R.raw.auth_tablet_placement_overlay);
    }

    public static synchronized void playTabletPlacementOverlay(MediaPlayer mediaPlayerTabletPlacement, MediaPlayer mediaPlayerTabletPlacementOverlay, MediaPlayer mediaPlayerAnimalSound){
        if (mediaPlayerTabletPlacement != null){
            if ((!mediaPlayerTabletPlacement.isPlaying()) && (!mediaPlayerTabletPlacementOverlay.isPlaying()) && (!mediaPlayerAnimalSound.isPlaying())){
                mediaPlayerTabletPlacementOverlay.start();
            }
        } else {
            if ((!mediaPlayerTabletPlacementOverlay.isPlaying()) && (!mediaPlayerAnimalSound.isPlaying())){
                mediaPlayerTabletPlacementOverlay.start();
            }
        }
    }
}

@@ -2634,16 +2634,17 @@
      if 
+(
 (mediaPl
@@ -2662,16 +2662,23 @@
 lacement
+Overlay
  != null
@@ -2678,18 +2678,112 @@
 != null)
-%7B%0A
+ && (mediaPlayerAnimalSound != null))%7B%0A            if (mediaPlayerTabletPlacement != null)%7B%0A    
         
@@ -2935,32 +2935,36 @@
                 
+   
+ 
 mediaPlayerTable
@@ -2994,34 +2994,42 @@
 ();%0A            
-%7D%0A
+    %7D%0A    
         %7D else %7B
@@ -3033,32 +3033,36 @@
 e %7B%0A            
+    
 if ((!mediaPlaye
@@ -3149,32 +3149,36 @@
                 
+    
 mediaPlayerTable
@@ -3196,32 +3196,50 @@
 verlay.start();%0A
+                %7D%0A
             %7D%0A  

Fix MediaPlayer crash