package epson.print.imgsel;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.p000v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;

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

import epson.common.Utils;
import epson.print.ActivityIACommon;
import epson.print.R;

public abstract class ImageSelectActivity extends ActivityIACommon implements ImageFolderListFragment.FolderSelectedListener, ImageGridFragment.ImageGridListener, LocalAlertDialogFragment.DialogCallback {
    public static final int DIALOG_CODE_ERROR_FINISH = 1;
    public static final String PARAM_SELECTED_FILE_LIST = "selected_files";
    private static final String STATE_SELECT_LIST = "select_list";
    private static final String TAG_FRAGMENT_FOLDER_LIST = "folder_list";
    private static final String TAG_FRAGMENT_IMAGE_GRID = "image_grid";
    private ImageSelector mImageSelector;

    public abstract ImageFinder getImageFinder();

    public int getMessageType() {
        return 0;
    }

    protected abstract void goNext();

    public boolean showNfcInvalidMessageOnPagerActivity() {
        return false;
    }

    public abstract boolean singleImageMode();

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_image_selector);
        setActionBar("", true);
        if (bundle == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.container, new ImageFolderListFragment(), TAG_FRAGMENT_FOLDER_LIST).commit();
        }
        mImageSelector = new ImageSelector();
        if (bundle != null) {
            ArrayList<String> stringArrayList = bundle.getStringArrayList(STATE_SELECT_LIST);
            if (stringArrayList != null) {
                mImageSelector.replaceFiles(stringArrayList);
            }
        } else {
            ArrayList<String> selectedListFromIntent = getSelectedListFromIntent(getIntent());
            if (selectedListFromIntent != null) {
                mImageSelector.replaceFiles(selectedListFromIntent);
            }
        }
        if (!Utils.isMediaMounted()) {
            LocalAlertDialogFragment.newInstance(getResources().getString(R.string.str_no_sdcard), 1).show(getSupportFragmentManager(), "alert_dialog");
        } else {
            AltThumbnailCache.initThumbnailCache(getApplicationContext());
        }
    }

    private ArrayList<String> getSelectedListFromIntent(Intent intent) {
        if (intent == null) {
            return null;
        }
        return intent.getStringArrayListExtra(PARAM_SELECTED_FILE_LIST);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        int selectedNumber = mImageSelector.selectedNumber();
        getMenuInflater().inflate(R.C2138menu.menu_image_selector, menu);
        MenuItem findItem = menu.findItem(R.id.action_go_preview);
        if (!singleImageMode()) {
            findItem.setEnabled(selectedNumber > 0);
        } else {
            findItem.setVisible(false);
        }
        if (selectedNumber <= 0) {
            ImageGridFragment gridFragment = getGridFragment();
            if (gridFragment == null) {
                setTitle(getResources().getString(R.string.FunctionName_iPrint_Photo));
            } else {
                String folderName = gridFragment.getFolderName();
                if (folderName != null) {
                    setTitle(new File(folderName).getName());
                } else {
                    setTitle(getResources().getString(R.string.FunctionName_iPrint_Photo));
                }
            }
        } else {
            setTitle(getResources().getString(R.string.photo_selected, new Object[]{Integer.valueOf(selectedNumber)}));
        }
        MenuItem findItem2 = menu.findItem(R.id.action_zoom);
        MenuItem findItem3 = menu.findItem(R.id.action_select);
        findItem2.setVisible(false);
        findItem3.setVisible(false);
        if (getGridFragment() != null && !singleImageMode()) {
            switch (getGridFragment().getPhotoSelectionMode()) {
                case 0:
                    findItem2.setVisible(false);
                    findItem3.setVisible(true);
                    break;
                case 1:
                    findItem2.setVisible(true);
                    findItem3.setVisible(false);
                    break;
            }
        }
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        int itemId = menuItem.getItemId();
        ImageGridFragment gridFragment = getGridFragment();
        if (itemId == R.id.action_thumbnail_type_select) {
            showOsCacheDialog();
            return true;
        } else if (itemId == R.id.action_go_preview) {
            goNext();
            return true;
        } else {
            if (itemId == R.id.action_select) {
                if (gridFragment != null) {
                    gridFragment.setPhotoSelectionMode(1);
                    supportInvalidateOptionsMenu();
                    return true;
                }
            } else if (itemId == R.id.action_zoom && gridFragment != null) {
                gridFragment.setPhotoSelectionMode(0);
                supportInvalidateOptionsMenu();
                return true;
            }
            return super.onOptionsItemSelected(menuItem);
        }
    }

    protected void showOsCacheDialog() {
        OsCacheDialog.newInstance().show(getSupportFragmentManager(), "dialog");
    }

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

    public void onFolderSelected(ImageFolderInfo imageFolderInfo) {
        startImageGridFragment(imageFolderInfo.mCanonicalName);
    }

    private void startImageGridFragment(String str) {
        getSupportFragmentManager().beginTransaction().replace(R.id.container, ImageGridFragment.newInstance(str), TAG_FRAGMENT_IMAGE_GRID).addToBackStack(TAG_FRAGMENT_IMAGE_GRID).commit();
    }

    protected void onPause() {
        if (isFinishing()) {
            AltThumbnailCache.deleteCacheDirectory();
        }
        super.onPause();
    }

    protected void refreshSelector(ArrayList<String> arrayList) {
        if (arrayList != null) {
            mImageSelector.replaceFiles(arrayList);
            if (getGridFragment() != null) {
                getGridFragment().updateImageSelector(arrayList);
            }
            supportInvalidateOptionsMenu();
        }
    }

    private ImageGridFragment getGridFragment() {
        return (ImageGridFragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT_IMAGE_GRID);
    }

    public ImageSelector getImageSelector() {
        return mImageSelector;
    }

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

    public void changeThumbnailCreateMethod(boolean z) {
        AltThumbnailCache.setThumbnailCreateMethod(this, z);
        reloadFragment();
    }

    private void reloadFragment() {
        if (TAG_FRAGMENT_IMAGE_GRID.equals(getActiveFragmentTag())) {
            getSupportFragmentManager().popBackStack();
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new ImageFolderListFragment(), TAG_FRAGMENT_FOLDER_LIST).commit();
    }

    private String getActiveFragmentTag() {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        int backStackEntryCount = supportFragmentManager.getBackStackEntryCount();
        if (backStackEntryCount >= 1) {
            return supportFragmentManager.getBackStackEntryAt(backStackEntryCount - 1).getName();
        }
        if (supportFragmentManager.findFragmentByTag(TAG_FRAGMENT_FOLDER_LIST) != null) {
            return TAG_FRAGMENT_FOLDER_LIST;
        }
        return null;
    }

    public static class OsCacheDialog extends DialogFragment {
        private boolean mUseMediaStoreThumbnail;

        public static OsCacheDialog newInstance() {
            return new OsCacheDialog();
        }

        public Dialog onCreateDialog(Bundle bundle) {
            CharSequence[] charSequenceArr = {getActivity().getString(R.string.str_use_mediastorage_thumbnail)};
            mUseMediaStoreThumbnail = AltThumbnailCache.isUseMediaStoreThumbnail();
            boolean[] zArr = {this.mUseMediaStoreThumbnail};
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setPositiveButton(R.string.f343ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    OsCacheDialog.this.onOkPressed();
                }
            }).setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            }).setMultiChoiceItems(charSequenceArr, zArr, new DialogInterface.OnMultiChoiceClickListener() {
                public void onClick(DialogInterface dialogInterface, int i, boolean z) {
                    if (i == 0) {
                        boolean unused = OsCacheDialog.this.mUseMediaStoreThumbnail = z;
                    }
                }
            });
            AlertDialog create = builder.create();
            create.setCanceledOnTouchOutside(false);
            return create;
        }

        private void onOkPressed() {
            ImageSelectActivity imageSelectActivity = (ImageSelectActivity) getActivity();
            if (imageSelectActivity != null) {
                imageSelectActivity.changeThumbnailCreateMethod(this.mUseMediaStoreThumbnail);
            }
        }
    }
}
