package epson.print.imgsel;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.AsyncTask;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;

import epson.common.ImageUtil;
import epson.print.EPImage;

public class ImageResizer {
    private static ImageResizer sImageResizer;
    private BitmapCache mBitmapCache = new BitmapCache();
    private volatile boolean mIsTargetActivityForeground;
    private int mReqSize;

    public interface BitmapDrawer {
        void setBitmap(Bitmap bitmap);
    }

    public static synchronized ImageResizer getInstance(int i, File file) {
        ImageResizer imageResizer;
        synchronized (ImageResizer.class) {
            if (sImageResizer == null) {
                sImageResizer = new ImageResizer(i, file);
            }
            imageResizer = sImageResizer;
        }
        return imageResizer;
    }

    private ImageResizer(int i, File file) {
        mReqSize = i;
        mBitmapCache.initialize(file);
    }

    public void setActivityForeground(boolean z) {
        mIsTargetActivityForeground = z;
        if (!z) {
            mBitmapCache.saveJournal();
        }
    }

    public ResizeTask load(String str, BitmapDrawer bitmapDrawer) {
        File file = new File(str);
        Bitmap bitmap = mBitmapCache.getBitmap(file);
        if (bitmap != null) {
            updateImageViewBitmap(bitmap, bitmapDrawer);
            return null;
        }
        ResizeTask resizeTask = new ResizeTask(file, bitmapDrawer, mReqSize);
        resizeTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
        return resizeTask;
    }

    private void updateImageViewBitmap(Bitmap bitmap, BitmapDrawer bitmapDrawer) {
        if (bitmap != null && bitmapDrawer != null) {
            bitmapDrawer.setBitmap(bitmap);
        }
    }

    public class ResizeTask extends AsyncTask<Void, Void, Bitmap> {
        private WeakReference<BitmapDrawer> mImageImageDrawerReference;
        private File mOrgFile;
        private int mReqSize;

        public ResizeTask(File file, BitmapDrawer bitmapDrawer, int i) {
            mOrgFile = file;
            mImageImageDrawerReference = new WeakReference<>(bitmapDrawer);
            mReqSize = i;
        }

        protected Bitmap doInBackground(Void... voidArr) {
            File file;
            Bitmap bitmap;
            if (isCancelled() || !ImageResizer.this.mIsTargetActivityForeground || (file = mOrgFile) == null) {
                return null;
            }
            try {
                bitmap = ImageUtil.loadBitmap(file.toString(), mReqSize, mReqSize, true, true);
            } catch (OutOfMemoryError e) {
                Log.w("ImageResizer", "doInBackground:" + e.toString());
                bitmap = null;
            }
            if (isCancelled()) {
                return null;
            }
            if (bitmap == null) {
                return bitmap;
            }
            Bitmap rotateBitmap = rotateBitmap(this.mOrgFile, bitmap);
            ImageResizer.this.mBitmapCache.addBitmap(this.mOrgFile, rotateBitmap);
            return rotateBitmap;
        }

        private void testSleep(int i) {
            try {
                Thread.sleep((long) i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        private Bitmap rotateBitmap(File file, Bitmap bitmap) {
            int exifRotation = getExifRotation(file);
            if (exifRotation == 0) {
                return bitmap;
            }
            Matrix matrix = new Matrix();
            matrix.setRotate((float) exifRotation);
            return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }

        private int getExifRotation(File file) {
            try {
                int attributeInt = new ExifInterface(file.toString()).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 1);
                if (attributeInt == 3) {
                    return EPImage.EPS_ROTATE_180;
                }
                if (attributeInt == 6) {
                    return 90;
                }
                if (attributeInt != 8) {
                    return 0;
                }
                return EPImage.EPS_ROTATE_270;
            } catch (IOException unused) {
                return 0;
            }
        }

        protected void onPostExecute(Bitmap bitmap) {
            BitmapDrawer bitmapDrawer = (BitmapDrawer) mImageImageDrawerReference.get();
            if (bitmap != null && bitmapDrawer != null) {
                bitmapDrawer.setBitmap(bitmap);
            }
        }
    }
}
