package epson.print.inkrpln;

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;

import android.support.p000v4.app.Fragment;

import android.support.p000v4.app.FragmentManager;
import epson.print.R;
import epson.print.IprintApplication;
import epson.print.imgsel.LocalAlertDialogFragment;
import java.io.Serializable;

public class JumpUrlFragment extends Fragment {
    private static final String DIALOG_TAG = "error-dialog";
    private static final String FRAGMENT_TAG = "tag-fragment";
    private static final String KEY_FRAGMENT_STATE = "fragment-state";
    private static final String KEY_RESULT_URL = "result_url";
    private static final String PARAM_KEY_DEPENDENCY_CREATOR = "dependency_creator";
    private static final int REQUEST_CODE_BROWSE = 1;
    private boolean mFragmentForeground;
    private FragmentStatus mFragmentStatus;
    private Uri mResultUrl;

    private enum FragmentStatus {
        WAITE_THREAD_END,
        WAITE_DIALOG_SHOW,
        WAITE_START_BROWSE_ACTIVITY,
        WAITE_RETURN_FROM_BROWSER,
        WAITE_ACTIVITY_FINISH
    }

    @NonNull
    public static JumpUrlFragment getInstance(FragmentManager fragmentManager) {
        JumpUrlFragment jumpUrlFragment = (JumpUrlFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
        if (jumpUrlFragment != null) {
            return jumpUrlFragment;
        }
        JumpUrlFragment jumpUrlFragment2 = new JumpUrlFragment();
        new Bundle().putSerializable(PARAM_KEY_DEPENDENCY_CREATOR, (Serializable) null);
        fragmentManager.beginTransaction().add((Fragment) jumpUrlFragment2, FRAGMENT_TAG).commit();
        return jumpUrlFragment2;
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setRetainInstance(true);
        this.mFragmentStatus = FragmentStatus.WAITE_THREAD_END;
        if (bundle != null) {
            this.mFragmentStatus = getFragmentState(bundle);
            this.mResultUrl = (Uri) bundle.getParcelable(KEY_RESULT_URL);
        }
        if (this.mFragmentStatus == FragmentStatus.WAITE_THREAD_END) {
            DependencyBuilder dependencyBuilder = null;
            Bundle arguments = getArguments();
            if (arguments != null) {
                dependencyBuilder = (DependencyBuilder) arguments.getSerializable(PARAM_KEY_DEPENDENCY_CREATOR);
            }
            new InkDeliverySystemTask(dependencyBuilder).execute(new Void[0]);
        }
    }

    private FragmentStatus getFragmentState(@NonNull Bundle bundle) {
        int i = bundle.getInt(KEY_FRAGMENT_STATE);
        FragmentStatus[] values = FragmentStatus.values();
        if (i < 0 || i > values.length) {
            i = 0;
        }
        return values[i];
    }

    public void onResume() {
        super.onResume();
        this.mFragmentForeground = true;
        switch (this.mFragmentStatus) {
            case WAITE_START_BROWSE_ACTIVITY:
                requestStartBrowser(this.mResultUrl);
                return;
            case WAITE_DIALOG_SHOW:
                requestShowErrorDialogAndFinishActivity();
                return;
            case WAITE_ACTIVITY_FINISH:
                requestShowErrorDialogAndFinishActivity();
                return;
            default:
                return;
        }
    }

    public void onPause() {
        super.onPause();
        this.mFragmentForeground = false;
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        if (i != 1) {
            super.onActivityResult(i, i2, intent);
        } else {
            requestFinishActivity();
        }
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putInt(KEY_FRAGMENT_STATE, this.mFragmentStatus.ordinal());
        bundle.putParcelable(KEY_RESULT_URL, this.mResultUrl);
    }

    /* access modifiers changed from: private */
    public void requestShowErrorDialogAndFinishActivity() {
        if (!this.mFragmentForeground) {
            this.mFragmentStatus = FragmentStatus.WAITE_DIALOG_SHOW;
        } else if (((LocalAlertDialogFragment) getFragmentManager().findFragmentByTag(DIALOG_TAG)) == null) {
            LocalAlertDialogFragment.newInstance(getString(R.string.EC_ERR_COMM_ERROR), R.string.fw_error_connect_server, 0).show(getFragmentManager(), DIALOG_TAG);
        }
    }

    /* access modifiers changed from: private */
    public void requestStartBrowser(Uri uri) {
        if (getActivity() == null) {
            this.mFragmentStatus = FragmentStatus.WAITE_START_BROWSE_ACTIVITY;
            this.mResultUrl = uri;
            return;
        }
        this.mFragmentStatus = FragmentStatus.WAITE_RETURN_FROM_BROWSER;
        this.mResultUrl = null;
        startActivityForResult(new Intent("android.intent.action.VIEW", uri), 1);
    }

    private void requestFinishActivity() {
        FragmentActivity activity = getActivity();
        if (activity == null) {
            this.mFragmentStatus = FragmentStatus.WAITE_ACTIVITY_FINISH;
        } else {
            activity.finish();
        }
    }

    private class InkDeliverySystemTask extends AsyncTask<Void, Void, Uri> {
        private DependencyBuilder mDependencyBuilder;

        public InkDeliverySystemTask(DependencyBuilder dependencyBuilder) {
            this.mDependencyBuilder = dependencyBuilder;
        }

        protected Uri doInBackground(Void... voidArr) {
            DependencyBuilder dependencyBuilder = this.mDependencyBuilder;
            if (dependencyBuilder == null) {
                dependencyBuilder = new DependencyBuilder();
            }
            return new InkReplenishSystem(dependencyBuilder.createPrinterAdapter(), dependencyBuilder.createInkRplnRepository()).refreshReplenInfoAndGetJumpUrl(IprintApplication.getInstance());
        }

        protected void onPostExecute(Uri uri) {
            if (uri == null) {
                JumpUrlFragment.this.requestShowErrorDialogAndFinishActivity();
            } else {
                JumpUrlFragment.this.requestStartBrowser(uri);
            }
        }
    }
}
