package epson.print.imgsel;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.p000v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import epson.common.ScalableImageView;
import epson.print.imgsel.ImageResizer;

public class ImageViewBaseFragment extends Fragment implements ImageResizer.BitmapDrawer {
    protected static final String ARG_FILENAME = "file_name";
    protected static final String ARG_MESSAGE_TYPE = "message_type";
    private static final String paramOffsetX = "paramOffsetX";
    private static final String paramOffsetY = "paramOffsetY";
    private static final String paramScale = "paramScale";
    protected String mFileName;
    protected ScalableImageView mImageView;
    protected OnFragmentInteractionListener mListener;
    protected ImageResizer.ResizeTask mLoadTask;
    protected ProgressBar mProgressBar;

    public interface OnFragmentInteractionListener {
        ImageSelector getSelector();

        void onSingleModeItemSelected();
    }

    public static ImageViewBaseFragment newInstance(String str, boolean z, int i) {
        if (z) {
            return ImageViewSingleFragment.newInstance(str, i);
        }
        return ImageViewMultiSelectFragment.newInstance(str);
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (getArguments() != null) {
            this.mFileName = getArguments().getString("file_name");
        }
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        if (bundle != null) {
            ScalableImageView scalableImageView = this.mImageView;
            scalableImageView.setmScaleFactor(bundle.getFloat(paramScale, scalableImageView.getmScaleFactor()));
            ScalableImageView scalableImageView2 = this.mImageView;
            scalableImageView2.setmOffsetX(bundle.getFloat(paramOffsetX, scalableImageView2.getmOffsetX()));
            ScalableImageView scalableImageView3 = this.mImageView;
            scalableImageView3.setmOffsetY(bundle.getFloat(paramOffsetY, scalableImageView3.getmOffsetY()));
        }
        return super.onCreateView(layoutInflater, viewGroup, bundle);
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putFloat(paramScale, this.mImageView.getmScaleFactor());
        bundle.putFloat(paramOffsetX, this.mImageView.getmOffsetX());
        bundle.putFloat(paramOffsetY, this.mImageView.getmOffsetY());
    }

    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        this.mLoadTask = ((ImageViewPagerActivity) getActivity()).getImageResizer().load(this.mFileName, this);
    }

    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            this.mListener = (OnFragmentInteractionListener) context;
        } catch (ClassCastException unused) {
            throw new ClassCastException(context.toString() + " must implement OnFragmentInteractionListener");
        }
    }

    public void onDestroy() {
        super.onDestroy();
        ImageResizer.ResizeTask resizeTask = this.mLoadTask;
        if (resizeTask != null) {
            resizeTask.cancel(true);
        }
    }

    public void onDetach() {
        super.onDetach();
        this.mListener = null;
    }

    public void setBitmap(Bitmap bitmap) {
        ScalableImageView scalableImageView = this.mImageView;
        if (scalableImageView != null && bitmap != null) {
            scalableImageView.setImageBitmap(bitmap);
            this.mImageView.setVisibility(0);
            this.mProgressBar.setVisibility(4);
        }
    }
}
