package epson.print.inkrpln;

import android.content.Context;
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.ThreeButtonDialog;
import epson.print.Util.EPLog;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class InkRplnProgressFragment extends Fragment {
    private static final String ARG_KEY_DEPENDENCY_BUILDER = "dependency_builder";
    private static final String DIALOG_TAG_THREE_BUTTON = "three_button_dialog";
    private static final String FRAGMENT_TAG = "InkDeliverySystem";
    private static final String KEY_FRAGMENT_STATUS = "fragment_status";
    public static final int STATUS_WAITE_ACTIVITY_FINISH = 1;
    public static final int STATUS_WAITE_DIALOG_INPUT = 3;
    public static final int STATUS_WAITE_DIALOG_OPEN = 2;
    public static final int STATUS_WAITE_THREAD_END = 0;
    private boolean mFragmentForeground;
    private int mFragmentStatus;
    private InkDeliverySystemTask mInkDeliverySystemTask;
    private InkReplenishSystem mInkReplenishSystem;

    @Retention(RetentionPolicy.SOURCE)
    private @interface FragmentStatus {
    }

    @NonNull
    public static InkRplnProgressFragment getInstance(@NonNull FragmentManager fragmentManager, @Nullable DependencyBuilder dependencyBuilder) {
        InkRplnProgressFragment inkRplnProgressFragment = (InkRplnProgressFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
        if (inkRplnProgressFragment != null) {
            return inkRplnProgressFragment;
        }
        InkRplnProgressFragment inkRplnProgressFragment2 = new InkRplnProgressFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_KEY_DEPENDENCY_BUILDER, dependencyBuilder);
        inkRplnProgressFragment2.setArguments(bundle);
        fragmentManager.beginTransaction().add((Fragment) inkRplnProgressFragment2, FRAGMENT_TAG).commit();
        return inkRplnProgressFragment2;
    }

    /* access modifiers changed from: private */
    public InkReplenishSystem getIPrintInkDsManager() {
        return this.mInkReplenishSystem;
    }

    /* access modifiers changed from: private */
    public void requestToShowDialogOrFinish() {
        if (this.mInkReplenishSystem.needsDisplayInvitationDialog(IprintApplication.getInstance())) {
            requestToShowDialog();
        } else {
            localFinishActivity();
        }
    }

    private void requestToShowDialog() {
        FragmentActivity activity = getActivity();
        if (activity == null || !this.mFragmentForeground) {
            this.mFragmentStatus = 2;
            return;
        }
        ThreeButtonDialog threeButtonDialog = (ThreeButtonDialog) activity.getSupportFragmentManager().findFragmentByTag(DIALOG_TAG_THREE_BUTTON);
        if (threeButtonDialog != null) {
            threeButtonDialog.dismiss();
        }
        ThreeButtonDialog.newInstance(R.layout.dialog_inkrpln_invitation).show(activity.getSupportFragmentManager(), DIALOG_TAG_THREE_BUTTON);
        this.mFragmentStatus = 3;
    }

    /* access modifiers changed from: package-private */
    @UiThread
    public void launchWebBrowseAndFinishActivity() {
        Uri readyInkUriFromSavedData = this.mInkReplenishSystem.getReadyInkUriFromSavedData(IprintApplication.getInstance());
        if (readyInkUriFromSavedData != null) {
            startActivity(new Intent("android.intent.action.VIEW", readyInkUriFromSavedData));
            this.mFragmentStatus = 1;
            return;
        }
        localFinishActivity();
    }

    private void setInkDsServerCheckDisable(@NonNull Context context) {
        this.mInkReplenishSystem.setServerCheckDisabled(context);
    }

    public void onResume() {
        super.onResume();
        this.mFragmentForeground = true;
        switch (this.mFragmentStatus) {
            case 1:
                localFinishActivity();
                return;
            case 2:
                requestToShowDialog();
                return;
            default:
                return;
        }
    }

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

    private void localFinishActivity() {
        this.mFragmentStatus = 1;
        FragmentActivity activity = getActivity();
        if (activity != null) {
            activity.finish();
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setRetainInstance(true);
        DependencyBuilder dependencyBuilder = (DependencyBuilder) getArguments().getSerializable(ARG_KEY_DEPENDENCY_BUILDER);
        if (dependencyBuilder == null) {
            dependencyBuilder = new DependencyBuilder();
        }
        this.mInkReplenishSystem = new InkReplenishSystem(dependencyBuilder.createPrinterAdapter(), dependencyBuilder.createInkRplnRepository());
        this.mFragmentForeground = false;
        this.mFragmentStatus = getFragmentStatusFromSavedInstanceState(bundle);
        if (this.mFragmentStatus == 0) {
            this.mInkDeliverySystemTask = new InkDeliverySystemTask();
            this.mInkDeliverySystemTask.execute(new Void[0]);
        }
    }

    private int getFragmentStatusFromSavedInstanceState(Bundle bundle) {
        if (bundle == null) {
            return 0;
        }
        int i = bundle.getInt(KEY_FRAGMENT_STATUS, 0);
        switch (i) {
            case 0:
            case 1:
            case 2:
            case 3:
                return i;
            default:
                return 0;
        }
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putInt(KEY_FRAGMENT_STATUS, this.mFragmentStatus);
    }

    private class InkDeliverySystemTask extends AsyncTask<Void, Void, Void> {
        private InkDeliverySystemTask() {
        }

        protected Void doInBackground(Void... voidArr) {
            try {
                InkRplnProgressFragment.this.getIPrintInkDsManager().tryRecordPrinterInfoFromServer(IprintApplication.getInstance());
                return null;
            } catch (IOException | IllegalArgumentException e) {
                EPLog.e("InkDeliverySystemTask", "exception <" + e.toString() + ">");
                return null;
            }
        }

        protected void onPostExecute(Void voidR) {
            InkRplnProgressFragment.this.requestToShowDialogOrFinish();
        }
    }
}
