package epson.print;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;


import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import java.io.IOException;
import java.util.ArrayList;

import epson.common.ImageUtil;
import epson.print.Util.EPLog;
import epson.print.Util.ImageFormatIdentifier;

public class GalleryAdapter extends BaseAdapter {
    private final int gallerlyHeight = 96;
    private final int gallerlyWidth = 96;
    private ArrayList<String> imageList = new ArrayList<>();
    private Context mContext;
    private final float mDensity;
    private int mGalleryItemBackground;

    public long getItemId(int i) {
        return (long) i;
    }

    public GalleryAdapter(Context context, ArrayList<String> arrayList) {
        mContext = context;
        imageList = arrayList;
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = obtainStyledAttributes.getResourceId(0, 0);
        mDensity = context.getResources().getDisplayMetrics().density;
        obtainStyledAttributes.recycle();
    }

    public void setImageList(ArrayList<String> arrayList) {
        imageList = arrayList;
        notifyDataSetChanged();
    }

    public void setAlignLeft(View view) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((WindowManager) mContext.getSystemService("window")).getDefaultDisplay().getMetrics(displayMetrics);
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        marginLayoutParams.setMargins(-(displayMetrics.widthPixels / 2), marginLayoutParams.topMargin, marginLayoutParams.rightMargin, marginLayoutParams.bottomMargin);
    }

    public int getCount() {
        return imageList.size();
    }

    public Object getItem(int i) {
        return imageList.get(i);
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        ImageView imageView;
        if (view == null) {
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            float f = mDensity;
            imageView.setLayoutParams(new Gallery.LayoutParams((int) (f * 96.0f), (int) (f * 96.0f)));
            imageView.setBackground(mContext.getResources().getDrawable(R.C2136drawable.background_gallary));
            imageView.setAdjustViewBounds(true);
            imageView.setFocusable(true);
            imageView.setPadding(10, 10, 10, 10);
        } else {
            imageView = (ImageView) view;
        }
        if (i >= 0) {
            try {
                if (i < imageList.size()) {
                    Bitmap createThumbnailBitmap = createThumbnailBitmap(this.imageList.get(i), 150);
                    if (createThumbnailBitmap != null) {
                        imageView.setImageBitmap(createThumbnailBitmap);
                    } else {
                        imageView.setImageResource(R.C2136drawable.icon_android);
                    }
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
                imageView.setImageBitmap((Bitmap) null);
            } catch (IndexOutOfBoundsException unused) {
                EPLog.m316v("index out of bounds exception in set Bitmap", "ij");
                imageView.setImageBitmap((Bitmap) null);
            } catch (OutOfMemoryError e2) {
                EPLog.m316v("err exeption", "line 240 <> EPImageList");
                e2.printStackTrace();
                imageView.setImageResource(R.C2136drawable.icon_android);
            }
        }
        return imageView;
    }

    @Nullable
    private Bitmap createThumbnailBitmap(@Nullable String str, int i) {
        if (str == null) {
            return null;
        }
        Bitmap loadBitmap = ImageUtil.loadBitmap(str, i, i, true, true);
        int jpegRotation = getJpegRotation(str);
        if (jpegRotation == 0) {
            return loadBitmap;
        }
        Matrix matrix = new Matrix();
        matrix.postRotate((float) jpegRotation);
        return Bitmap.createBitmap(loadBitmap, 0, 0, loadBitmap.getWidth(), loadBitmap.getHeight(), matrix, true);
    }

    private int getJpegRotation(@NonNull String str) {
        if (3 != ImageFormatIdentifier.identifyImageFormat(str)) {
            return 0;
        }
        try {
            int attributeInt = new ExifInterface(str).getAttributeInt(android.support.media.ExifInterface.TAG_ORIENTATION, 0);
            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;
        }
    }
}
