package epson.print.imgsel;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import epson.common.ScalableImageView;

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) {
            mFileName = getArguments().getString("file_name");
        }
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        if (bundle != null) {
            ScalableImageView scalableImageView = mImageView;
            scalableImageView.setmScaleFactor(bundle.getFloat(paramScale, scalableImageView.getmScaleFactor()));
            ScalableImageView scalableImageView2 = mImageView;
            scalableImageView2.setmOffsetX(bundle.getFloat(paramOffsetX, scalableImageView2.getmOffsetX()));
            ScalableImageView scalableImageView3 = 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, mImageView.getmScaleFactor());
        bundle.putFloat(paramOffsetX, mImageView.getmOffsetX());
        bundle.putFloat(paramOffsetY, mImageView.getmOffsetY());
    }

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

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

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

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

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