package in.techware.carrefour.util;

import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * Created by Jemsheer K D on 3/10/2015.
 * Package in.techware.gocourt.util
 * Project Comida
 */
public class ImageFilePath {
    /**
     * Method for return file path of Gallery image
     *
     * @param context
     * @param uri
     * @return path of the selected image file from gallery
     */
    public static String getPath(final Context context, final Uri uri) {
        //check here to KITKAT or new version
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        // DocumentProvider

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (uri.getScheme().equalsIgnoreCase("file")) {
                System.out.println("In File Section");
                return uri.getPath();
            } else if (DocumentsContract.isDocumentUri(context, uri)) {
                System.out.println("In Document Section");
                // ExternalStorageProvider
                if (isExternalStorageDocument(uri)) {
                    System.out.println("In External Storage Section");
                    final String docId = DocumentsContract.getDocumentId(uri);
                    final String[] split = docId.split(":");
                    final String type = split[0];

                    if ("primary".equalsIgnoreCase(type)) {
                        return Environment.getExternalStorageDirectory() + "/" + split[1];
                    }
                }
                // DownloadsProvider
                else if (isDownloadsDocument(uri)) {
                    System.out.println("In Downloads Document Section");
                    final String id = DocumentsContract.getDocumentId(uri);
                    final Uri contentUri = ContentUris.withAppendedId(
                            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                    return getDataColumn(context, contentUri, null, null);
                }
                // MediaProvider
                else if (isMediaDocument(uri)) {
                    System.out.println("In Media Document Section");
                    final String docId = DocumentsContract.getDocumentId(uri);
                    final String[] split = docId.split(":");
                    final String type = split[0];

                    Uri contentUri = null;
                    if ("image".equals(type)) {
                        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    } else if ("video".equals(type)) {
                        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                    } else if ("audio".equals(type)) {
                        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                    }

                    final String selection = "_id=?";
                    final String[] selectionArgs = new String[]{
                            split[1]
                    };

                    return getDataColumn(context, contentUri, selection, selectionArgs);
                } else if ("content".equalsIgnoreCase(uri.getScheme())) {

                    // Return the remote address
                    if (isGooglePhotosUri(uri))
                        return uri.getLastPathSegment();


                    String selection = null;
                    String[] selectionArgs = null;
                    String[] projection = null;
                    Cursor cursor = null;
                    try {
                        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
                        int column_index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                        if (cursor.moveToFirst()) {
                            String fileName = cursor.getString(column_index);
                            File file = new File(context.getCacheDir(), fileName);

                            int maxBufferSize = 1024 * 1024;

                            try {
                                InputStream inputStream = context.getContentResolver().openInputStream(uri);
                                Log.e("InputStream Size", "Size " + inputStream);
                                int bytesAvailable = inputStream.available();
//                    int bufferSize = 1024;
                                int bufferSize = Math.min(bytesAvailable, maxBufferSize);
                                final byte[] buffers = new byte[bufferSize];

                                FileOutputStream outputStream = new FileOutputStream(file);
                                int read = 0;
                                while ((read = inputStream.read(buffers)) != -1) {
                                    outputStream.write(buffers, 0, read);
                                }
                                Log.e("File Size", "Size " + file.length());
                                inputStream.close();
                                outputStream.close();

                                if (file.length() > 0) {
                                    file.getAbsolutePath();
                                    Log.e("File Path", "Path " + file.getPath());
                                    Log.e("File Size", "Size " + file.length());
                                    return file.getAbsolutePath();
                                } else {
                                    return null;
                                }


                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
//                            return cursor.getString(column_index);
                        }
                    } catch (Exception e) {
                    }

//                    return getDataColumn(context, uri, null, null);
                }
            } else if (uri.getScheme().equalsIgnoreCase("content")) {
                System.out.println("In Content Section");
                String imageFilePath = "";
                Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
                try {
                    assert cursor != null;
                    cursor.moveToFirst();
                    imageFilePath = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    assert cursor != null;
                    cursor.close();
                    return imageFilePath;
                }
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {

            // Return the remote address
            if (isGooglePhotosUri(uri))
                return uri.getLastPathSegment();

            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        } else if (uri.getPath().startsWith("file") || uri.getPath().startsWith("File")) {
            return uri.getPath();
        }
        return null;
    }

    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context       The context.
     * @param uri           The Uri to query.
     * @param selection     (Optional) Filter used in the query.
     * @param selectionArgs (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    private static String getDataColumn(Context context, Uri uri, String selection,
                                        String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int index = cursor.getColumnIndexOrThrow(column);
                String filePath = cursor.getString(index);
                return filePath;
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    private static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    private static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    private static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is Google Photos.
     */
    private static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }


    public static void deleteUri(Context context, Uri uri) {
        try {
            context.getContentResolver().delete(uri, null, null);
        } catch (Exception e) {
        }
    }

    public static void sendBroadcast(Context context, String image_url) {
        if (image_url != null) {
            MediaScannerConnection.scanFile(context,
                    new String[]{image_url}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });
        }
        try {
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        } catch (Exception e) {
        }

        try {
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        } catch (Exception e) {
        }
    }
}

