package epson.print.Util;

import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.database.StaleDataException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.provider.MediaStore;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;

import epson.common.Utils;
import epson.image.epsonImage;
import epson.print.CommonDefine;
import epson.print.EPImageUtil;

public class Photo implements Serializable, CommonDefine {
    private static final int DATE_FORMAT_DD_MM_YYYY = 2;
    private static final int DATE_FORMAT_MM_DD_YYYY = 1;
    private static final int DATE_FORMAT_OFF = 0;
    private static final int DATE_FORMAT_YYYY_MM_DD = 3;
    private static final int UNCONSTRAINED = -1;
    private Bitmap Micro_thumbnail;
    private Bitmap Mimi_thumbnail;
    private boolean checked;
    private String name;
    private String path;

    public String getPath() {
        return path;
    }

    public void setPath(String str) {
        path = str;
    }

    public String getName() {
        return name;
    }

    public void setName(String str) {
        name = str;
    }

    public Bitmap getMimi_thumbnail() {
        return mimi_thumbnail;
    }

    public void setMimi_thumbnail(Bitmap bitmap) {
        mimi_thumbnail = bitmap;
    }

    public Bitmap getMicro_thumbnail() {
        return micro_thumbnail;
    }

    public void setMicro_thumbnail(Bitmap bitmap) {
        micro_thumbnail = bitmap;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean z) {
        checked = z;
    }

    public static boolean IsFilePhoto(String str) {
        if (str == null || !Utils.isPhotoFile(str)) {
            return false;
        }
        if (!Utils.checkMimeType(str, CommonDefine.IMAGE_TYPE_BMP) || new EPImageUtil().getBitCount(str) == 24) {
            return true;
        }
        return false;
    }

