package com.triestpa.cloudcamera.Utilities;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;
import android.view.WindowManager;
import android.widget.Toast;

import com.triestpa.cloudcamera.CloudCameraApplication;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SystemUtilities {
    final static String TAG = SystemUtilities.class.getName();

    public final static int MEDIA_TYPE_IMAGE = 1;
    public final static int MEDIA_TYPE_VIDEO = 2;

    public static boolean isOnline(Activity activity) {
        ConnectivityManager cm =
                (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnectedOrConnecting();
    }

    public static void lockOrientation(Activity activity) {
        int orientation = activity.getRequestedOrientation();
        int rotation = ((WindowManager) activity.getSystemService(
                Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
        switch (rotation) {
            case Surface.ROTATION_0:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
            case Surface.ROTATION_90:
                orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                break;
            case Surface.ROTATION_180:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                break;
            case Surface.ROTATION_270:
                orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                break;
            default:
                orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                break;
        }
        activity.setRequestedOrientation(orientation);
    }

    public static void unlockOrientation(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }

    /*
   * Create a File for saving an image or video
   */
    public static File getOutputMediaFile(int type) {
        if (isExternalStorageWritable()) {
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "CloudCamera");
            // This location works best if you want the created images to be shared
            // between applications and persist after your app has been uninstalled.

            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.e(TAG, "failed to create directory");
                    return null;
                }
            }

            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile;
            if (type == MEDIA_TYPE_IMAGE) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                        "IMG_" + timeStamp + ".jpg");
            } else if (type == MEDIA_TYPE_VIDEO) {
                mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                        "VID_" + timeStamp + ".mp4");
            } else {
                return null;
            }
            return mediaFile;
        }
        else {
            Log.e(TAG, "External Storage Not Writable");
            return null;
        }
    }

    /* Checks if external storage is available for read and write */
    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    public static boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }

    public static void showToastMessage(String message) {
        Toast.makeText(CloudCameraApplication.getAppContext(), message, Toast.LENGTH_SHORT).show();

    }

    public static void downloadFile(String url, String id, int mediatype) {
        Uri videoUri = Uri.parse(url);
        DownloadManager.Request r = new DownloadManager.Request(videoUri);

        // This put the download in the same Download dir the browser uses
        if (mediatype == MEDIA_TYPE_IMAGE) {
            r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, id + ".jpeg");
        }
        else {
            r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, id + ".mp4");
        }

        r.allowScanningByMediaScanner();

        // Notify user when download is completed
        r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        // Start download
        Context context = CloudCameraApplication.getAppContext();
        DownloadManager dm = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE);
        dm.enqueue(r);
    }
}

@@ -306,33 +306,8 @@
 nt;%0A
-import android.util.Log;%0A
 impo
@@ -2353,55 +2353,9 @@
 ) %7B%0A
-        if (isExternalStorageWritable()) %7B%0A    
+%0A
     
@@ -2372,607 +2372,74 @@
 edia
-Storag
+Cach
 eDir = 
-new File(Environment.getExternalStoragePublicDirectory(%0A                    Environment.DIRECTORY_PICTURES), %22CloudCamera%22);%0A            // This location works best if you want the created images to be shared%0A            // between applications and persist after your app has been uninstalled.%0A%0A            // Create the storage directory if it does not exist%0A            if (!mediaStorageDir.exists()) %7B%0A                if (!mediaStorageDir.mkdirs()) %7B%0A                    Log.e(TAG, %22failed to create directory%22);%0A                    return null;%0A                %7D%0A            %7D%0A%0A    
+CloudCameraApplication.getAppContext().getCacheDir();%0A%0A
     
@@ -2470,20 +2470,16 @@
 le name%0A
-    
         
@@ -2561,28 +2561,24 @@
 ));%0A        
-    
 File mediaFi
@@ -2577,28 +2577,24 @@
  mediaFile;%0A
-    
         if (
@@ -2625,36 +2625,32 @@
 ) %7B%0A            
-   
- 
 mediaFile = new 
@@ -2651,38 +2651,36 @@
 = new File(media
-Storag
+Cach
 eDir.getPath() +
@@ -2717,20 +2717,16 @@
         
-   
- 
 %22IMG_%22 +
@@ -2743,28 +2743,24 @@
  + %22.jpg%22);%0A
-    
         %7D el
@@ -2798,36 +2798,32 @@
 ) %7B%0A            
- 
-   
 mediaFile = new 
@@ -2832,22 +2832,20 @@
 le(media
-Storag
+Cach
 eDir.get
@@ -2890,20 +2890,16 @@
         
-    
 %22VID_%22 +
@@ -2924,28 +2924,24 @@
 %22);%0A        
-  
-  
 %7D else %7B%0A   
@@ -2929,36 +2929,32 @@
        %7D else %7B%0A
-    
             retu
@@ -2966,28 +2966,24 @@
 ll;%0A        
-    
 %7D%0A          
@@ -2976,28 +2976,24 @@
   %7D%0A        
- 
-   
 return media
@@ -3002,804 +3002,8 @@
 le;%0A
-        %7D%0A        else %7B%0A            Log.e(TAG, %22External Storage Not Writable%22);%0A            return null;%0A        %7D%0A    %7D%0A%0A    /* Checks if external storage is available for read and write */%0A    public static boolean isExternalStorageWritable() %7B%0A        String state = Environment.getExternalStorageState();%0A        if (Environment.MEDIA_MOUNTED.equals(state)) %7B%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A    /* Checks if external storage is available to at least read */%0A    public static boolean isExternalStorageReadable() %7B%0A        String state = Environment.getExternalStorageState();%0A        if (Environment.MEDIA_MOUNTED.equals(state) %7C%7C%0A                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) %7B%0A            return true;%0A        %7D%0A        return false;%0A
     
@@ -3587,24 +3587,16 @@
        %7D
-%0A       
  else %7B%0A

Store temporary media files in internal cache directory