package epson.print.imgsel;

import android.content.Intent;
import android.os.Bundle;
import android.support.p000v4.app.FragmentManager;
import android.support.p000v4.app.FragmentStatePagerAdapter;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;

import java.io.File;
import java.util.ArrayList;

import epson.print.ActivityIACommon;
import epson.print.R;

public class ImageViewPagerActivity extends ActivityIACommon implements ImageViewBaseFragment.OnFragmentInteractionListener, LocalAlertDialogFragment.DialogCallback {
    private static final String CACHE_DIRECTORY = "imgsel_viewpager";
    public static final int DIALOG_CODE_ERROR_FINISH = 1;
    public static final int DIALOG_CODE_NOTIFY = 2;
    public static final int MESSAGE_TYPE_GENERIC = 0;
    public static final int MESSAGE_TYPE_PHOTO_COPY = 1;
    public static final String PARAMS_KEY_IMAGE_LIST = "image_file_arraylist";
    public static final String PARAMS_KEY_POSITION = "position";
    public static final String PARAMS_KEY_SELECTOR = "selector";
    public static final String PARAMS_MESSAGE_TYPE = "message_type";
    public static final String PARAMS_SINGLE_FILE_MODE = "single_file_mode";
    public static final int RESULT_CODE_GO_NEXT = 10;
    public static final String RETURN_KEY_SELECTOR = "return_selector";
    private static final String STATE_SELECTED_FILE_LIST = "selected_file_list";
    private static ArrayList<String> sParamFileList;
    private ArrayList<String> mFileList;
    private int mFirstPosition;
    private ImageResizer mImageResizer;
    private ImageSelector mImageSelector;
    private int mMessageType;
    private boolean mSingleFileMode;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_temp_image_pager);
        setActionBar("", true);
        getParamsFromIntent(getIntent(), bundle);
        if (this.mFileList == null) {
            returnAndRefreshSelection();
            return;
        }
        mImageResizer = ImageResizer.getInstance(getFullscreenImageSize(), getCacheDirectory());
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), mFileList));
        viewPager.setCurrentItem(this.mFirstPosition);
    }

    private File getCacheDirectory() {
        File externalCacheDir = getExternalCacheDir();
        if (externalCacheDir == null) {
            return null;
        }
        return new File(externalCacheDir, CACHE_DIRECTORY);
    }

    private int getFullscreenImageSize() {
        int[] screenSize = getScreenSize();
        return (screenSize[0] > screenSize[1] ? screenSize[0] : screenSize[1]) / 2;
    }

    private int[] getScreenSize() {
        int[] iArr = {0, 0};
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        iArr[0] = displayMetrics.widthPixels;
        iArr[1] = displayMetrics.heightPixels;
        return iArr;
    }

    private void getParamsFromIntent(Intent intent, Bundle bundle) {
        ArrayList<String> stringArrayList;
        mFileList = null;
        if (intent != null) {
            ArrayList<String> stringArrayListExtra = intent.getStringArrayListExtra(PARAMS_KEY_SELECTOR);
            if (!(bundle == null || (stringArrayList = bundle.getStringArrayList(STATE_SELECTED_FILE_LIST)) == null)) {
                stringArrayListExtra = stringArrayList;
            }
            mImageSelector = new ImageSelector(stringArrayListExtra);
            if (bundle == null) {
                mFileList = sParamFileList;
                sParamFileList = null;
                mFirstPosition = intent.getIntExtra(PARAMS_KEY_POSITION, 0);
                mSingleFileMode = intent.getBooleanExtra(PARAMS_SINGLE_FILE_MODE, false);
                mMessageType = intent.getIntExtra(PARAMS_MESSAGE_TYPE, 0);
            }
        }
    }

    public void onBackPressed() {
        returnAndRefreshSelection();
    }

    protected void onStop() {
        super.onStop();
    }

    protected void onSaveInstanceState(Bundle bundle) {
        bundle.putStringArrayList(STATE_SELECTED_FILE_LIST, mImageSelector.getFileArrayList());
        super.onSaveInstanceState(bundle);
    }

    public ImageSelector getSelector() {
        return mImageSelector;
    }

    public void onSingleModeItemSelected() {
        returnAndGoNext();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.C2138menu.menu_image_view_pager, menu);
        MenuItem findItem = menu.findItem(R.id.action_go_preview);
        int selectedNumber = mImageSelector.selectedNumber();
        findItem.setEnabled(selectedNumber > 0);
        if (this.mSingleFileMode) {
            setTitle("");
            findItem.setVisible(false);
        } else {
            setTitle(getResources().getString(R.string.photo_selected, new Object[]{Integer.valueOf(selectedNumber)}));
        }
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != R.id.action_go_preview) {
            return super.onOptionsItemSelected(menuItem);
        }
        returnAndGoNext();
        return true;
    }

    private void returnAndGoNext() {
        Intent intent = new Intent();
        intent.putStringArrayListExtra(RETURN_KEY_SELECTOR, mImageSelector.getFileArrayList());
        setResult(10, intent);
        finish();
    }

    private void returnAndRefreshSelection() {
        Intent intent = new Intent();
        intent.putStringArrayListExtra(RETURN_KEY_SELECTOR, mImageSelector.getFileArrayList());
        setResult(-1, intent);
        finish();
    }

    protected void onResume() {
        super.onResume();
        mImageResizer.setActivityForeground(true);
    }

    protected void onPause() {
        super.onPause();
        mImageResizer.setActivityForeground(false);
    }

    public ImageResizer getImageResizer() {
        return mImageResizer;
    }

    public void onDialogCallback(int i) {
        if (i == 1) {
            finish();
        }
    }

    private class PagerAdapter extends FragmentStatePagerAdapter {
        private ArrayList<String> mFileList;

        public PagerAdapter(FragmentManager fragmentManager, ArrayList<String> arrayList) {
            super(fragmentManager);
            mFileList = arrayList;
        }

        public Fragment getItem(int i) {
            return ImageViewBaseFragment.newInstance(this.mFileList.get(i), ImageViewPagerActivity.this.mSingleFileMode, ImageViewPagerActivity.this.mMessageType);
        }

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

    public static void setFileList(ArrayList<String> arrayList) {
        sParamFileList = arrayList;
    }
}