    public static ArrayList<Long> getIdImage(String str, Context context, int i) throws NullPointerException, CursorIndexOutOfBoundsException, StaleDataException {
        ArrayList<Long> arrayList = new ArrayList<>();
        File file = new File(str);
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            Arrays.sort(listFiles, new ModifiedTimeComparator());
            for (File file2 : listFiles) {
                if (!file2.isDirectory() && IsFilePhoto(file2.getPath())) {
                    arrayList.add(Long.valueOf(loadIdOriginal(file2.getAbsolutePath(), context)));
                    if (arrayList.size() >= i) {
                        break;
                    }
                }
            }
        }
        return arrayList;
    }

    static class ModifiedTimeComparator implements Comparator<File> {
        ModifiedTimeComparator() {
        }

        public int compare(File file, File file2) {
            int i = ((file2.lastModified() - file.lastModified()) > 0 ? 1 : ((file2.lastModified() - file.lastModified()) == 0 ? 0 : -1));
            if (i == 0) {
                return 0;
            }
            return i > 0 ? 1 : -1;
        }
    }

    public static long loadIdOriginal(String str, Context context) throws NullPointerException, CursorIndexOutOfBoundsException, StaleDataException {
        long j;
        Cursor query = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, (String[]) null, (String) null, (String[]) null, (String) null);
        query.moveToFirst();
        while (true) {
            if (!query.getString(query.getColumnIndexOrThrow("_data")).equalsIgnoreCase(str)) {
                if (!query.moveToNext()) {
                    j = 0;
                    break;
                }
            } else {
                j = query.getLong(query.getColumnIndexOrThrow("_id"));
                break;
            }
        }
        query.close();
        return j;
    }

    public static ArrayList<String> getStringImage(String str, int i) throws NullPointerException, CursorIndexOutOfBoundsException, StaleDataException {
        ArrayList<String> arrayList = new ArrayList<>();
        File file = new File(str);
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            Arrays.sort(listFiles, new ModifiedTimeComparator());
            for (File file2 : listFiles) {
                if (!file2.isDirectory() && IsFilePhoto(file2.getPath())) {
                    arrayList.add(file2.getAbsolutePath());
                    if (arrayList.size() >= i) {
                        break;
                    }
                }
            }
        }
        return arrayList;
    }

    public static String getDateTimeSystem(String str, int i) {
        String str2;
        Date date;
        epsonImage epsonimage = new epsonImage();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd");
        try {
            str2 = new ExifInterface(str).getAttribute(android.media.ExifInterface.TAG_DATETIME);
            if (str2 == null) {
                str2 = epsonimage.epsmpGetDate(str);
                EPLog.e("PHOTO", "Get day from C not from EXIF");
            }
        } catch (IOException e) {
            e.printStackTrace();
            EPLog.e("PHOTO", "Get day from C not from EXIF");
            str2 = epsonimage.epsmpGetDate(str);
        }
        try {
            date = simpleDateFormat.parse(str2);
        } catch (ParseException e2) {
            e2.printStackTrace();
            EPLog.e("PHOTO", "Get last modified day");
            date = new Date(new File(str).lastModified());
        }
        String[] split = simpleDateFormat.format(date).split(":");
        switch (i) {
            case 1:
                return split[1] + CommonDefine.SLASH + split[2] + CommonDefine.SLASH + split[0];
            case 2:
                return split[2] + CommonDefine.SLASH + split[1] + CommonDefine.SLASH + split[0];
            case 3:
                return split[0] + CommonDefine.SLASH + split[1] + CommonDefine.SLASH + split[2];
            default:
                return "";
        }
    }

    public static Bitmap createBitmapWithUri(String str, int i, int i2, boolean z) throws IOException {
        if (!z) {
            return getBitmapView(str, i, i2);
        }
        return createBitmapGrayscale(str, i, i2);
    }

    public static Bitmap ConvertToGrayscale(Bitmap bitmap) {
        int rgb = Color.rgb(54, 182, 18);
        for (int i = 0; i < bitmap.getHeight(); i++) {
            for (int i2 = 0; i2 < bitmap.getWidth(); i2++) {
                bitmap.setPixel(i2, i, rgb);
            }
        }
        return bitmap;
    }

    public static Bitmap createBitmapGrayscale(String str, int i, int i2) throws IOException {
        Bitmap bitmap;
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0.0f);
        ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
        Paint paint = new Paint();
        paint.setColorFilter(colorMatrixColorFilter);
        int i3 = i2;
        int i4 = i;
        Bitmap bitmap2 = null;
        while (true) {
            if (bitmap2 != null) {
                break;
            }
            try {
                bitmap2 = getBitmapView(str, i4, i3);
                if (bitmap2 != null) {
                    int width = bitmap2.getWidth();
                    int height = bitmap2.getHeight();
                }
            } catch (OutOfMemoryError unused) {
                i4 /= 2;
                i3 /= 2;
                if (bitmap2 != null) {
                    bitmap2.recycle();
                    bitmap2 = null;
                }
            }
        }
        if (bitmap2 != null) {
            bitmap = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), Bitmap.Config.ARGB_8888);
        } else {
            bitmap = null;
        }
        if (bitmap == null) {
            return null;
        }
        new Canvas(bitmap).drawBitmap(bitmap2, 0.0f, 0.0f, paint);
        if (bitmap2 != null) {
            bitmap2.recycle();
        }
        return bitmap;
    }

    public static Bitmap getBitmapView(String r4, int r5, int r6) throws java.io.FileNotFoundException {
        throw new UnsupportedOperationException("Method not decompiled: epson.print.Util.Photo.getBitmapView(java.lang.String, int, int):android.graphics.Bitmap");
    }

    public static int computeSampleSize(InputStream inputStream, int i, int i2) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(inputStream, (Rect) null, options);
        return computeSampleSize(options, Math.min(i, i2) / 2, i * i2);
    }

    public static int computeSampleSize(BitmapFactory.Options options, int i, int i2) {
        int computeInitialSampleSize = computeInitialSampleSize(options, i, i2);
        if (computeInitialSampleSize > 8) {
            return ((computeInitialSampleSize + 7) / 8) * 8;
        }
        int i3 = 1;
        while (i3 < computeInitialSampleSize) {
            i3 <<= 1;
        }
        return i3;
    }

    public static int computeInitialSampleSize(BitmapFactory.Options options, int i, int i2) {
        int i3;
        int i4;
        double d = options.outWidth;
        double d2 = options.outHeight;
        if (i2 == -1) {
            i3 = 1;
        } else {
            i3 = Math.ceil(Math.sqrt((d * d2) / (i2)));
        }
        if (i == -1) {
            i4 = 128;
        } else {
            double d3 = i;
            i4 = Math.min(Math.floor(d / d3), Math.floor(d2 / d3));
        }
        if (i4 < i3) {
            return i3;
        }
        if (i2 == -1 && i == -1) {
            return 1;
        }
        return i == -1 ? i3 : i4;
    }
}
