package org.otfusion.caturday.util;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.ImageView;

import org.otfusion.caturday.application.VoteCatsApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ApplicationUtils {

    private ApplicationUtils() {
    }

    public static VoteCatsApplication getApplication(Activity activity) {
        return (VoteCatsApplication) activity.getApplication();
    }

    @SuppressWarnings("unchecked")
    public static <T> T castActivityToInterface(Activity activity, Class<T> interfaceType) {
        try {
            return (T) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement " + interfaceType.getClass().getName() + " interface");
        }
    }

    public static Intent getShareImageIntent(Uri fileUri) {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
        shareIntent.setType("image/jpeg");
        return shareIntent;
    }

    public static Uri getLocalBitmapUri(ImageView imageView) {
        // Extract Bitmap from ImageView drawable
        Drawable drawable = imageView.getDrawable();
        Bitmap bmp;
        if (drawable instanceof BitmapDrawable){
            bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        } else {
            return null;
        }
        // Store image to default external storage directory
        Uri bmpUri = null;
        try {
            File file =  new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
            file.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.close();
            bmpUri = Uri.fromFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }


    public static String generateRandomCatName(String suffix) {
        List<String> differentNames = Arrays.asList(
                "aegean",
                "bambino",
                "birman",
                "cheetoh",
                "chausie",
                "cyprus",
                "dwelf",
                "egyptian_mau",
                "javanese",
                "korat",
                "minskin",
                "munchkin",
                "persian",
                "siamese",
                "sphynx",
                "toyger",
                "toybob"
        );
        Random random = new Random(System.currentTimeMillis());
        int sizeOfList = differentNames.size();
        int randomInt = random.nextInt((sizeOfList - 1));
        String prefix = differentNames.get(randomInt) + "_";
        return prefix + suffix;
    }

}

@@ -1461,1003 +1461,8 @@
  %7D%0A%0A
-    public static Uri getLocalBitmapUri(ImageView imageView) %7B%0A        // Extract Bitmap from ImageView drawable%0A        Drawable drawable = imageView.getDrawable();%0A        Bitmap bmp;%0A        if (drawable instanceof BitmapDrawable)%7B%0A            bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();%0A        %7D else %7B%0A            return null;%0A        %7D%0A        // Store image to default external storage directory%0A        Uri bmpUri = null;%0A        try %7B%0A            File file =  new File(Environment.getExternalStoragePublicDirectory(%0A                    Environment.DIRECTORY_DOWNLOADS), %22share_image_%22 + System.currentTimeMillis() + %22.png%22);%0A            file.getParentFile().mkdirs();%0A            FileOutputStream out = new FileOutputStream(file);%0A            bmp.compress(Bitmap.CompressFormat.PNG, 90, out);%0A            out.close();%0A            bmpUri = Uri.fromFile(file);%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return bmpUri;%0A    %7D%0A%0A%0A
     

remove unused code