package epson.epsonconnectregistration;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.util.Xml;
import android.view.View;

import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import epson.common.httpclient.IAHttpClient;
import epson.print.R;

public class ECConfigurationTask extends ECBaseTask {
    private static final int CHECK_INTERVAL = 5000;
    private static final int MAX_RETRY = 6;
    private static final String TAG = "ECConfigurationTask";
    private Uri registrationUri;

    protected void onPreExecute() {
        super.onPreExecute();
        enableProgressText(true);
    }

    private void enableProgressText(boolean z) {
        View findViewById;
        Activity activity = (Activity) activityWeakReference.get();
        if (activity == null || (findViewById = activity.findViewById(R.id.status)) == null) {
            EPLog.m83w(TAG, "enableProgressText Failed");
            return;
        }
        EPLog.d(TAG, "enableProgressText called");
        findViewById.setVisibility(z ? 0 : 4);
    }

    protected ECStatus doInBackground(Void... voidArr) {
        IAHttpClient iAHttpClient = new IAHttpClient();
        Context checkParam = checkParam();
        if (checkParam == null) {
            return ECStatus.ERROR_UNEXPECTED;
        }
        Uri endpoint = getEndpoint();
        IAHttpClient.HttpPost httpPost = new IAHttpClient.HttpPost(endpoint.toString());
        SoapWEBSETUPDataInfo soapWEBSETUPDataInfo = new SoapWEBSETUPDataInfo();
        try {
            String createSetRequestWEBSETUP = SoapRequestFactory.createSetRequestWEBSETUP(checkParam, endpoint, getLang(checkParam));
            httpPost.setContentType("application/soap+xml");
            byte[] bytes = createSetRequestWEBSETUP.getBytes(Xml.Encoding.UTF_8.name());
            httpPost.setEntity(bytes);
            httpPost.setContentLength(Integer.valueOf(bytes.length));
            IAHttpClient.HttpResponse execute = iAHttpClient.execute(httpPost);
            if (execute.getResponseCode() == 200) {
                soapWEBSETUPDataInfo.parseXml(new ByteArrayInputStream(execute.getEntity().toByteArray()));
                if (!soapWEBSETUPDataInfo.getRetVal()) {
                    EPLog.m83w(TAG, "Failed enable EC 1");
                    return ECStatus.ERROR_CANNOT_ENABLE;
                }
                int i = 0;
                do {
                    String createGetRequestWEBSETUP = SoapRequestFactory.createGetRequestWEBSETUP(checkParam, endpoint);
                    httpPost.setContentType("application/soap+xml");
                    byte[] bytes2 = createGetRequestWEBSETUP.getBytes(Xml.Encoding.UTF_8.name());
                    httpPost.setEntity(bytes2);
                    httpPost.setContentLength(Integer.valueOf(bytes2.length));
                    IAHttpClient.HttpResponse execute2 = iAHttpClient.execute(httpPost);
                    if (execute2.getResponseCode() == 200) {
                        soapWEBSETUPDataInfo.parseXml(new ByteArrayInputStream(execute2.getEntity().toByteArray()));
                        if (!soapWEBSETUPDataInfo.inProgress()) {
                            break;
                        }
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        i++;
                    } else {
                        throw new IOException("soapRequest ResponseCode =  " + execute2.getResponseCode());
                    }
                } while (i < 6);
                if (!soapWEBSETUPDataInfo.isSucceeded()) {
                    EPLog.m83w(TAG, "Failed enable EC 2");
                    return ECStatus.ERROR_CANNOT_ENABLE;
                }
                registrationUri = soapWEBSETUPDataInfo.getRegistrationUri();
                return ECStatus.ERROR_NONE;
            }
            throw new IOException("soapRequest ResponseCode =  " + execute.getResponseCode());
        } catch (IOException e2) {
            e2.printStackTrace();
            return ECStatus.ERROR_COMMUNICATION;
        } catch (XmlPullParserException e3) {
            e3.printStackTrace();
            return ECStatus.ERROR_UNEXPECTED;
        }
    }

    protected void onPostExecute(ECStatus eCStatus) {
        super.onPostExecute(eCStatus);
        enableProgressText(false);
        final Activity activity = (Activity) activityWeakReference.get();
        if (activity == null) {
            EPLog.m83w(TAG, "activity is null. Maybe activity died");
        } else if (ECStatus.ERROR_NONE.equals(eCStatus)) {
            EPLog.d(TAG, "Succeed Configuration");
            AlertDialog create = new AlertDialog.Builder(activity).setTitle(R.string.ec_enabled_title).setMessage(R.string.ec_enabled_message).setPositiveButton(R.string.str_next, (DialogInterface.OnClickListener) new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    ECConfigurationTask eCConfigurationTask = ECConfigurationTask.this;
                    eCConfigurationTask.openWebPage(activity, eCConfigurationTask.registrationUri);
                    activity.finish();
                }
            }).create();
            create.setCancelable(false);
            create.show();
        } else {
            EPLog.d(TAG, "Failed Configuration");
            AlertDialog create2 = new AlertDialog.Builder(activity).setTitle(R.string.ec_enabling_error_title).setMessage(R.string.ec_enabling_error_message).setPositiveButton(R.string.str_ok, (DialogInterface.OnClickListener) new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
                    activity.finish();
                }
            }).create();
            create2.setCanceledOnTouchOutside(false);
            create2.setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialogInterface) {
                    activity.finish();
                }
            });
            create2.show();
        }
    }
}
