package epson.print.imgsel;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.p000v4.app.Fragment;

import android.support.p000v4.app.ListFragment;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import epson.print.ActivityRequestPermissions;
import epson.print.R;

public class ImageFolderListFragment extends ListFragment {
    private static final String FRAGMENT_TAG_NO_IMAGE_DIALOG = "no_image_dialog";
    private static final int RESULT_RUNTIMEPERMMISSION = 1;
    private ImageFolderAdapter mAdapter;
    private FolderFindTask mFolderFindTask;
    private boolean mIsFragmentForground;

    public interface FolderSelectedListener {
        ImageFinder getImageFinder();

        void onFolderSelected(ImageFolderInfo imageFolderInfo);
    }

    public void onListItemClick(ListView listView, View view, int i, long j) {
        if (this.mAdapter != null) {
            ((FolderSelectedListener) getActivity()).onFolderSelected(this.mAdapter.getFolderInfo(i));
        }
    }

    public void onActivityCreated(Bundle bundle) {
        super.onActivityCreated(bundle);
        this.mAdapter = new ImageFolderAdapter(getActivity());
        setListAdapter(this.mAdapter);
    }

    public void addItem(ImageFolderInfo imageFolderInfo, Bitmap bitmap) {
        ImageFolderAdapter imageFolderAdapter = this.mAdapter;
        if (imageFolderAdapter != null) {
            imageFolderAdapter.addItem(imageFolderInfo, bitmap);
            this.mAdapter.notifyDataSetChanged();
        }
    }

    public int getItemSize() {
        ImageFolderAdapter imageFolderAdapter = this.mAdapter;
        if (imageFolderAdapter == null) {
            return 0;
        }
        return imageFolderAdapter.getCount();
    }

    public void onPause() {
        super.onPause();
        this.mIsFragmentForground = false;
        FolderFindTask folderFindTask = this.mFolderFindTask;
        if (folderFindTask != null) {
            folderFindTask.cancel(true);
            this.mFolderFindTask = null;
            this.mAdapter.releaseResource();
            this.mAdapter.notifyDataSetChanged();
        }
    }

    public void onResume() {
        super.onResume();
        dismissNoImageDialog();
        this.mIsFragmentForground = true;
        if (ActivityRequestPermissions.isRuntimePermissionSupported()) {
            String[] strArr = {"android.permission.WRITE_EXTERNAL_STORAGE"};
            ActivityRequestPermissions.Permission permission = new ActivityRequestPermissions.Permission(strArr[0], new String[]{getString(R.string.permission_dialog_title), getString(R.string.permission_dialog_title)}, new String[]{ActivityRequestPermissions.DialogParameter.setMessage2((Fragment) this, getString(R.string.permission_dialog_message_storage)), ActivityRequestPermissions.DialogParameter.setMessage3A((Fragment) this, getString(R.string.permission_dialog_message_storage), getString(R.string.permission_function_storage))});
            if (!ActivityRequestPermissions.checkPermission(getActivity(), strArr)) {
                ActivityRequestPermissions.requestPermission((Fragment) this, permission, 1);
                return;
            }
        }
        startFolderFindTask();
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1) {
            switch (i2) {
                case 0:
                    getActivity().onBackPressed();
                    return;
                default:
                    return;
            }
        }
    }

    private void dismissNoImageDialog() {
        try {
            LocalAlertDialogFragment localAlertDialogFragment = (LocalAlertDialogFragment) getChildFragmentManager().findFragmentByTag(FRAGMENT_TAG_NO_IMAGE_DIALOG);
            if (localAlertDialogFragment != null) {
                localAlertDialogFragment.dismiss();
            }
        } catch (ClassCastException unused) {
        }
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public void onDetach() {
        super.onDetach();
        ImageFolderAdapter imageFolderAdapter = this.mAdapter;
        if (imageFolderAdapter != null) {
            imageFolderAdapter.releaseResource();
            this.mAdapter = null;
            setListAdapter((ListAdapter) null);
        }
    }

    private void startFolderFindTask() {
        FolderSelectedListener folderSelectedListener = (FolderSelectedListener) getActivity();
        if (folderSelectedListener != null) {
            FolderFindTask folderFindTask = this.mFolderFindTask;
            if (folderFindTask != null) {
                folderFindTask.cancel(true);
                this.mAdapter.releaseResource();
                this.mAdapter.notifyDataSetChanged();
            }
            this.mFolderFindTask = new FolderFindTask(this);
            this.mFolderFindTask.execute(new ImageFinder[]{folderSelectedListener.getImageFinder()});
        }
    }

    public void onFolderFindTaskEnd() {
        FragmentActivity activity = getActivity();
        if (activity != null && this.mIsFragmentForground && getItemSize() <= 0) {
            LocalAlertDialogFragment.newInstance(activity.getResources().getString(R.string.str_no_photo), R.string.unknow_error_title, 1).show(getChildFragmentManager(), FRAGMENT_TAG_NO_IMAGE_DIALOG);
        }
    }
}
